Previous Page
Next Page

B.4. Perl

Perl has a surprisingly long history on the Macintosh; years before it was present as part of Unix in Mac OS X, it had been ported to the Classic Mac OS by Matthias Neeracher (see http://www.ptf.com/macperl). Modules to form and send Apple events were naturally part of this port (see http://www.ptf.com/macperl/depts/articles/IPCwMP.html). Support for these modules has continued, under the guidance of Chris Nandor, and they are available to Mac OS X users.

Once again, you have a choice: you can form the raw Apple events yourself, using Mac::AppleEvents, or you can use AppleScript terminology through Mac::Glue. The operation of Mac::Glue is similar to Frontier with its glue tables (and is probably deliberately modelled after it); a glue table for a given application must be formed and stored on disk before you can target that application using AppleScript English-like terminology. In order to get started, you will need Mac::Carbon (which includes Mac::AppleEvents) as well as Mac::Glue; on Tiger these are installed by default, but it can't hurt to check for more recent versions.

To make glue, run the gluemac Unix tool. For example:

% sudo /usr/bin/gluemac /Volumes/gromit/Users/matt2/extra/BBEdit\ 8/BBEdit.app
Making glue for '/Volumes/gromit/Users/matt2/extra/BBEdit 8/BBEdit.app'
What is the glue name? [BBEdit]:
Created and installed App glue for 'BBEdit.app, v8.2.3' (BBEdit)

The resulting glue table contains a document in the standard Perl documentation format (POD, for "plain old documentation") expressing the dictionary in Perl syntax. You can read it with perldoc; another tool, gluedoc, provides a shortcut to the desired document:

% gluedoc BBEdit

Using the POD document and the instructions for Mac::Glue, you can usually figure out the Perl analogue to an AppleScript expression. Here's code to generate our model events:

use Mac::Glue;
my $bb = Mac::Glue->new('BBEdit');
$bb->launch( );
$bb->make(new => 'document');
my $doc = $bb->obj(document => 1);
my $text = $bb->obj(property => 'text', $doc);
$bb->set($text, to => 'Hello, world!');

Personally, I find Perl's notion of object-orientation clunky, and Mac::Glue's expression of AppleScript terminology rather inconsistent. Moreover, a Perl script that uses Mac::Glue takes even longer to get running than the corresponding AppleScript code would take to compile. This makes me wonder why it wouldn't be preferable to use osascript and have done with it (Chapter 25). Still, it's Perl, and for people who like this sort of thing, this is the sort of thing they like.


Previous Page
Next Page