Reputation: 1
I could use some help writing an Applescript to parse some text from a .txt file and output to a separate .txt file. I need a script to 1) identify a section (delisted by “»=“), 2) identify if a specific set of characters exists in a section, “[*]”, 3) print section header with all sections that contain specific set of characters, 4) repeat to end of file. Basically, these are notes with action items, and I want to extract the notes that contain open action items.
Input text structure from notes.txt is as follows:
»===============================
<Date> 2016-09-19
<Topic> The topic of the day
<Attendees> Mario, Luigi
»1) Where to go from here
- Someone said this
[*] Mario: schedule this meeting
- And who wants to do that other thing?
»2) And on to the next thing
»3) So on and so forth [*] Luigi can order the pizza
»===============================
<Date> 2016-09-18
<Topic> The topic of the yesterday
<Attendees> Zelda, Wario, Snoop Dog
»1) Where we went from there
- Who said this
[x] Mario: scheduled this meeting yesterday
- And who wants to do that other thing?
»2) And on to the next thing again
»3) So on and so forth * Luigi can order the sausages
»===============================
Output text to actionItems.txt should be as follows:
»===============================
<Date> 2016-09-19
<Topic> The topic of the day
<Attendees> Mario, Luigi
»1) Where to go from here
- Someone said this
[*] Mario: schedule this meeting
- And who wants to do that other thing?
»3) So on and so forth [*] Luigi can order the pizza
»===============================
Maybe I can get a little help with this? Thanks!
Upvotes: 0
Views: 1380
Reputation: 3095
Reading and writing text file is quite easy with Applescript :
Read :
set myFile to choose file "Select your txt file" -- ask user to select txt file
set myText to (read myFile) as string -- content the text file is in variable myText
Write content of text variable newText into file 'action items.txt' on your desktop :
set newFile to ((path to desktop) as string) & "actionItems.txt"
try
open for access file newFile with write permission
write (newText & return) to file newFile starting at eof
close access file newFile
on error
try
close access file newFile
end try
end try
In between, you must split your text using the powerful 'item text delimiters'. You must first define the delimiters like :
set AppleScript's text item delimiters to {"»=", "¬ª="}
(during my test, I found that your "»=" should be, in fact set as "¬ª=" !!)
Then you can access to each text item (the part of text between 2 delimiters) through a repeat loop :
set AppleScript's text item delimiters to {"»=", "¬ª="}
set myNotes to text items of myText
set newText to ""
repeat with I from 1 to (count of myNotes) --loop through each item
-- do what ever you need with 'item I of myNotes'
set Newtext to "this is my test" -- assign new text to be written
end repeat
You will need to adjust your tests in the loop: for instance if your text starts with a space, then, 1st item will not be your expected header, but a space !
Last thing : to test if a text variable contains something, use the 'contains' word :
Set A to "This is my text sample"
if A contains "is my" then
-- A contains 'is my'
else
-- A do not contains 'is my'
end if
Good luck !
Upvotes: 1