【问题标题】:Find and replace in text files using AppleScript使用 AppleScript 在文本文件中查找和替换
【发布时间】:2014-03-10 11:55:55
【问题描述】:

我正在尝试编写一个将通过启动代理运行的applescript。脚本需要做的是编辑用户首选项 plist 文件,以便默认保存位置特定于该用户。我知道这可以通过将“~/documents”设置为模板 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

【问题讨论】:

    标签: file text replace find applescript


    【解决方案1】:

    这里是不需要文本编辑的版本。它将以 utf-8 编码读取文件,更新其内容并将其作为 utf-8 编码文本存储回文件中。我在写入文件时使用 try 块的原因是,如果另一个应用程序同时以读取权限打开文件,则会出现错误。如果您希望搜索和替换区分大小写,则考虑大小写块可以围绕 set ti 到 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
    

    【讨论】:

      【解决方案2】:

      嗯,是的,正如@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 风格(冒号分隔)文件路径的变量。

      【讨论】:

        【解决方案3】:

        修改自http://discussions.apple.com/message/20542594#20542594,下面的处理程序可以做你想做的事。我做了一些更改并添加了您的变量。一些注意事项:(1)处理程序调用之前的“我的”确保它被视为脚本的处理程序,而不是 TextEdit 应该在“内部”解释的东西(因为它在一个告诉块中); (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
        

        【讨论】:

          猜你喜欢
          • 2014-01-14
          • 2021-07-26
          • 1970-01-01
          • 1970-01-01
          • 2011-06-12
          • 2012-02-14
          • 1970-01-01
          • 2020-07-29
          • 1970-01-01
          相关资源
          最近更新 更多