有没有办法在 AppleScript 中制作自定义库?

Is there a way to make a custom library in AppleScript?

所以我有一组函数,几乎在我编写的每个脚本中都会调用这些函数。有没有一种方法可以存储它们,我可以调用它们而无需在每个项目中粘贴 100 行代码?

有关详细信息,请查看 Creating a Library in the AppleScript Language Guide

作为快速 示例,在 macOS Catalina 中,我创建了 Script Libraries 文件夹在我的家[=85]的图书馆 文件夹 =] 文件夹,在终端:

mkdir -p "$HOME/Library/Script Libraries"

脚本编辑器中,我保存了一个名为我的Library.scpt脚本Script Libraries foldersLibrary folder我的 主页 文件夹,包含一个 处理程序 我经常使用 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()处理程序

示例 AppleScript 代码:

property myLibrary : script "My Library"

property theURL : " & ¬
    "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 的 handler ,然后显示 警报消息



Note: Libraries are supported in OS X Mavericks v10.9 (AppleScript 2.3) and later. To share properties and handlers between scripts in prior OS versions, use the load script command as described in Libraries using Load Script.

请注意 load script 方法 仍然可以与更新版本的 OS 一起使用。

更新:

另请注意,所问的问题是关于“有没有办法在 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"

使用此 post 的已接受答案中描述的方法与我第一次开始创建和使用 Script Libraries.

我终于意识到,并通过艰苦的方式学到了,这个方法有一个很大的陷阱

例如,假设您创建了 20 个不同的脚本文件,这些文件调用脚本库“My Library.scpt”文件中的处理程序。然后有一天,您决定编辑您的脚本库“My Library.scpt”文件,然后重新保存该文件。然后您会意识到使用“My Library.scpt”文件的 20 个不同的脚本文件并没有使用您的库文件的新的重新保存的编辑版本。它们仍然是 运行 以前版本的代码。这是因为使用这些库的脚本在编译时加载了它的代码。换句话说,对于你创建的那 20 个脚本文件,为了能够在“My Library.scpt”文件中使用更新的代码,你现在需要煞费苦心地梳理所有脚本文件,试图记住哪些文件调用了在你的“我的 Library.scpt” 文件上打开,重新编译,并再次保存每一个。相信我……这是一场噩梦!

解决他的问题的方法是让您的脚本文件在每次运行时加载脚本库文件。这确保使用“My Library.scpt”库文件的脚本正在使用它的最新版本。

为此,您必须将已接受答案中的代码更改为……

property myLibrary : script "My Library"

property theURL : " & ¬
    "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 : " & ¬
    "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!"