Hi, everyone! I’m new here.
I have been learning AppleScript myself for several days, and I ran into a problem when I was to split a text file with it.
The content of this UTF-8 text file test.txt is simple:
Each sentence is separated by 3 returns (a.k.a “\n\n\n”).
When I was trying to read this text file as a text variable and split it in a list containing one line per entry with the AppleScript below, something funny happened.
set theAlias to alias ("Macintosh:Users:troy:Desktop:test.txt")
set fp to open for access theAlias
set originalText to (read fp for (get eof fp) as «class utf8»)
close access theAlias
set oldTid to AppleScript's text item delimiters
set AppleScript's text item delimiters to (return & return & return)
set parsedList to text items of originalText
set AppleScript's text item delimiters to oldTid
parsedList
And the result shows that it doesn’t work at all. It just takes the whole text content as one entry in the list:
Can anyone tell me what is the problem with my script?
Many many thanks!
I guess that the lines are not separated by return characters but by linefeed ones.
Try :
set theAlias to alias ("Macintosh:Users:troy:Desktop:test.txt")
set originalText to paragraphs of (read fp)
set oldTid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set originalText to originalText as text
# Now, you are sure that lines are separated by linefeed characters
set AppleScript's text item delimiters to (linefeed & linefeed & linefeed)
# Split the text using the group of three linefeeds as delimiter
set parsedList to text items of originalText
set AppleScript's text item delimiters to oldTid
parsedList
Yvan KOENIG (VALLAURIS, France) samedi 27 octobre 2012 19:01:22