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

时间:2014-06-17 11:37:53

标签: macos 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中很方便,并希望通过调用它的名称来重用它。我用谷歌搜索了howto。然后我将代码段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.

参考:“使用声明”(请参阅​​https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html中搜索use script "Happy Fun Ball"找到的部分)

所以我回到google,发现有人建议我将片段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 )并将其导出为替换/Users/henry/Library/Script\ Libraries/text.app,但在运行时出错,失败;

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

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

这是一个详细的问题描述。请帮助,非常感谢!

1 个答案:

答案 0 :(得分:2)

我已经在MacScripter上撰写了一篇关于脚本库的大量教程。要使用的正确语法是:

use theScriptLibrary : script "text"

theScriptLibrary's removeText("hellow", "w")

除非使用脚本定义文件,否则需要将处理程序寻址到特定的脚本对象。当您不使用脚本定义文件时,处理程序将被调用到当前顶级脚本对象,并且在父级链中没有removeText()处理程序。这确实没有在AppleScript语言指南中讨论过。

Here's my tutorial