这基本上是我从没学过脚本/编程以来的第一个 AppleScript ;) 但我能够让它工作,但它似乎不是最佳的,因为它偶尔会崩溃,并且我期望当所有这些单个命令都放在一个桶中时会有更好的性能(速度方面): 我想合并几个“将文本设置为替换文本”行。但我无法让它发挥作用。
Run AppleScript:
on replaceText(find, replace, textString)
set prevTIDs to AppleScript‘s text item delimiters
set AppleScript‘s text item delimiters to find
set textString to text items of textString
set AppleScript‘s text item delimiters to replace
set textString to „“ & textString
set AppleScript‘s text item delimiters to prevTIDs
return textString
end replaceText
on run {input}
set theText to replaceText(„ABC“, „CBA“, input as string)
end run
Run AppleScript:
on replaceText(find, replace, textString)
set prevTIDs to AppleScript‘s text item delimiters
set AppleScript‘s text item delimiters to find
set textString to text items of textString
set AppleScript‘s text item delimiters to replace
set textString to „“ & textString
set AppleScript‘s text item delimiters to prevTIDs
return textString
end replaceText
on run {input}
set theText to replaceText(" ", "", input as string)
end run
Run AppleScript:
on replaceText(find, replace, textString)
set prevTIDs to AppleScript‘s text item delimiters
set AppleScript‘s text item delimiters to find
set textString to text items of textString
set AppleScript‘s text item delimiters to replace
set textString to „“ & textString
set AppleScript‘s text item delimiters to prevTIDs
return textString
end replaceText
on run {input}
set theText to replaceText(„ABC“, „CBA“, input as string)
set theText to replaceText(" ", "", input as string)
end run
我尝试将所有“设置文本替换文本”放在一起,也尝试使用“else”或中间的延迟,但它根本不起作用。
我的脚本的主要目的如下:
首先,这无论如何都行不通,因为双引号必须是
character id 34
("
) 和单引号 character id 39
('
)。
该错误是拼写错误。要将第二个查找/替换调用应用于第一个查找/替换调用的结果,您必须在第二行中将
input as string
替换为 theText
。我重命名了变量以使其清晰。
并且仅当在应用程序
AppleScript's
块内更改文本项分隔符时才需要 tell
。
on replaceText(find, replace, textString)
set prevTIDs to text item delimiters
set text item delimiters to find
set textItems to text items of textString
set text item delimiters to replace
set textString to textItems as text
set text item delimiters to prevTIDs
return textString
end replaceText
on run {input}
set firstReplace to replaceText("ABC", "CBA", input as string)
set secondReplace to replaceText(space, "", firstReplace)
return secondReplace
end run