by Cameron Laird and Kathryn Soraiz
Here are three reasons why it's a good time to do so:
- Marketplace changes — mostly impelled by problems with
the insecurity of Windows — have revived interest in
Macintoshes for both desktops and servers.
- It's easy and rewarding to pick up AppleScript basics.
- New books by Matt Neuburg and Bert Altenburg on the
subject makes sense of the subject for typical UnixReview.com readers.
We need to qualify that last reason — Neuburg's
The Definitive Guide and Altenburg's
for Absolute Starters were new when we began reading them last
winter, but events delayed a few of the "Regular Expressions" columns we
planned, and we're only now catching up. To explain why we think the subject
remains timely, let's focus first on the language itself.
This is a scripting language
AppleScript is a real scripting language. Each
month, this column focuses on the general topic of highly productive
languages; with a degree of hesitation, we often abbreviate
those as "scripting" languages. The hesitation results from
the ambiguity and confusion in the label,
Whatever its fuzziness in general, though, no one argues
about AppleScript — it has all the qualities anyone expects
from a scripting language. First announced by Apple
in May 1992 as a descendant of the HyperCard project,
AppleScript is interactive, "English-like", and far
stronger in its ability to co-operate with other
applications than in its intrinsic computational
ability. AppleScript doesn't know about regular
expressions or trigonometric functions, its performance
is no better than mediocre, and its syntax is ambiguous,
but it can leverage thousands of external programs precisely
This column often delves into language-theoretic issues — Is Ruby's
block structure an advantage or disadvantage in maintenance? How long
does it take to accustom oneself to Python's significant white space?
How will object orientation appear in Perl 6? With AppleScript, there's
little to say — the language itself in its syntax and semantics
is not particularly powerful or innovative. It mimics English; in fact,
one of its most difficult technical achievements was discarded a few
years ago. Several releases could be remapped so that they resembled
human languages beside English: French, German, and so on. Apple determined
that only a small minority of users ever took advantage of this provocative
feature and abandoned it a few releases back.
AppleScript is a modest language then, aiming mostly to stay
out of the developer's way. Beyond its precociously clean
query-based object orientation, which offers such interesting
advantages as the ability to broadcast messages, though,
its one great strength
is that it knows MacOS — that is, it hooks into the operating
system (OS) "all the way down", so that it is uniquely capable of
accessing the capabilities of the OS.
Many books start with AppleScript illustrations that are, frankly,
done at least as easily in almost any other scripting language. Feel
free to experiment with AppleScript as a general-purpose language. For
our introduction, though, we want to emphasize the language's unique
advantages. Consider this example — while printing out "Hello,
world" is a traditional starting point, AppleScript can make this or
other human language phrases audible. From a MacOS X command
# osascript << HERE
? say "Hello, world."
Hear that? As with most high-level languages, you can also
create a source file, such as
hello.scpt, with contents
say "Hello, world."
In the AppleScript community at large, it's still far more common
to use the AppleScript application than the command-line
osascript, which only became available with the Unix-based
rewrite of MacOS X. To take advantage of Script Editor, navigate to
"Applications/AppleScript/Script Editor", enter:
say "Hello, world."
in the text field, and push the Run button (see Figure 1).
Study the work of others
Once you're off to the right start with AppleScript, you'll find abundant
sample scripts you can study — in the books mentioned above, in
the Scripts subfolder standard with MacOS, and in numerous Web sites.
The AppleScript community has a rich tradition of sharing its ideas
and creations. At the same time, it diverges from what UnixReview.com
readers expect with most other Unix technologies in at least a couple
of ways. For example, most books on AppleScript are oriented heavily
to specific applications; they typically explore subjects like scripting
Quark Express, or automation of a particular video editor. AppleScript
for Absolute Beginners and AppleScript: The Definitive Guide
are unique, as best we can tell, in focusing on the language
and counting on the reader to understand how to apply its abstractions
in particular instances.
A related fact you should know is that AppleScripters are, for the
most part, more enthusiastic than expert as software engineers. They're
"domain experts" — graphics designers, sound technicians, scientists,
and office workers — who just want computers to help them do their
real jobs. They make plenty of mistakes from the perspective of full-time
The AppleScripters have plenty to teach, though, about all the good
works a little AppleScript glue can assemble. Even "Unscriptable Applications",
Chapter 22 in Neuburg's book, often are scriptable, with a
little research. One of us (Cameron) often is involved in network programming
and needs to toggle File Sharing, the Firewall, various proxies, VPN
connections, and more, in different combinations. While we haven't figured
out command-line interfaces that control all these variables, AppleEvents
and the Accessibility Interface give enough hooks to simplify their
management enormously. Rather than pointing and clicking our way through
multiple panels, we have automations that make the right choices without
our direct involvement.
Your greatest return is likelier to be in thinking of AppleScript
as a way to get to other applications, than as a stand-alone
language. There are three qualifications to this you should
- You can use AppleScript with other languages. Neuburg treats this, a
favorite theme of ours, particularly well; his Chapter 23 on "Unix" details
several examples of cooperation between AppleScript and Perl. Use a general-purpose
language for application development, and leverage AppleScript libraries
to take advantage of your Mac's unique capabilities.
- AppleScript knows Cocoa. More precisely, Apple builds in a bridge
between Cocoa and AppleScript, so that it's possible to construct light-duty
Cocoa applications entirely in AppleScript. This can be a hugely productive
way to develop small-scale programs that "look right" to Mac users;
the canonical alternative, to code in Object C, is far "heavier" and
more time-consuming, at least for projects of modest size.
- You actually don't need AppleScript to do
That last item deserves more attention — as apt as AppleScript
is for hooking into MacOS internals, it's really just a cover
for an engine that does the real work. That engine is OSA,
the Open Scripting Architecture. Several other languages,
including Perl, Python, Dylan, Java, Tcl, and Ruby, now
offer implementations that, in principle, do everything
that AppleScript does. Most of these aren't quite as succinct
in OSA operations, because their object models don't match
MacOS as well as does AppleScript's. Still, OSA opens up great
opportunities to simplify operations through use of languages
you already know.
Special thanks this month to Richard Collins, Bernard Desgraupes,
Steve Harley, Jim Ingham, Hengist Podd, and Daniel A. Steffen, who
were among the folks that helped us understand AppleScript and
related technologies. For more references, see http://wiki.tcl.tk/applescript.
Kathryn and Cameron run their own consultancy,
specializing in high-reliability and high-performance
applications managed by high-level languages. Join
them each month as their "Regular
Expressions" column explores issues and opportunities
that arise in practical application development with