Name

Libraries

Synopsis

A library is a type of script object that represents a group of methods that are stored in a compiled script. Just define several useful functions in Script Editor then save them with a meaningful name, like WebLib. You can then call these functions from other scripts by using the load script osax:

set wlib to (load script "macintosh hd:ASlibraries:WebLib")

Let’s say the WebLib library has a method called httpGet. The wlib variable now refers to the WebLib script object, so you could call this method with the code:

wlib's httpGet(  )

You could also save WebLib as a stay-open applet. As long as the library applet is running (with few performance implications as a small applet will only use several hundred bytes of memory), you could call its functions from another applet with code as spare as:

tell app "WebLib" to httpGet("my.yahoo.com")

AppleScript will be able to “find” the WebLib applet a lot easier when you try to compile the latter code fragment if WebLib is already a running process.

There are several good reasons why you might want to have code libraries running on your machine, with a principal one being code reuse. Let’s say you have five separate applets that each performs various mathematical calculations. Instead of defining the same math subroutines inside of each applet, you can keep these subroutines stored inside a single library, from which the various applets can use these methods after loading the library themselves. It is inefficient to include ...

Get AppleScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.