Applescript 查找、替换和样式
Applescript find, replace and style
我一直在努力让这个简单的脚本工作,它应该搜索一个字符串,在应用一些样式时替换它,例如将文本设置为粗体和红色。
这是我目前的情况:
tell application "Finder"
set fl to files of folder POSIX file "/Users/Sc/Desktop/app/" as alias list
end tell
repeat with f in fl
tell application "TextEdit"
open f
set text of front document to replace_chars(text of front document, "a", "0000") of me
end tell
end repeat
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set the size of replacement_string to 14
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
但是这会产生错误,我理解原因,但不确定如何在更换之前对其进行样式设置,如有任何帮助,我们将不胜感激。
TextEdit
不是搜索和替换样式文本的最佳工具,例如免费的 TextWrangler
具有更强大的技能。
无论如何,试试这个,它只影响 TextEdit
中打开的一个文档
"algorithm"是计算搜索字符串的偏移量,用replace字符串替换,并保持位置在repeat循环中。最后将更改的文本重新分配给 TextEdit
的文本对象,并将存储位置的样式更改为 {font: Helvetica Bold, size: 24 and color: red}
.
property searchString : "a"
property replaceString : "0000"
tell application "TextEdit"
set completed to false
set ranges to {}
set theText to text of front document
set theSize to size of text of front document
repeat while completed is false
tell current application to set o to offset of searchString in theText
if o is 0 then
set completed to true
else
tell theText to set newText to text 1 thru (o - 1) & replaceString & text (o + (length of searchString)) thru -1
set end of ranges to o
copy newText to theText
end if
end repeat
set text of front document to theText
set size of text of front document to theSize
repeat with aRange in ranges
tell characters aRange thru (aRange + (length of replaceString) - 1) of front document
set size to 24
set its color to {65535, 0, 0}
set font to "Helvetica Bold"
end tell
end repeat
end tell
TextEdit 使用 Cocoa Scripting 的标准 Text Suite 实现,即使在最好的情况下也很笨拙。如果你正在做 whole-word 替换,以下应该有效:
tell application "TextEdit"
set aRef to a reference to (every word of document 1 where it is "OLDWORD")
set aRef's color to {65535, 0, 0}
set aRef's contents to "NEWWORD"
end tell
同样的方法也适用于single-character或whole-paragraph替换;只需适当修改 every word of...
查询即可。但是,在每种情况下,您都必须匹配 exactly one word/character/paragraph 并将其替换为 exactly one word/character/paragraph,否则您由于 Cocoa 脚本由愚蠢和无能的 [2] 组成(例如尝试将重复出现的单词更改为两个新单词,例如 "foo"
-> "boo boo"
明白我的意思)。
此外,与在更好编写的应用程序中发现的 Text Suite 实现不同,CocoaScripting 的 Text Suite 无法描述 文本范围(例如 text (character i) thru (character j) of...
/text i thru j of...
), 所以不可能告诉 TextEdit 去匹配 "oo" in "baboon", "fool", "spitoon", 等等。如果你需要匹配任意字符范围,您必须将文本输入 AppleScript 并自己计算每个匹配项的开始和结束。
[1] 在进行多次替换时,CS的Text Suite会先计算出所有需要更改的文本范围的位置,然后从第一个匹配开始到最后一个结束进行每次替换。因此,新旧文本长度的任何差异都意味着它已经计算出的所有剩余匹配索引不再正确,因为之前在该位置的字符现在已经向左或向右移动。为了正确完成工作,CS 应该从头到尾计算所有位置,然后从 last 开始替换它们并向后 backwards到第一个。
[2](CocoaScripting.framework
最初是由不了解 AppleScript 工作原理的 Cocoa 开发人员设计的,此后一直由也不了解 AppleScript 的开发人员维护。所以它去。)
由于其他发帖者都指出 TextEdit 不适合这项任务,请允许我提出一个 "out of the box" 建议:使用 MS Word
Word 中的查找和替换工具可以轻松处理此任务,以及更复杂的替换。
如果您有很多 RTF 格式的源文档,您可以在 Word 中打开它们,进行更改,然后保存回相同(或不同)的 RTF 文件。创建一个 Word VBA 宏来执行此操作非常容易。
然后您可以编写 AppleScript 以在 Word 和 运行 宏中打开每个 RTF 文件,或者您可以使用主 Word VBA 完成所有处理。你可以用任何一种方式来做,但我会选择所有 VBA 路线,因为在我看来,它是一种比 AppleScript 更好的处理 MS 文档的语言。
当然,这需要您安装了MS Word。我仍然 运行 在我的所有 Mac 上安装 MS Office 2011,暂时不打算升级到 Office 2016。 Word 2011 运行良好。
祝你好运。
我一直在努力让这个简单的脚本工作,它应该搜索一个字符串,在应用一些样式时替换它,例如将文本设置为粗体和红色。
这是我目前的情况:
tell application "Finder"
set fl to files of folder POSIX file "/Users/Sc/Desktop/app/" as alias list
end tell
repeat with f in fl
tell application "TextEdit"
open f
set text of front document to replace_chars(text of front document, "a", "0000") of me
end tell
end repeat
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set the size of replacement_string to 14
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
但是这会产生错误,我理解原因,但不确定如何在更换之前对其进行样式设置,如有任何帮助,我们将不胜感激。
TextEdit
不是搜索和替换样式文本的最佳工具,例如免费的 TextWrangler
具有更强大的技能。
无论如何,试试这个,它只影响 TextEdit
"algorithm"是计算搜索字符串的偏移量,用replace字符串替换,并保持位置在repeat循环中。最后将更改的文本重新分配给 TextEdit
的文本对象,并将存储位置的样式更改为 {font: Helvetica Bold, size: 24 and color: red}
.
property searchString : "a"
property replaceString : "0000"
tell application "TextEdit"
set completed to false
set ranges to {}
set theText to text of front document
set theSize to size of text of front document
repeat while completed is false
tell current application to set o to offset of searchString in theText
if o is 0 then
set completed to true
else
tell theText to set newText to text 1 thru (o - 1) & replaceString & text (o + (length of searchString)) thru -1
set end of ranges to o
copy newText to theText
end if
end repeat
set text of front document to theText
set size of text of front document to theSize
repeat with aRange in ranges
tell characters aRange thru (aRange + (length of replaceString) - 1) of front document
set size to 24
set its color to {65535, 0, 0}
set font to "Helvetica Bold"
end tell
end repeat
end tell
TextEdit 使用 Cocoa Scripting 的标准 Text Suite 实现,即使在最好的情况下也很笨拙。如果你正在做 whole-word 替换,以下应该有效:
tell application "TextEdit"
set aRef to a reference to (every word of document 1 where it is "OLDWORD")
set aRef's color to {65535, 0, 0}
set aRef's contents to "NEWWORD"
end tell
同样的方法也适用于single-character或whole-paragraph替换;只需适当修改 every word of...
查询即可。但是,在每种情况下,您都必须匹配 exactly one word/character/paragraph 并将其替换为 exactly one word/character/paragraph,否则您由于 Cocoa 脚本由愚蠢和无能的 [2] 组成(例如尝试将重复出现的单词更改为两个新单词,例如 "foo"
-> "boo boo"
明白我的意思)。
此外,与在更好编写的应用程序中发现的 Text Suite 实现不同,CocoaScripting 的 Text Suite 无法描述 文本范围(例如 text (character i) thru (character j) of...
/text i thru j of...
), 所以不可能告诉 TextEdit 去匹配 "oo" in "baboon", "fool", "spitoon", 等等。如果你需要匹配任意字符范围,您必须将文本输入 AppleScript 并自己计算每个匹配项的开始和结束。
[1] 在进行多次替换时,CS的Text Suite会先计算出所有需要更改的文本范围的位置,然后从第一个匹配开始到最后一个结束进行每次替换。因此,新旧文本长度的任何差异都意味着它已经计算出的所有剩余匹配索引不再正确,因为之前在该位置的字符现在已经向左或向右移动。为了正确完成工作,CS 应该从头到尾计算所有位置,然后从 last 开始替换它们并向后 backwards到第一个。
[2](CocoaScripting.framework
最初是由不了解 AppleScript 工作原理的 Cocoa 开发人员设计的,此后一直由也不了解 AppleScript 的开发人员维护。所以它去。)
由于其他发帖者都指出 TextEdit 不适合这项任务,请允许我提出一个 "out of the box" 建议:使用 MS Word
Word 中的查找和替换工具可以轻松处理此任务,以及更复杂的替换。
如果您有很多 RTF 格式的源文档,您可以在 Word 中打开它们,进行更改,然后保存回相同(或不同)的 RTF 文件。创建一个 Word VBA 宏来执行此操作非常容易。
然后您可以编写 AppleScript 以在 Word 和 运行 宏中打开每个 RTF 文件,或者您可以使用主 Word VBA 完成所有处理。你可以用任何一种方式来做,但我会选择所有 VBA 路线,因为在我看来,它是一种比 AppleScript 更好的处理 MS 文档的语言。
当然,这需要您安装了MS Word。我仍然 运行 在我的所有 Mac 上安装 MS Office 2011,暂时不打算升级到 Office 2016。 Word 2011 运行良好。
祝你好运。