develooper Front page | perl.macosx | Postings from April 2003

Re: Applescript from Perl (OSX)?

Thread Previous
From:
Chris Nandor
Date:
April 23, 2003 07:47
Subject:
Re: Applescript from Perl (OSX)?
Message ID:
pudge-D31ECE.10475323042003@onion.valueclick.com
In article <BACC0A06.3A61%perotti@pobox.com>,
 perotti@pobox.com (Riccardo Perotti) wrote:

>On 04/23/2003 06:32 AM, "Chris Nandor" <pudge@pobox.com> wrote:
>
>>  for my $file (@files) {
>>    my $data = do { local $/; open my $fh, '<', $file; <$fh> };
>>    $bbedit->compare($file, against => $path)
>>      unless $data eq $master;
>>  }
>
>Excuse me: where did the '$bbedit->compare()' type of stuff come from?
>This looks like something I would definitely use pretty often!

It is Mac::Glue.  Works on MacPerl for Mac OS, and currently being ported to 
Mac OS X.  I use it, but it has a lot of work to do to prep for release, a 
lot of cleanup.  So, "soon."

For a quick overview: you create "glues" for scriptable apps (in MacPerl 
with a droplet, in Mac OS X with a command-line script):

   % gluemac /Applications/iTunes.app

That generates the glue, which contains mappings for "AppleScript" 
vocabulary to the underlying Apple events, and POD describing the classes, 
events, etc. for that application (you also need to run similar scripts to 
create glues for the main AppleScript "dialect" and any scripting additions 
you are using).  Then you create an object and start using it.

   my $itunes = new Mac::Glue 'iTunes';
   $itunes->play;

And it Just Works.  No AppleScript involved, though the same vocabulary is 
used.  No figuring out the event ID/suite ID/app signature.  Mac::Glue does 
all the work.  In theory it should be faster than calling DoAppleScript() or 
somesuch (though as noted, it is slightly slower right now on Mac OS X), but 
even without performance increases it is much nicer to use IMO.

The biggest drawbacks to Mac::Glue are 1. startup time (it can take a few 
seconds, depending on the speed of the machine, to load in the required 
extensions ... I hope there is a solution to this at some point) and 2. 
complexity.

In some ways, the AppleScript versions are less complex, due to various 
factors, including the fact that you can have context-sensitive barewords in 
AppleScript (in a tell block for iTunes, AppleScript can know that "current 
track" is a property, but in a tell block for the Finder, it will know that 
it isn't), so Perl needs some extra syntax sugar (and I am open to 
suggestions on ways to make it easier).


>Also, Chris, just so that I have it clear: for MacOSX, which would be the
>(your) preferred way of calling AppleScripts and/or using osa commands?

Any of RunAppleScript (Mac::AppleScript), DoAppleScript (MacPerl.pm), and 
applescript (Mac::OSA::Simple) are about equivalent (though RunAppleScript 
is not ported/built for MacPerl, so that is a portability consideration).  
They are all far easier to use and more efficient than calling osascript 
with system/open/``.

The only reason I can see to call osascript is for portability (machines 
that don't have the modules installed), and even then, I'd only do it if 
there weren't a lot of successive calls required.

If you want to execute compiled scripts from disk, or run the same script 
multiple times, Mac::OSA::Simple is really the way to go (though it is the 
least mature ... I found another bug last night, I'll be releasing a new 
version soon :-).

Of course, my preferred method is to eschew AppleScript altogether, which is 
what Mac::Glue is for.

As with everything, YMMV.

-- 
Chris Nandor                      pudge@pobox.com    http://pudge.net/
Open Source Development Network    pudge@osdn.com     http://osdn.com/

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About