Как анализировать строки в Applescript / Mac

2678
light.hammer

Как бы я написал скрипт для анализа строк в Applescript (или другой скриптовой программе для Mac)? Пример: отсканируйте текстовый файл на предмет «Я люблю поесть», скопируйте следующий текст, в конце «.», Чтобы, если мой файл был «Я люблю есть яблоки. Я люблю есть вишню. Я люблю есть виноград, орехи пекан и персики». он вернул бы "яблоки вишни виноград, орехи пекан и персики"

0

1 ответ на вопрос

1
Chris Johnsen

Обычный подозреваемый для обработки текста в AppleScript - встроенное глобальное свойство text item delimiters. Вы можете использовать его как для разбиения одной строки на несколько частей (при использовании text items … ofформы ссылки), так и для выбора конечных точек диапазона (при использовании text item Nв text … ofформе ссылки), а также для объединения нескольких частей в одну строку (при приведении списка к строке). ,

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 ", ".") 
Хотя глупый вопрос: как бы я создал разделители текстовых элементов, которые включали бы кавычки? Очевидно, что "" "не сработает light.hammer 13 лет назад 0
Вы можете экранировать символ кавычки, используя обратную косую черту: "\" " NSGod 13 лет назад 1