So I have a set of functions that I call in almost every script I write. Is there a way to store them that I can call them without pasting 100 lines of code in every project?
So I have a set of functions that I call in almost every script I write. Is there a way to store them that I can call them without pasting 100 lines of code in every project?
For in depth details, have a look at Creating a Library in the AppleScript Language Guide.
As a quick example, in macOS Catalina, I created the Script Libraries folder in the Library folder of my Home folder, in Terminal:
mkdir -p "$HOME/Library/Script Libraries"
In Script Editor, I saved a script named My Library.scpt in the Script Libraries folders in the Library folder of my Home folder, containing a handler I use often with Safari:
Example AppleScript code:
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
I then used the waitForSafariPageToFinishLoading() handler in another script in this manner:
Example AppleScript code:
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!"
In the example AppleScript code, shown directly above, Safari waits for the web page to finish loading, using a handler from My Library.scpt, and then displays an alert message.
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.
Note that the load script method can still be used with newer versions of the OS.
Update:
Also note that the question asked was about "Is there a way to make a custom library in AppleScript?", not how to use them and why I said "As a quick example". However, that said, for practical long term usage, to the point mentioned in the other answer, I'd use e.g.:
set myLibrary to ¬
load script alias ¬
((path to library folder from user domain as text) & ¬
"Script Libraries:My Library.scpt")
Instead of:
property myLibrary : script "My Library"
Using the method as described in the accepted answer to this post, is the same method I used when I first started creating and using Script Libraries.
I eventually realized, and learned the hard way, there is a big pitfall with this method.
For example, let’s say that you have created 20 different script files that call on Handlers from your Script Library “My Library.scpt” file. Then one day you decide to edit your Script Library “My Library.scpt” file then re-save the file. You then come to realize that the 20 different script files that use the “My Library.scpt” file are not using the new re-saved edited version of your Library file. They are still running the code from the previous version. This is because the scripts that use those libraries load its code at compile time. In other words, for those 20 script files you created to be able to use the updated code in the “My Library.scpt” file, you now need to painstakingly comb through all of your script files, trying to remember which files called on your “My Library.scpt” file and open, re-compile, and save each one again. Trust me… it’s a nightmare!
The work around for his problem is to have your script files load the Script Library file every time it runs. This ensures that he scripts which are using the “My Library.scpt” library file are using it’s latest version.
To do this, you would have to change the code in the Accepted Answer from this…
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!"
to this…
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!"