AppleScript Libraries provide a plugin architecture for extending the power and abilities of AppleScript. AppleScript Libraries are user-created script files and bundles, composed in AppleScript or AppleScript/Objective-C, which contain specialized commands that can be referenced in scripts to provide extra or “missing” functionality.
As mentioned above, AppleScript libraries written using AppleScript/Objective-C, provide access to standard Cocoa classes and methods. With “use statements”, the number of available Cocoa methods increases dramatically, enabling script libraries to access specified frameworks, such as Foundation, AppKit, or WebKit.
Installing Script Libraries
AppleScript Libraries are installed and made available for use by AppleScript scripts, by placing their files within a folder titled: “Script Libraries”. A Script Libraries folder can reside in a variety of locations, depending on how you want to define their availability on the computer.
The illustration below outlines four possible locations for a Script Libraries folder:
Simple Script Library
While AppleScript Libraries can vary in the complexity of their design and implementation, their purpose is the same: to provide easy access in scripts to collections of specialized commands and handlers.
An AppleScript Library’s source code may be written in standard AppleScript, or it may employ AppleScript/Objective-C to access methods and classes from standard Cocoa frameworks such as Foundation and AppKit, and even import specialized frameworks such as MapKit, EventKit, or WebKit.
Regardless of the kind of AppleScript Library you create, the process of creating the library is similar for every type:
Script code containing routines and handlers, is placed into a script file or bundle, which is then saved and placed into the Script Libraries folder in your local Library folder to become available for use by other scripts.
Follow the steps detailed below to create and use a simple AppleScript Library.
Create and Save a Script File
To create a simple AppleScript Library, written in AppleScript, open a new plain AppleScript script document. Save the new document as a standard script file by choosing “Compiled Script” from the Format popup menu in the Save dialog (see below):
For this example, name the script: Simple AppleScript Library. By default, the saved script file will have a name extension of “scpt”.
|Simple AppleScript Library|
|02||set thisPath to the POSIX path of aReference|
|03||if thisPath ends with "/" then -- file bundle|
|04||set thisPath to text 1 thru -2 of thisPath|
|06||set AppleScript's text item delimiters to "/"|
|07||set fileName to the last text item of thisPath|
|08||set AppleScript's text item delimiters to ""|
|09||set the reversedFileName to ¬|
|10||(the reverse of every character of fileName) as text|
|11||set x to the offset of "." in the reversedFileName|
|12||if x is 0 then|
|15||set baseName to text 1 thru -(x + 1) of fileName|
|Call AppleScript Library|
|01||tell script "Simple AppleScript Library"|
In some situations, the functions of the native AppleScript language and its standard set of scripting additions, may be lacking in the ability to perform a certain task, such as transforming the case of text.
Fortunately, AppleScript/Objective-C (introduced in OS X v10.6) provides access to the thousands of functions that make up OS X’s powerful Cocoa frameworks.
|02||set aPath to the POSIX path of aReference|
|03||set cocoaString to ¬|
|04||current application's NSString's stringWithString:aPath|
|05||set fileName to cocoaString's lastPathComponent()|
|06||set baseName to fileName's stringByDeletingPathExtension()|
|07||return baseName as text|
|Call AppleScript Library|
|01||tell script "ASOC Library"|
To make it easier to remember and implement library commands, AppleScript Libraries can be written to publish their own scripting terminology, viewable to users using the AppleScript Editor’s dictionary viewer. The Alert Utilities script library is an example of the use of terminology.
This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.