我正在尝试使用文件文本中的字符串用applescript重命名一些文本文件。这些文件是旧的Usenet消息,最终以严重混乱的名称(如REARIY-1)结尾。我已经弄清楚了如何读取文件并找到我想要的文本字符串(由SUBJECT进行:)我还想出了如果用户键入文本时如何重命名文件,但是打开时遇到了麻烦读取文件,并获取允许我重命名的路径。
到目前为止,我所做的最好的事情如下(这要求用户小心选择两次相同的文件):
set foo to choose file with multiple selections allowed
set fileAlias to foo as alias
set footext to read file ((choose file with prompt "phhhht") as string)
set targetText to "Subject:"
set extractedProperty to ""
set foundIt to "Target text not found."
set oldDelims to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {ASCII character 13}
set bar to text items of footext
repeat with thisItem in bar
if thisItem contains targetText then
set foundIt to thisItem
exit repeat
end if
end repeat
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
try
if thisItem contains "not found" then
display dialog "text not found, filename not changed"
tell me to quit
end if
end try
repeat with thisWord in words of foundIt
if contents of thisWord is not targetText then
set extractedProperty to extractedProperty & thisWord & " "
end if
end repeat
set extractedProperty to characters 9 thru -2 of extractedProperty as text
display dialog extractedProperty -- for check only
tell application "Finder"
set name of fileAlias to (extractedProperty & ".txt")
end tell
您只需要从以下位置更改第三行:
set foo to choose file with multiple selections allowed
set fileAlias to foo as alias
set footext to read file ((choose file with prompt "phhhht") as string)
至
set footext to read file (foo as string)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句