In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Thane Norton) wrote:

> Up until now, I have done all of my perl->AppleScript interfacing  
> using osascript. I am planning on writing an script that does a lot  
> of Scripting in OmniGraffle, and was wondering if anyone has any  
> experience with the CPAN AppleScript packages that are available.   
> Specifically I am looking at Mac::AppleScript or  
> MacPerl::AppleScript.  Thoughts?

The two are about the same.  The differences are primarily that (I think) 
Mac::AppleScript is not really supported any longer, because it was written 
before Mac::Carbon was ported to Mac OS X (and is now included in Mac OS X, 
as of Tiger), and it's a bit slower.

There's also the Mac::OSA::Simple::applescript() function, which calls the 
OSA functions directly, and is about as fast as MacPerl::DoAppleScript.

Of course, osascript is *far* slower than both.  Of course, this is not 
because osascript itself is slow, but because perl has to call out to the 
shell.  Plus, osascript is harder to use than the others, since you have to 
do all that escaping.

The non-osascript methods are all good though; MacPerl::DoAppleScript() is 
just preferred because it is already included in Tiger and it's the fastest 
of the four methods.

Here's some benchmarking I did a little while ago, and I just reran the 
Benchmark in Tiger and it's basicaly the same.

   http://www.nntp.perl.org/group/perl.macperl/2938

Also, as noted, there's Mac::Glue, which is usually a bit slower than 
calling AppleScript functions directly, but can be faster in some cases, 
too, by being able to do looping and other things Perl does better, in Perl.  
Mac::Glue calls the Apple event API directly.

But, of course, its main feature is that you can write Perl instead of 
AppleScript.

Finally, the Mac::OSA::Simple method allows you to compile scripts and 
execute them, thereby giving you the best possible performance.  (You can 
also compile a script that can accept variables, for those cases where you 
need to call the same script many times but with different values passed to 
it, by setting up handlers.)

So those are the main issues, to my mind: ease/style of use, availability, 
and performance.  Here's an update on the benchmarks, which test only raw 
performance of getting the value of a property.

   #!/usr/bin/perl
   use strict;
   use warnings;

   use Benchmark qw(timethese cmpthese);
   use Mac::AppleScript 'RunAppleScript';
   use MacPerl 'DoAppleScript';
   use Mac::OSA::Simple qw(applescript compile_applescript);
   use Mac::Glue;

   my $finder = new Mac::Glue 'Finder';
   # cache name property object so we don't need to recreate it every time
   my $prop = $finder->prop(name => of => 'startup disk');

   my $script = 'tell application "Finder" to get name of startup disk';
   my $compiled = compile_applescript($script);

   my %tests = (
     applescpt  => sub { applescript($script)         },
     applescptc => sub { $compiled->execute           },
     doscript   => sub { DoAppleScript($script)       },
     runscript  => sub { RunAppleScript($script)      },
     glue       => sub { $finder->prop(name => of => 'startup disk')->get },
     gluec      => sub { $prop->get                   },
     osascript  => sub { `osascript -ss -e '$script'` }
   );

   my $results = timethese(500, \%tests);
   cmpthese($results);


Benchmark: timing 500 iterations of applescpt, applescptc, doscript, glue, 
gluec, osascript, runscript...
 applescpt:  3 wallclock secs ( 1.40 usr +  0.38 sys =  1.78 CPU) @ 280.90/s 
(n=500)
applescptc:  1 wallclock secs ( 0.40 usr +  0.04 sys =  0.44 CPU) @ 
1136.36/s (n=500)
  doscript:  3 wallclock secs ( 1.01 usr +  0.36 sys =  1.37 CPU) @ 364.96/s 
(n=500)
      glue:  7 wallclock secs ( 2.89 usr +  1.12 sys =  4.01 CPU) @ 124.69/s 
(n=500)
     gluec:  7 wallclock secs ( 2.39 usr +  1.09 sys =  3.48 CPU) @ 143.68/s 
(n=500)
 osascript: 281 wallclock secs ( 0.13 usr  3.31 sys + 96.68 cusr 53.40 csys 
= 153.52 CPU) @ 145.35/s (n=500)
 runscript: 27 wallclock secs ( 8.15 usr +  6.58 sys = 14.73 CPU) @ 33.94/s 
(n=500)
             Rate runscript   glue gluec osascript applescpt doscript 
applescptc
runscript  33.9/s        --   -73%  -76%      -77%      -88%     -91%       
-97%
glue        125/s      267%     --  -13%      -14%      -56%     -66%       
-89%
gluec       144/s      323%    15%    --       -1%      -49%     -61%       
-87%
osascript   145/s      328%    17%    1%        --      -48%     -60%       
-87%
applescpt   281/s      728%   125%   96%       93%        --     -23%       
-75%
doscript    365/s      975%   193%  154%      151%       30%       --       
-68%
applescptc 1136/s     3248%   811%  691%      682%      305%     211%         
--


(Note: much of Mac::Carbon, and Mac::Glue, does not work yet on Intel; my 
port should be done in a week.)

Hope that helps,

-- 
Chris Nandor                      [EMAIL PROTECTED]    http://pudge.net/
Open Source Technology Group       [EMAIL PROTECTED]     http://ostg.com/

Reply via email to