AppleScript:多个“将文本设置为替换文本”

问题描述 投票:0回答:1

这基本上是我从没学过脚本/编程以来的第一个 AppleScript ;) 但我能够让它工作,但它似乎不是最佳的,因为它偶尔会崩溃,并且我期望当所有这些单个命令都放在一个桶中时会有更好的性能(速度方面): 我想合并几个“将文本设置为替换文本”行。但我无法让它发挥作用。

This works (even though it's not super clean/easy optimised)

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

This doesn't work

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”或中间的延迟,但它根本不起作用。

我的脚本的主要目的如下:

  1. 从任何程序中获取选定的文本(文件路径)
  2. 相应修改所选文本(Mac <-> Windows 路径结构)
  3. 将文本复制到剪贴板并使用Finder打开文件夹/文件(Mac OS)
applescript
1个回答
0
投票

首先,这无论如何都行不通,因为双引号必须是

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
© www.soinside.com 2019 - 2024. All rights reserved.