2014-06-17 28 views
0

片段1A在AppleScript中,如何从其他AppleScript调用/重用子例程?

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.

参考:“使用说明”(指通过搜索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

snippet 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,但得到了一个错误运行时,失败;

其次要在片段1A removeText()更换removeText(searchText, sourceText)(获得片段1D),并远销它来代替/Users/henry/Library/Script\ Libraries/text.app但在运行时得到一个错误,失败。

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

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

回答

2

我已经在MacScripter上写了相当广泛的关于脚本库的教程。使用正确的语法是:

use theScriptLibrary : script "text" 

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

你需要解决的处理程序,以一个特定的脚本对象,除非你使用一个脚本定义文件。当您不使用脚本定义文件时,该处理程序将被调用到当前顶层脚本对象,并且在父母链中没有removeText()处理程序。这在AppleScript语言指南中的确没有讨论过。

Here's my tutorial