AppleScript is a scripting language devised by Apple Inc., and built into Mac OS. More generally, "AppleScript" is the word used to designate the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface.
AppleScript was released in October 1993 as part of System 7.1.1 (System 7 Pro, the first major upgrade to System 7). QuarkXPress (ver. 3.2) was one of the first major software applications that supported AppleScript, and as a result, AppleScript was widely adopted within the publishing segment of the Apple market and remained popular after QuarkXpress (and other applications) were ported to Microsoft Windows.
The move to Mac OS X and its Cocoa frameworks has seen AppleScript come into its own. Cocoa applications offer basic scriptability with little effort on the part of the developer. AppleScript Studio, released with Mac OS X 10.2, allows users to build entire applications using AppleScript and Cocoa objects.
For example, an appleScript might open a photo in a photo-editing application; reduce its resolution; add a border; add a photo credit; export a Web-ready copy; write a Web link for the photo into a text editor then move on to the next photo in the set, and so on through hundreds or thousands of photos, eventually creating a Web-ready photo gallery, which the script uses an FTP client to upload to the user's Web site. For the user, hundreds of steps in multiple applications with potentially thousands of documents have been reduced to one: run a script. A large complex script could be developed to run only once, while other scripts are used again and again, further leveraging the development time to make it a trivial consideration.
An important AppleScript concept is that scripts drive applications in a fundamentally different way from the way users interact with them. Users manipulate the application's user interface, pulling down menus and clicking buttons; AppleScript scripts can get and set values and invoke actions exposed by the application's internal object model. So, for example, rather than simulating keystrokes to enter text into fields in a database application an AppleScript script would typically use commands that directly set the values of the desired fields of the record, possibly without the application even displaying the record being updated. AppleScript also has the ability to control non-scriptable applications through Graphical User Interface (GUI) scripting, which allows AppleScripts to select menu items, click buttons, enter text into text fields, and generally control the interfaces of most Mac OS X 10.x applications to handle operations not available through the scripting interface.
Nearly all applications share a number of commands (open a file; close a document; print; save; quit; etc.). Each scriptable application contains the AppleScript terminology it understands in the form of an Apple Event dictionary, which AppleScript uses to determine the valid commands that can be issued in each application's context. The Apple Event dictionary is distributed as part of the application, and is evaluated by AppleScript at runtime and when scripts are compiled. For many applications that support plug-ins, the plug-ins themselves may be AppleScriptable and would include an Apple Event dictionary that would be merged with the Application's dictionary at run time.
In AppleScript, the classic Hello World! program is written like this:
say "Hello World!"
When run, the above program causes the computer to say the phrase "Hello World!" in its default voice.
AppleScript support is provided by many Mac OS X applications, from both Apple and third parties. Scriptable applications include Apple's Finder, Safari, iPhoto, and iTunes, as well as Adobe Illustrator and Photoshop, Bare Bones BBEdit and TextWrangler, Microsoft Word and Excel, VMwareFusion and many more.
Some applications send Apple Events when their menus are clicked, or other user interface actions are taken. These applications are said to be "recordable" because Script Editor can record their user interface Apple Events, creating an AppleScript macro line-by-line as you work.
Note that while a script is recording, you can't run it. However, you can do everything else: move the insertion point; add, edit, or remove code; even compile your script. The recordable application collaborates in a sense with your AppleScript writing, rather than replacing it. This approach typically yields more practical results than attempting to record an entire script from start to finish.
Only a small minority of scriptable applications is recordable. Examples include Finder and BBEdit. Studying the code that is produced by a recordable application as you work can aid in learning how to write scripts for that application.
With each version of Mac OS X it has become simpler for developers to implement AppleScript within their applications using the Cocoa API. In the Mac OS events are handled by applications, but, in the Cocoa API, events are decoded into a "high level" command by the
NSApplication object, and the messages dispatched directly to the correct object. All Cocoa API applications are "factored" by default; the developer doesn't write any of the event handling code (normally) and writes only the "work methods" that those events will call.
Another major advantage is that Cocoa objects are presented to the outside world (other applications and even machines) in a standardized format that anyone can examine directly. Under Cocoa, AppleScript is much "thinner"; the script engine decodes the script, translates object names from human-readable to their internal format, and then calls those methods on the target application directly.
The heart of the AppleScript language is the use of terms that act as nouns and verbs that can be combined. For example, rather than a different verb to print a page, document or range of pages (printPage, printDocument, printRange) appleScript uses a single "print" verb which can be combined with an object, such as a page, a document or a range of pages.
Generally, AEOM defines a number of objects--like "document" or "paragraph"--and corresponding actions--like "cut" and "close". The system also defines ways to refer to properties of objects, so one can refer to the "third paragraph of the document 'Good Day'", or the "color of the last word of the front window". AEOM uses an application dictionary to associate the Apple Events with human-readable terms, allowing the translation back and forth between human-readable AppleScript and bytecode Apple Events. To discover what elements of a program are scriptable, dictionaries for supported applications may be viewed. (In the Xcode and Script Editor applications, this is under File → Open Dictionary.)
To designate which application is meant to be the target of such a message, AppleScript uses a "tell" construct:
Alternatively, the tell may be expressed in one line by using an infinitive:
For events in the "Core Suite" (activate, open, reopen, close, print, and quit), the application may be supplied as the direct object to transitive commands:
The concept of an object hierarchy can be expressed using nested blocks:
AppleScript includes syntax for ordinal counting, "the first paragraph", as well as cardinal, "paragraph one". Likewise, the numbers themselves can be referred to as text or numerically, "five", "fifth" and "5" are all supported, they are called synonyms. Also, to add to the English-likeness, the word "the" can legally be used anywhere in the script in order to enhance readability: it has no effect on the functionality of the script.
Every script has a run handler, but declaring it is optional. If the run handler is undeclared, commands in the script's top level, outside of any other handler, are considered to be an implicit run handler.
If the script has an open handler:
then the applet becomes a droplet: when the user drags and drops filesystem items onto the script, it will be launched and the open handler will be invoked and a list of aliases will be set with the open handler's variable name. A droplet can also run as an applet: when double-clicked, the run handler is called. Adding a run handler with a choose file command to your droplet enables a second pathway for the user to provide the script with a list of filesystem items.
If the script has an idle handler:
then the applet will pause for a specified number of seconds (20 in this example) and will execute the script within the idle handler again, and again ...
(Note: "on" is a synonym for "to" when starting a handler; for example, "on run" instead of "to run".)
An important aspect of the AppleScript implementation was the Open Scripting Architecture (OSA). Apple provided OSA for third-party scripting/automation products such as QuicKeys and UserLand Frontier, to function on an equal status with AppleScript. AppleScript was implemented as a scripting component, and the basic specs for interfacing such components to the OSA were public, allowing other developers to add their own scripting components to the system. Public client APIs for loading, saving and compiling scripts would work the same for all such components, which also meant that applets and droplets could hold scripts in any of those scripting languages.
One of the most interesting features of the OSA are "scripting additions", or OSAX for Open Scripting Architecture eXtension, which were based on Hypercard's External Commands. Scripting Additions allow programmers to extend the function of AppleScript. Commands included as Scripting Additions are available system wide, and are not dependent on an application. Mac OS X includes a collection of scripting additions referred to as Standard Additions, which extends the function of AppleScript with a variety of new commands, including user interaction dialogs, reading and writing files, file system commands, date functions, text and math operations.