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

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

问题描述

我想写这将通过推出代理运行AppleScript。什么脚本需要做的就是编辑用户preference plist文件,使默认的保存位置是特定于该用户。据我所知,这可以通过只设置〜/文件为模板的plist的位置来完成。但是,premier临例如还需要从头文件写入到本地驱动器。为简单起见,我想每个用户拥有这些认沽基于他们的用户名一个位置。这个脚本会只需要当本地配置文件刚刚从模板初次创造登录到运行。

我已经使用本网站上的一些样本code和只是让下面一个简单的测试开始。这个测试应该修改txt文件,并替换另一个一个字。这个脚本是目前没有工作。当测试它开辟了test.txt的在TextEdit中,但确实仅此而已。要么显示没有错误。

感谢您提前

约翰。

  replaceText(替换这个,这个替换,/Volumes/USB_Drive/test.txt)
在replaceText(search_string的,replacement_text,this_document)
告诉应用程序文字编辑
    开放this_document
    设置的AppleScript的文字分隔符项目的SEARCH_STRING
    this_text设置到前文档列表的文本
    设置的AppleScript的文字分隔符项目的replacement_text
    前面文档的文本设置为(this_text为字符串)
    接近this_document是节约
告诉结束
结束replaceText


解决方案

下面不需要文本编辑的版本。它会读取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

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

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