Script Libraries

ScriptLibraryIcon-256

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.

Greater Scope

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:

deploymentscenarios-624

 1  Home Library folder - To enable the use of an AppleScript Library by the current user account, place AppleScript Library files inside a Script Libraries folder located in the user’s Library folder.

 2  Computer Library folder - To enable the use of an AppleScript Library by all user accounts, place AppleScript Library files inside a Script Libraries folder located in the computer’s Library folder.

 3  Applet/Script Bundle Resources folder - To enable the use and distribution of an AppleScript Library by a script applet, or script bundle ("scptd"), place AppleScript Library files inside a Script Libraries folder located in the applet’s or script bundle’s Resources folder. NOTE: Applet and script bundle libraries are only available to scripts running inside the applet’s process, or the application running the script bundle.

 4  Application Resources folder - To enable the use and distribution of an AppleScript Library by an application that executes AppleScript scripts, place AppleScript Library files inside a Script Libraries folder located in the application’s Resources folder. NOTE: Application-bundle libraries are only available to scripts running inside that application’s process.

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):

script-format

For this example, name the script: Simple AppleScript Library. By default, the saved script file will have a name extension of “scpt”.

basenameFromFileReference(choose file) on basenameFromFileReference(aReference) set thisPath to the POSIX path of aReference if thisPath ends with "/" then -- file bundle set thisPath to text 1 thru -2 of thisPath end if set AppleScript's text item delimiters to "/" set fileName to the last text item of thisPath set AppleScript's text item delimiters to "" set the reversedFileName to ¬ (the reverse of every character of fileName) as text set x to the offset of "." in the reversedFileName if x is 0 then return fileName else set baseName to text 1 thru -(x + 1) of fileName return baseName end if end baseNameFrom
on basenameFromFileReference(aReference) set thisPath to the POSIX path of aReference if thisPath ends with "/" then -- file bundle set thisPath to text 1 thru -2 of thisPath end if set AppleScript's text item delimiters to "/" set fileName to the last text item of thisPath set AppleScript's text item delimiters to "" set the reversedFileName to ¬ (the reverse of every character of fileName) as text set x to the offset of "." in the reversedFileName if x is 0 then return fileName else set baseName to text 1 thru -(x + 1) of fileName return baseName end if end basenameFromFileReference
tell script "Simple AppleScript Library" basenameFromFileReference(choose file) end tell

AppleScriptObj-C Libraries

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.

use AppleScript version "2.4" -- Yosemite (10.10) or later use framework "Foundation" use scripting additions basenameFromFileReference(choose file) on basenameFromFileReference(aReference) set aPath to the POSIX path of aReference set cocoaString to current application's NSString's stringWithString:aPath set fileName to cocoaString's lastPathComponent() set baseName to fileName's stringByDeletingPathExtension() return baseName as text end basenameFromFileReference
on basenameFromFileReference(aReference) set aPath to the POSIX path of aReference set cocoaString to current application's NSString's stringWithString:aPath set fileName to cocoaString's lastPathComponent() set baseName to fileName's stringByDeletingPathExtension() return baseName as text end basenameFromFileReference
tell script "ASOC Library" basenameFromFileReference(choose file) end tell

Terminology

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.

Resources

And here is a link to Shane Stanley’s free AppleScriptObj-C libraries. Thank you Shane! FileManager

Photos UtilitiesLorem Ipsum

UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER