我想使用AppleScript查找和替换所选文档中的文本。我想要替换的文本将始终相同,因此我想使用该字符串设置一些预定义变量,在所选文档中搜索该字符串,并将其替换为另一个预定义字符串。我想为这一个文档重复这个查找和替换过程大约4次(每次查找不同的字符串变量)。完成后,我想自动保存修改后的文档。
有人能为我提供简单的脚本吗?这是我到目前为止......
tell application "Finder"
set theFile to (open for access (choose file with prompt "Select a file to read:"))
set txt to (read theFile for (get theFile))
end tell
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
get replaceText("lorem", "ipsum", txt)
问题是它没有读取文件的所有内容(.html)。一个lorem ipsum段落它只是阅读 “ipsum ipsum dolor sit amet,consectetur adipisicing elit,sed do eius” 我尝试在(获取文件)中使用eof,但这不起作用
答案 0 :(得分:3)
烨。 :)
set the search_document to (choose file of type "TEXT")
replaceText("whatever you want to search for", "whatever you want to replace with", search_document)
on replaceText(search_string, replacement_text, this_document)
tell application "TextEdit"
open this_document
set AppleScript's text item delimiters to the search_string
set this_text to the text of the front document as list
set AppleScript's text item delimiters to the replacement_text
set the text of the front document to (this_text as string)
close this_document saving yes
end tell
end replaceText
关于此脚本的快速警告。如果您要在包含此字符串的文档中用单词one
替换单词two
的每个出现...
If someone added one plus one, what would be the result?
......这将是你的结果......
If sometwo added two plus two, what would be the result?
除此之外,没有什么可担心的。
快乐的编码! :)