Unix Review > Unix Review Archives > 2004 > August 2004
Print-Friendly Version
Regular Expressions: Three Reasons to Pay Attention to AppleScript August 2004
by Cameron Laird and Kathryn Soraiz

Learn AppleScript. 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 AppleScript: The Definitive Guide and Altenburg's AppleScript 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, "scripting".

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 and efficiently.

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 line, invoke osascript:

    # osascript << HERE
    ? say "Hello, world."
    ? HERE
Hear that? As with most high-level languages, you can also create a source file, such as hello.scpt, with contents
    say "Hello, world."
and invoke osascript hello.scpt.

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:
Figure 1: Hello, world!

    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 software developers.

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.

There's more

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 know, though:

  • 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 AppleScript jobs.

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, Phaseit, Inc., 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 scripting languages.

Sys Admin Spotlight

Bringing Intelligency to Backup and Recovery
Bringing Intelligency to Backup and Recovery

CMP DevNet Spotlight

Certification: Is LPI Certification in Need of an Update?
Is the Level 1 certification from the Linux Professional Institute (LPI) still relevant? To answer that, it's important to understand what LPI is, and what the purpose of the Level 1 certification is.

In the News

Yahoo Releases APIs
"With the launch on Tuesday of the Yahoo Developer Network, Yahoo is providing access to application programming interfaces (API) for a variety of its search services, including Web, image, video, news and local search."

Firefox, Mozilla Suffer Security Holes
While the open source browser Firefox and its parent project, Mozilla, have received attention for stealing market share from Microsoft's Internet Explorer, they must also suffer the critical eye of hackers and security analysts. A patched version of the Firefox browser was released to fix nine newly discovered vulnerabilities, but Mozilla is still at risk.

Bill Gates Knighted
The United Kingdom's Queen Elizabeth bestowed an honorary knighthood—"honorary" because American citizens aren't allowed to use the title "Sir"—on Bill Gates, in recognition of his philanthropic work in developing nations.

Microsoft Raises Database Stakes at Low End
Facing tougher competition from rivals Oracle and IBM, as well as from open-source databases, Microsoft fought back last week by expanding its aging SQL Server 2000 line with a new workgroup database and disclosing feature and pricing details of its long-awaited SQL Server 2005 release, code-named Yukon.


Subscribe to the UnixReview Newsletter

Subscribe to Sys Admin

Subscribe to THE journal for UNIX systems administrators. Receive 45% off your subscription by following the link below:


Sys Admin and The Perl Journal CD-ROM version 10.0

Version 10.0 delivers every issue of Sys Admin from 1992 through 2004 and every issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!

Click Here!


File replication and content synchronization
One-to-one, scheduled file replication and content synchronization for cross platform replication on Windows, UNIX, Linux, HP and more.

Test SSL security on your site today
Take your first step towards giving your online business a competitive advantage. Test-drive a Thawte SSL certificate our easy online guide. will show you how. Click here to get started.

Free Development Tools for All Embedded Linux
Free TimeStorm Linux Development Suite (LDS) Evaluation from TimeSys works with all embedded Linux distributions to allow you to quickly define, install, bring up, develop and port Linux on your target platform.

Calavista CTK Dev Tools: No Broken Builds - Ever!
On CVS and thinking about migrating? STOP! CTK, easily installed on top of CVS and your issue tracking system, gives you best of breed CM control, continuous builds, regression tests, and visibility - and best of all, developers love the automation!

Wanna see your ad here?