我编写了一个 AppleScript,它从用逗号分隔的文本文件中返回一个随机字符串:
set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
set the_text to read some_file as string
set the text item delimiters of AppleScript to ", "
set the_lines to (every text item of the_text)
return some item of the_lines
但我现在有一个用新行而不是逗号分隔的文本文件。每行以 0-20 个空格开头。我只想返回随机行的文本部分。我该怎么做呢?
另外,如果文本部分被普通(非卷曲)引号包围,我该如何修剪引号?
以下脚本将处理您的注释中的附加要求,并包含用于修剪字符串开头和结尾字符的语句。它排除从文件中读取的前 27 行,并将重复从列表中获取和修剪随机行,直到最终结果不为空。
set theFile to (choose file)
set theLines to paragraphs 28 thru -1 of (read theFile)
repeat -- forever
set someLine to some item of theLines
-- trim characters at the start
if someLine is not "" then repeat until the first character of someLine is not in {space, tab, quote}
if (count someLine) is 1 then
set someLine to ""
exit repeat
end if
set someLine to text 2 thru -1 of someLine
end repeat
-- trim characters at the end
if someLine is not "" then repeat until the last character of someLine is not in {quote}
if (count someLine) is 1 then
set someLine to ""
exit repeat
end if
set someLine to text 1 thru -2 of someLine
end repeat
if someLine is not "" then exit repeat
end repeat
return someLine
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)