O suspeito habitual de processar texto no AppleScript é a propriedade global incorporada text item delimiters
. Você pode usá-lo para dividir uma única sequência em várias partes (ao usar o formulário de referência text items … of
), selecionar pontos de extremidade de intervalo (ao usar text item N
no formulário de referência text … of
) e unir várias partes em uma única cadeia (quando coagir uma lista para uma string).
to pickText(str, startAfter, stopBefore)
set pickedText to {}
set minLength to (length of startAfter) + (length of stopBefore)
repeat while length of str is greater than or equal to minLength
-- look for the start marker
set text item delimiters to startAfter
-- finish if it is not there
if (count of text items of str) is less than 2 then exit repeat
-- drop everything through the end of the first start marker
set str to text from text item 2 to end of str
-- look for the end marker
set text item delimiters to stopBefore
-- finish if it is not there
if (count of text items of str) is less than 2 then exit repeat
-- save the text up to the end marker
set end of pickedText to text item 1 of str
-- try again with what is left after the first end marker
set str to text from text item 2 to end of str
end repeat
set text item delimiters to " "
pickedText as string
end pickText
-- process some “hard coded” text
set s to "I love eating apples. I love eating cherries. I love eating grapes, pecans, and peaches."
pickText(s, "I love eating ", ".") --> "apples cherries grapes, pecans, and peaches"
-- process text from a file
set s to read file ((path to desktop folder as string) & "Untitled.txt")
pickText(s, "I love eating ", ".")