所以我有一组函数,我几乎在我编写的每个脚本中都会调用这些函数。有没有办法存储它们,我可以调用它们而无需在每个项目中粘贴 100 行代码?
答案 0 :(得分:2)
有关详细信息,请查看 Creating a Library 中的 AppleScript Language Guide。
作为一个快速示例,在macOS Catalina中,我在脚本库文件夹中创建了脚本库文件夹 >图书馆 文件夹在我的主页 文件夹,在终端:
mkdir -p "$HOME/Library/Script Libraries"
在脚本编辑器中,我在脚本库中保存了一个名为My Library.scpt的脚本 >文件夹在我的主页文件夹的库文件夹中,包含一个处理程序< /em> 我经常与 Safari 一起使用:
示例 AppleScript 代码:
to waitForSafariPageToFinishLoading()
-- # Wait for page to finish loading in Safari.
-- # This works in **macOS Catalina** and
-- # macOS Big Sur and may need adjusting for
-- # other versions of macOS.
tell application "System Events" to repeat until ¬
exists (buttons of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end waitForSafariPageToFinishLoading
然后我以这种方式在另一个脚本中使用了waitForSafariPageToFinishLoading()
handler:
示例 AppleScript 代码:
property myLibrary : script "My Library"
property theURL : "https://stackoverflow.com/questions/68581982/" & ¬
"is-there-a-way-to-make-a-custom-library-in-applescript"
tell application "Safari" to ¬
make new document with properties {URL:theURL}
myLibrary's waitForSafariPageToFinishLoading()
display alert "The web page has finished loading!"
在示例 AppleScript 代码(如上图所示)中,Safari 等待网页 使用 My Library.scpt 中的 处理程序 完成加载,然后显示警报消息。
注意:库在 OS X Mavericks v10.9 (AppleScript 2.3) 及更高版本中受支持。要在先前操作系统版本中的脚本之间共享属性和处理程序,请使用 load script 中所述的 Libraries using Load Script 命令。
请注意,load script
方法仍可用于较新版本的操作系统。
更新:
另请注意,所问的问题是关于“有没有办法在 AppleScript 中创建自定义库?”,不是如何使用它们以及为什么我说“作为一个快速的示例”。但是,也就是说,对于实际的长期使用,就另一个答案中提到的这一点而言,我会使用例如:
set myLibrary to ¬
load script alias ¬
((path to library folder from user domain as text) & ¬
"Script Libraries:My Library.scpt")
代替:
property myLibrary : script "My Library"
答案 1 :(得分:2)
使用本文已接受的答案中描述的方法,与我第一次开始创建和使用脚本库时使用的方法相同。
我最终意识到,并以艰难的方式学习,这种方法有一个很大的陷阱。
例如,假设您创建了 20 个不同的脚本文件,这些文件从您的脚本库“My Library.scpt”文件中调用处理程序。然后有一天您决定编辑您的脚本库“My Library.scpt”文件,然后重新保存该文件。然后您会意识到使用“My Library.scpt”文件的 20 个不同的脚本文件没有使用您的库文件的重新保存的新编辑版本。他们仍在运行以前版本的代码。这是因为使用这些库的脚本在编译时加载其代码。换句话说,为了能够使用“My Library.scpt”文件中的更新代码而创建的这 20 个脚本文件,现在需要精心梳理所有脚本文件,尝试记住哪些文件在您的“My Library.scpt”文件并再次打开、重新编译和保存。相信我……这是一场噩梦!
解决他的问题的方法是让您的脚本文件在每次运行时加载脚本库文件。这可确保使用“My Library.scpt”库文件的脚本使用的是最新版本。
要做到这一点,您必须从这里更改已接受答案中的代码……
property myLibrary : script "My Library"
property theURL : "https://stackoverflow.com/questions/68581982/" & ¬
"is-there-a-way-to-make-a-custom-library-in-applescript"
tell application "Safari" to ¬
make new document with properties {URL:theURL}
tell myLibrary to waitForSafariPageToFinishLoading()
display alert "The web page has finished loading!"
到这个……
set myLibrary to load script alias ((path to home folder as text) & ¬
"Library:Script Libraries:My Library.scpt")
property theURL : "https://stackoverflow.com/questions/68581982/" & ¬
"is-there-a-way-to-make-a-custom-library-in-applescript"
tell application "Safari" to ¬
make new document with properties {URL:theURL}
tell myLibrary to waitForSafariPageToFinishLoading()
activate
display alert "The web page has finished loading!"