I have a bunch of code I want to to use in several different applescripts so I would like to put it into 开发者_StackOverflow中文版it's own applescript that I can reference from other applescripts. Basically I want to do an include. How is this done in applescript?
Here is the best way I have found to accomplish this. You can call a function on another script like so:
script a.scpt
set myScript to load script "b.scpt"
set foo to myScript's theTest()
script b.scpt
on theTest()
return true
end theTest
As you can see you can call functions within b.scpt from a.scpt by calling the function name myScript's theTest().
You can put all the handlers and scripts that you want to reference inside a Script Library (for example: my_math_lib.scpt and my_string_lib.scpt). Then, save this file in a Script Libraries folder on your computer.
Depending on how you want to define the availability of this library, you use a different folder:
- Place the
my_math_lib.scptandmy_string_lib.scptfiles inside the/Library/Script Librariesfolder to make them available for all users on the computer. - Place them in the
~/Library/Script Librariesfolder to make them available for a specific user only.
You can then use all the handlers in those libraries as follows:
property math_lib : script "my_math_lib"
property string_lib : script "my_string_lib"
math_lib's do_this()
string_lib's do_that()
加载中,请稍侯......
精彩评论