AppleScript 1.0
Volume Number: 9
Issue Number: 8
Column Tag: Scripting
AppleScript 1.0 Overview 
What is AppleScript and how does it work?
By Mark Minshull, Apple Computer, Inc.
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
About the author
Mark Minshull is the Engineering Manager of the Application Scripting Group at
Apple Computer, Inc. When he's not managing the group he secretly writes
AppleScript scripts. His E-mail addresses are minshull.m@ applelink. apple.com and
MINSHULL.M on AppleLink.
Although the AppleScript™ 1.0 Runtime and AppleScript Developer's Toolkit have
been shipping since the end of April, there's still seems to be some uncertainty about
just what AppleScript is. In this article I'll attempt to demystify AppleScript by
covering the following topics:
• What's the relationship of AppleScript to Apple events, other scripting systems,
and the Open Scripting Architecture?
• What does the AppleScript language look like?
• If you order AppleScript, just what do you get ?
This article does not cover the ins-and-outs of using AppleScript to script other
applications.
AppleScript and the Open Scripting Architecture
In May 1991, Apple announced the Open Scripting Architecture (OSA), a
standard for scripting technologies. The OSA specifies that for an application to be
“OSA-compliant” (i.e., scriptable) it must support Apple Events, understand Apple
Event object descriptors and, where possible, adhere to standard event suites as
defined in the Apple Events Registry.
About 25 major applications are now OSA-compliant (including Excel, FileMaker
Pro 2.0, Canvas) and many more will be shipping soon. Future Apple technologies -
such as speech recognition - will depend on scriptable applications to deliver services,
as well as be scriptable themselves.
AppleScript is system extension that allows users to control OSA-compliant
applications through written scripts. It extends and complements the Macintosh
graphical interface to offer automation, customization, and application integration
capabilities-offering users a high degree of control over the way in which they work
with their computers. It also extends the Open Scripting Architecture, as discussed
below.
New Kinds of Script-aware Applications
Prior to the release of AppleScript commercial developers could make their
applications scriptable as described in the previous section. With the shipment of
AppleScript and the OSA Application Programmers Interface (API), two new types of
script-aware applications are possible:
• Attachable applications are applications that are capable of triggering scripts.
For example, an attachable application might hand off a script to AppleScript for
execution in response to a specified condition, such as a change in data value, or
in response to a user action, such as entering text in a particular field. In this
way, applications that are attachable can act as front ends to other applications.
• Recordable applications are applications that are capable of sending Apple events
to themselves to report user actions to the Apple Event Manager. Currently
AppleScript can decompile these recorded Apple events into the form of a script.
Thus a recordable application allows users to create and compile scripts simply
by turning on a “ record” mode button within the AppleScript Script Editor.
(Recordable applications are implemented strictly through the Apple events 1.01
interface and are outside the scope of this article).
Introducing the OSA API
The OSA API provides an interface to developers to implement attachable
applications. For example, the AppleScript Script Editor calls the OSA API to load,
compile, run, and store scripts.
So what does this mean to you, the developer? Well... your application can now
easily load, run, and store scripts that reside in memory, documents, or as external
script files. Applications can direct these scripts at other scriptable applications - as
does the AppleScript Script Editor - or at themselves to provide a powerful macro
capability (provided your application is scriptable, of course!). An even more
ambitious use of the OSA API is to allow customization of your application's user
interface or behavior through the attachment of scripts to application objects.
I did say it was easy to load and run scripts...below is the complete code needed to
connect to a scripting component, load a script, and execute it:
/* 1 */
instance = OpenDefaultComponent('osas','gnrc');
desc.descriptorType = 'scpt';
desc.dataHandle = Get1Resource('scpt', 128);
OSALoad(instance,&desc,0,&scriptID);
ReleaseResource(desc.dataHandle);
OSAExecute(instance,scriptID,kOSANullScript,0,&result);
OSADispose(scriptID);
What About Other Scripting Systems?
The “back end” of the OSA API is defined such that any scripting system can plug
in under it. Therefore, your application doesn't have to worry about which scripting
systems is installed; OSA calls are dispatched to the appropriate OSA scripting
component based on a script's ID or signature.
So, for example, when the next version of Frontier ships you'll be able to attach
and execute Frontier scripts through the identical API that you can now use with
AppleScript. (A QuicKeys OSA scripting component is also under development).
There's a lot more provided by the OSA API than I can cover here; for example, it
defines a software architecture for building “tinkerable” applications via a
message-passing inheritance model. These and other details are described in the new
Inside Mac IAC Volume (an electronic copy is included with the AppleScript
Developer's Toolkit).
The AppleScript Scripting Component
Let's now take a look at the AppleScript language... Although the main design
focus of AppleScript is to control other applications via scripted Apple events, it is
itself a fully-featured programming language.
I'll first discuss AppleScript's control structures and subroutine definition
syntax, and give examples of working with strings and lists. I'll also cover methods
for writing modular scripts using script libraries, and touch on some advanced
language features. Throughout I'll arbitrarily dive into the details on some topics, but
this is by no means an exhaustive survey of the language.
AppleScript Control Structures and Subroutines
AppleScript has all of the standard control structures you know and love. Here's
an assortment of repeat loops:
-- 2
-- repeating "forever" or until a forced exit
repeat
if getX() > 10 then exit repeat
end repeat
-- repeating while a condition is true.
repeat while myString = "•
set myString to some word of anotherString
end repeat
-- repeating while a condition is false.
set backedUp to false
repeat until backedUp
set backedUp to doMyBackup()
end repeat
-- repeating with a looping variable
repeat with j from 10 to 1 by -2
set negSum to negSum + j
end repeat
-- repeating in a list
repeat with i in {1,12,4}
set x to item i of aList
end repeat