且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-23 14:40:10

下面不需要文本编辑的版本。它会读取UTF-8编码文件,更新它是回文件为UTF-8 EN codeD文本内容和存储。我使用try块周围写文件的原因是,如果另一个应用程序与在同一时间读取权限打开该文件会出现错误。在考虑case块可以绕的设置TI,如果你想搜索和替换区分大小写包裹到theContent 的每一个文本项。没有必要为此当更换串,只为发现它是有效的。

 设置stringToFind为替换
设置stringToReplace为本
设置theFile选择文件
设置theContent阅读theFile为«类UTF8»
集合{oldTID,AppleScript的文本项分隔符}到{AppleScript的文本项分隔符,stringToFind}
设置TI能够theContent的每一个文本项
设置的AppleScript的文本项分隔符stringToReplace
设置newContent以Ti作为字符串
设置的AppleScript的文本项分隔符oldTID
尝试
    FD设置为打开具有写权限访问theFile
    EOF的FD设置为0
    写newContent于fd为«类UTF8»
    紧邻FD
出错
    紧邻theFile
年底试

I am trying to write an applescript which will run via a launch agent. What the script needs to do is edit a user preference plist file so that default save locations are specific to that user. I am aware that this can be done by just setting "~/documents" as the location in the template plist. But Premier Pro for example also needs to write scratch files to a local drive. For simplicity I would like each user to have these put in a locations based on their username. This script will only need to run if the local profile has just been created from a template at first log on.

I have started by using some sample code found on this site and just making a simple test below. This test should edit a txt file and replace one word with another. This script is currently not working. When tested it opens up test.txt in TextEdit but does nothing more. No errors are displayed either.

Thank you in advance

John.

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

Here an version that doesn't need text edit. It will read the file in utf-8 encoding, update it's contents and store that back into the file as utf-8 encoded text. The reason I use an try block around writing the file is that there will be an error if another application has the file open with read permission at the same time. The considering case block can be wrapped around the set ti to every text item of theContent if you want the search and replace case sensitive. There is no need for this to be active when you replace the string, only for finding it.

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