使用AppleScript在文本文件中查找和替换

3
我正在尝试编写一个通过启动代理运行的AppleScript。脚本需要编辑用户首选项plist文件,以便默认保存位置特定于该用户。我知道可以通过在模板plist中设置“〜/文档”作为位置来完成这个操作。但例如Premier Pro还需要将临时文件写入本地驱动器。为简单起见,我希望每个用户都将这些文件放置在基于其用户名的位置上。如果本地配置文件是从模板在第一次登录时创建的,则只需要运行此脚本。
我已经开始使用在此网站上找到的一些示例代码,并制作了下面的简单测试。此测试应编辑一个txt文件并用另一个单词替换一个单词。但是脚本目前无法正常工作。测试时它会在TextEdit中打开test.txt,但不会执行其他操作。也没有显示任何错误。
提前感谢您。
约翰。
replaceText("replace this", "replace with this", "/Volumes/USB_Drive/test.txt")


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
3个回答

4

这里有一个不需要文本编辑的版本。它将以 utf-8 编码读取文件,更新其内容并将其作为 utf-8 编码的文本存储回文件中。我在写入文件周围使用 try 块的原因是:如果另一个应用程序同时以读权限打开该文件,则会出现错误。如果您想进行搜索和替换(大小写敏感),则可以将 considering case 块包装在 set ti to every text item of theContent 周围。只有在查找字符串时才需要这样做,替换字符串时不需要。

set stringToFind to "replace that"
set stringToReplace to "with this"
set theFile to choose file
set theContent to read theFile as «class utf8»
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
set ti to every text item of theContent
set AppleScript's text item delimiters to stringToReplace
set newContent to ti as string
set AppleScript's text item delimiters to oldTID
try
    set fd to open for access theFile with write permission
    set eof of fd to 0
    write newContent to fd as «class utf8»
    close access fd
on error
    close access theFile
end try

4

好的,是的,正如@dj_bazzie_wazzie所指出的那样,你实际上不需要一个文本编辑器来完成这个任务,你可以使用终端并执行类似于以下的命令:

perl -pi -e 's/old text/new text/g' /path/to/theFile.plist

当然,你可以在AppleScript中使用强大的do shell script命令:

do shell script "perl -pi -e 's/" & search_string & "/" & replacement_text & "/g' " & quoted form of (POSIX path of file_path)

--假设file_path是一个变量,其值为Mac风格(以冒号分隔)的文件路径。


1

http://discussions.apple.com/message/20542594#20542594修改而来,下面的处理程序可以实现你想要的功能。我进行了一些更改并添加了你的变量。几个注意点:(1)在处理程序调用之前加上'my'确保它被视为脚本的处理程序而不是TextEdit应该“内部”解释的内容(因为它在tell块中);(2)'considering case'使处理程序区分大小写,我认为这是你想要的;(3)你可能会考虑使用类似于TextWrangler这样的工具,它有强大和丰富的AppleScript支持,并包括在其AS字典中的文本替换,Smile也是如此,它是一个很棒的脚本编辑器(可以处理文本,并且能够很好地格式化plist文件)。

tell application "TextEdit"
    set workingWin to open this_document
    set this_text to the text of the workingWin
    set the text of the workingWin to (my replaceText(this_text, search_string, replacement_text))
    close workingWin saving yes
end tell

to replaceText(someText, oldItem, newItem)
    (*
         replace all occurances of oldItem with newItem
              parameters -     someText [text]: the text containing the item(s) to change
                        oldItem [text, list of text]: the item to be replaced
                        newItem [text]: the item to replace with
              returns [text]:     the text with the item(s) replaced
         *)
    considering case
        set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
        try
            set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
            set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
        on error errorMessage number errorNumber -- oops
            set AppleScript's text item delimiters to tempTID
            error errorMessage number errorNumber -- pass it on
        end try
    end considering
    return someText
end replaceText

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接