使用 AppleScript 在文本文件中查找和替换 [英] Find and replace in text files using AppleScript

查看:53
本文介绍了使用 AppleScript 在文本文件中查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个通过启动代理运行的苹果脚本.该脚本需要做的是编辑用户首选项 plist 文件,以便默认保存位置特定于该用户.我知道这可以通过将~/documents"设置为模板 plist 中的位置来完成.但例如 Premier Pro 还需要将临时文件写入本地驱动器.为简单起见,我希望每个用户都根据他们的用户名将这些放在一个位置.只有在首次登录时才从模板创建本地配置文件时,才需要运行此脚本.

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.

我已经开始使用在本网站上找到的一些示例代码,并在下面进行了一个简单的测试.此测试应编辑一个 txt 文件并将一个单词替换为另一个单词.此脚本当前不起作用.测试时,它会在 TextEdit 中打开 test.txt,但什么也不做.也不会显示任何错误.

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.

提前致谢

约翰.

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

推荐答案

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

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

这篇关于使用 AppleScript 在文本文件中查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆