在 AppleScript 中,如何调用/重用另一个 AppleScript 的子程序?

在 AppleScript 中,如何调用/重用另一个 AppleScript 的子程序?

片段 1a

on removeText(searchText, sourceText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to searchText
set sourceText to text items of sourceText

set text item delimiters of AppleScript to ""
set sourceText to "" & sourceText
set text item delimiters of AppleScript to prevTIDs

return sourceText
end removeText

片段 2a

on removeText(searchText, sourceText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to searchText
set sourceText to text items of sourceText

set text item delimiters of AppleScript to ""
set sourceText to "" & sourceText
set text item delimiters of AppleScript to prevTIDs

return sourceText
end removeText

set theSentence to "I love Windows and I will always love Windows."
set theSentence to removeText("Windows", theSentence)

我发现这个子程序(代码片段 1a)在代码片段 2a 中很方便,我想通过调用它的名字来重用它。我在 Google 上搜索了操作方法。然后我将代码片段 1a 保存为,/Users/henry/Library/Script\ Libraries/text.scpt并在代码片段 2a 中替换了

片段 1b

on removeText(searchText, sourceText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to searchText
set sourceText to text items of sourceText

set text item delimiters of AppleScript to ""
set sourceText to "" & sourceText
set text item delimiters of AppleScript to prevTIDs

return sourceText
end removeText

片段 3

use script "text"

并得到片段 2b然后我运行了代码片段 2b,但出现错误«script» doesn’t understand the “removeText” message.

参考:“use 语句”(请参阅use script "Happy Fun Ball"​​在https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html

所以我回到谷歌并发现有人建议我将代码片段 1a 保存为“脚本应用程序”。

参考2:https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_about_handlers.html

在这个例子中,

片段 4

tell application "NonStayOpen"
launch
stringTest("Some example text.")
end tell

因此我将代码片段 1a 导出为/Users/henry/Library/Script\ Libraries/text.app并编写了代码片段 2c

片段 2c

tell application "text"
launch
set theSentence to "I love Windows and I will always love Windows."
set theSentence to removeText("Windows", theSentence)
end tell

然后我运行它并收到错误{} doesn't match the parameters {searchText, sourceText} for removeText.

之后,我首先尝试附加removeText(searchText, sourceText)到代码片段 1a(获取片段 1c)并导出到replace/Users/henry/Library/Script\ Libraries/text.app但运行时出错,失败;

其次在代码片段 1a 中removeText(searchText, sourceText)替换为removeText()片段 1d)并导出进行替换/Users/henry/Library/Script\ Libraries/text.app,但运行时出错,失败。

在代码片段 2a 中,如何从另一个 AppleScript 或“脚本应用程序”调用/重用子程序(代码片段 1a)(参见参考资料 2)?

答案1

这是我在许多 AppleScript 中使用的:

set iTunesFunctions to load script "Users:jim:Library:iTunes:Scripts:iTunes Functions.scpt" as alias
CaseCorrection() of iTunesFunctions

如果你将包含 snippet1a 的文件保存到“MyFunctions.scpt”中,你的代码将如下所示:

set MyFunctions to load script "MyFunctions.scpt" as alias
snippet1a(searchText, sourceText) of MyFunctions

相关内容