file - 使用 AppleScript 查找和替换文本文件中的多个实例
问题描述
我完全没有编码或任何知识。但在日常生活中,我必须在 Mac 的 TextEdit 中打开一个文件才能打开一个 .txt 文件并查找和替换几个文本实例。不难,但如果有一个自动化的解决方案来解决这个问题(节省时间并避免人为错误),那就太好了。
在这个网站上,我发现了 dj bazzie wazzie 的这个有趣的脚本,即使我使用 Automator 也可以让它工作,所以很有希望!
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
但是,我需要更改的不是文件中的 1 件事而是 3 件事。我怎样才能调整这段代码,而不是用“用这个”代替“替换那个”,而是用“成熟的苹果”替换“未成熟的苹果”,用“成熟的猕猴桃”替换“未成熟的猕猴桃”和“未成熟的香蕉”替换“熟香蕉”?
我一直对此感到困惑,可能答案比预期的要容易得多,因为我是个菜鸟,我还没有找到解决方案。
对此的想法非常受欢迎。提前谢谢了!
解决方案
示例 AppleScript 代码(如下所示)在macOS Catalina下的脚本编辑器中进行了测试,系统偏好设置中的语言和区域设置设置为英语(美国) - 主要并且为我工作,没有问题1。
- 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。
示例 AppleScript 代码:
set textToFindList to {"unripe apple", "unripe kiwi", "unripe banana"}
set textToReplaceWithList to {"ripe apple", "ripe kiwi", "ripe banana"}
if (length of textToFindList) is not equal to ¬
(length of textToReplaceWithList) then return
set theFile to choose file
-- tell application "Finder" to duplicate file theFile with exact copy
set theText to read theFile as «class utf8»
repeat with i from 1 to length of textToFindList
set theText to my findAndReplaceInText(theText, ¬
item i of textToFindList, ¬
item i of textToReplaceWithList)
end repeat
my writeToFile(theText, theFile, true)
-- # Handlers #
on findAndReplaceInText(theText, theSearchString, theReplacementString)
--considering case
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
--end considering
end findAndReplaceInText
on writeToFile(theText, theFile, overwriteExistingContent)
try
set theFile to theFile as string
if theFile contains "/" then
set theOpenedFile to open for access theFile with write permission
else
set theOpenedFile to open for access file theFile with write permission
end if
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theText to theOpenedFile starting at eof
close access theOpenedFile
return true
on error
try
close access file theFile
end try
return false
end try
end writeToFile
笔记:
该writeToFile(theText, theFile, overwriteExistingContent)
处理程序是一个稍加修改的处理程序,来自:Reading and Writing Files
链接的Apple支持文档中的处理程序已修改为处理POSIX和HFS+文件路径。
findAndReplaceInText(theText, theSearchString, theReplacementString)
处理程序如果来自在字符串中查找和替换文本来自:操作文本
如果您需要区分大小写的查找/替换,请取消注释处理程序中的--considering case
和代码--end considering
行。on findAndReplaceInText(theText, theSearchString, theReplacementString)
如果您想在编辑之前自动制作文件的备份副本,请从以下代码--
行前面删除_:
tell application "Finder" to duplicate file theFile with exact copy
注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句和错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟的值 delay 0.5
适当设置。
推荐阅读
- html - flexbox 中的子级使父级大于应有的大小
- python - mplfinance 图中 bbox_inches='tight' 时的 KeyError
- javascript - 有没有办法检查 javascript/typescript 中的 CSV 上是否存在标头?
- kubernetes - 从 Skaffold 和清单迁移到 DevSpace 和组件图表后,Ingress 无法正常工作
- caching - CacketPHP 3.8 禁用从 tmp/cache 加载资产
- google-analytics - 如何测量事件时间戳 (GA4)
- android - 带有浮动顶栏的脚手架(Jetpack Compose)
- c++ - 最差拟合算法
- rust - 如何将 tera::Error 转换为 actix_web::Error?
- typescript - TypeScript 通用工厂函数基于实例参数返回类型而不重载