Last edited · 14 revisions  

 


Script Libraries

updated 2018-10-16 17:31 GMT-5


Definition

A top-level script object saved in a Script Libraries folder becomes a script library usable by other scripts. Libraries let you share and reuse handlers, reorganize large scripts into a set of smaller libraries that are easier to manage, and build richer, higher-level functionality out of simpler libraries. 1

A Script Library is a normal script file (.scpt) or script bundle (.scptd) that contains one or more handlers or functions, that is stored in one of the supported locations (see below). These files may be created and edited using the Apple Script Editor or Script Debugger by Late Night Software.


Script Library Location

Script Libraries must be stored in one of the following folders on your Mac. You should create the folder if it doesn’t already exist:

Location    Available To
~/Library/Script Libraries/    Currently Logged In User
/Library/Script Libraries/ All Users of the Computer
/Network/Library/Script Libraries/ Multiple computers on a network
/Resources//Library/Script Libraries/
(folder inside a script or app bundle.)
Script bundle (.scptd) or App bundle (.app)
/System/Library/Script Libraries/ All Users
(These are libraries provided by macOS.)

How To Use (Call) a Handler in a Script Library

While you can use any of the methods used to reference a Script object in other scripts, the most common and useful method is to first create reference variable in the script that uses the Script Library, using this syntax:

use <ScriptRefVar> : script "<Script Lib File Name>"
use scripting additions

where
<ScriptRef> is the AppleScript Variable that you will use to reference the Script Library
<Script Lib File Name> is the name of the Script Library file without its extension.
use scripting additions is needed if you plan to use any of the commands provided by any scripting additions (.osax) that you have installed, including the built-in scripting additions provided by Apple. This is also required if you have any other use statements in your script.


For example:

use myLib : script "JMichaelTX Lib"
use scripting additions


Then when you want to call a handler/function in this Script Library, you would use one of these forms:

set x to myLib's someHandlerName()
-- OR --
set x to someHandlerName() of myLib


For additional information about writing script libraries, see Creating a Library 2 in AppleScript Language Guide and Libraries 3 in JavaScript for Automation Release Notes


Footnotes

1.  Apple. (2016-01-25). _Script Libraries -- AppleScript Language Guide_

2.  Apple. (2016-01-25). _Creating a Library -- AppleScript Language Guide_

3.  Apple. (2015-09-16). _Libraries -- JavaScript for Automation Release Notes_