macperl-toolbox-digest Sunday, March 14 1999 Volume 01 : Number 005 [MacPerl-Toolbox] OpenResFile [MacPerl-Toolbox] New Mac::OSA::Simple, p2as Re: [MacPerl-Toolbox] New Mac::OSA::Simple, p2as Re: [MacPerl] Re: [MacPerl-Toolbox] New Mac::OSA::Simple, p2as [MacPerl-Toolbox] Mac::Glue ponderings Re: [MacPerl-Toolbox] Mac::Glue ponderings ---------------------------------------------------------------------- Date: Fri, 12 Mar 1999 08:55:25 -0500 From: Chris Nandor <pudge@pobox.com> Subject: [MacPerl-Toolbox] OpenResFile FSpOpenResFile and OpenResFile have a bug in them that causes them to always treat $ENV{MACPERL} as the current working directory, instead of treating the value of chop($pwd = `pwd`) as the current working directory. I am releasing Mac::OSA::Simple 0.50 soon, which allows you to save and load compiled AppleScripts in resource forks by filename, and I need to warn people to pass an absolute path. - -- Chris Nandor mailto:pudge@pobox.com http://pudge.net/ %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6']) ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org ------------------------------ Date: Fri, 12 Mar 1999 10:23:01 -0500 From: Chris Nandor <pudge@pobox.com> Subject: [MacPerl-Toolbox] New Mac::OSA::Simple, p2as I put up a new Mac::OSA::Simple 0.50 this morning, should be making its rounds. Here is a revised p2as that takes advantage of the new save() method. It greatly simplifies the script. It is now very easy to save something as a compiled AppleScript: #!perl -w use Mac::OSA::Simple; $script = compile_applescript('return "foo"'); $script->save('HD:Desktop Folder:newscript'); And equally easy to load it in: $script2 = load_osa_script('HD:Desktop Folder:newscript'); print $script2->execute; Enjoy! Read the docs in the module if you want to use it directly, there are some issues you need to know about. #!perl -w # p2as # pudge@pobox.com # 1999.03.12 use File::Basename; use Mac::OSA::Simple 0.50 qw(:all); use Mac::Resources; use Mac::Memory; use strict; sub fix_text (\$); die "Need at least one Perl script!\n" unless @ARGV; # select which type of compiled script you want ... hardcode this # if you like: Text = 1, Alias = 0 my $switch = MacPerl::Answer('For all scripts, save script text or ' . 'alias to script on disk?', 'Text', 'Alias'); # drop as many scripts as you can handle for my $f (@ARGV) { my($comp, $data, $res, $script, $len, $file, $dir, $text); # get AppleScript text $text = ($switch ? get_text($f) : get_alias($f)) or (warn("No text for '$f'") && next); # get new name of file ($file, $dir) = fileparse($f, '\..+$'); $file = "$dir$file.scr"; # get compiled AppleScript and save it to the file $comp = compile_applescript($text); $comp->save($file); } sub get_alias { my($file, $text) = @_; fix_text($file); $text = qq'tell application "MacPerl" to Do Script alias "$file"'; $text; } sub get_text { my($file, $script, $text) = @_; local($/, *F); open F, $file or die "Can't open '$file': $!"; $script = <F>; close F or die "Can't close '$file': $!"; fix_text($script); $text = <<EOS; tell application "MacPerl" to Do Script " $script " EOS $text; } sub fix_text (\$) { my $text = shift; # more to do than just fix " marks and \ ? $$text =~ s/\\/\\\\/g; $$text =~ s/"/\\"/g; 1; } __END__ - -- Chris Nandor mailto:pudge@pobox.com http://pudge.net/ %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6']) ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org ------------------------------ Date: Fri, 12 Mar 1999 09:49:25 -0600 From: "Jefferson R. Lowrey" <lowrey@mailbag.com> Subject: Re: [MacPerl-Toolbox] New Mac::OSA::Simple, p2as At 10:23 AM -0500 3/12/99, Chris Nandor wrote: >I put up a new Mac::OSA::Simple 0.50 this morning, should be making its >rounds. Here is a revised p2as that takes advantage of the new save() >method. It greatly simplifies the script. It is now very easy to save >something as a compiled AppleScript: > Just a quick thanks for all the hard work you do for MacPerl. Thanks! >sub fix_text (\$) { > my $text = shift; > > # more to do than just fix " marks and \ ? > $$text =~ s/\\/\\\\/g; > $$text =~ s/"/\\"/g; > > 1; >} Do we want to recognize the AppleScript line continuation character? It might not be terribly necessary, but there could be instances where people are reading in scripts that have it. I'm going to type it in in quotes, and hope it doesn't get too mangled by mailers "¬". I assume the AS compiler recognizes it by ASCII value - but that might not be true. If so, it's 194. - -Jeff Lowrey ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org ------------------------------ Date: Fri, 12 Mar 1999 12:03:55 -0500 From: Chris Nandor <pudge@pobox.com> Subject: Re: [MacPerl] Re: [MacPerl-Toolbox] New Mac::OSA::Simple, p2as At 10.49 -0500 1999.03.12, Jefferson R. Lowrey wrote: >>sub fix_text (\$) { >> my $text = shift; >> >> # more to do than just fix " marks and \ ? >> $$text =~ s/\\/\\\\/g; >> $$text =~ s/"/\\"/g; >> >> 1; >>} > >Do we want to recognize the AppleScript line continuation character? It >might not be terribly necessary, but there could be instances where people >are reading in scripts that have it. I'm going to type it in in quotes, >and hope it doesn't get too mangled by mailers "¬". I assume the AS >compiler recognizes it by ASCII value - but that might not be true. If so, >it's 194. It shouldn't need to be escaped, because when in quotes in AppleScript, it is treated as normal text, which is what is wanted. So: --- script.plx --- print "¬" Becomes: --- script.scr --- tell application "MacPerl" to Do Script "print \"¬\"" Which does what we want. - -- Chris Nandor mailto:pudge@pobox.com http://pudge.net/ %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6']) ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org ------------------------------ Date: Fri, 12 Mar 1999 14:03:05 -0500 From: Chris Nandor <pudge@pobox.com> Subject: [MacPerl-Toolbox] Mac::Glue ponderings Looking at all of this OSA stuff, I wonder if there might be another way. Here is a thought of another way to do this: use Mac::Glue; use Mac::MoreFiles; $finder = Mac::Glue->new('Finder'); $finder->select( 'startup disk' ); $finder->open('selection', { using => qq[alias "$Application{'R*ch'}"] }); $finder->glue_finish; This would get converted to the text: tell application "Finder" select startup disk open selection using alias "HD:BBEdit 5.0:BBEdit 5.0" end tell And it would be compiled down into an OSA script. Then it could be executed: $finder->glue_execute; Of course, there is a big downside here: it takes a lot longer to compile an OSA script than it does to execute an Apple Event. #!perl -w use Benchmark; use Mac::OSA::Simple; use Mac::AppleEvents::Simple; use strict; use vars qw($as $osa $as_c $osa_c); $Mac::AppleEvents::Simple::SWITCH = 0; # two ways to do same thing $as = [qw[misc slct MACS], q"'----':obj {form:prop, want:type(prop), seld:type(sdsk), from:'null'()}"]; $osa = <<EOS; tell application "Finder" select startup disk end tell EOS # compile it first $as_c = build_event(@$as); $osa_c = compile_applescript($osa); timethese(100, { AppleEvents => 'do_event(@$as)', OSA => 'applescript($osa)', AppleEvents_C => '$as_c->send_event', # note, there is a bug in # current Mac::AppleEvents::Simple # so this will not work for you :) OSA_C => '$osa_c->execute' }); __END__ Benchmark: timing 100 iterations of AppleEvents, AppleEvents_C, OSA, OSA_C... AppleEvents: 5 secs ( 5.10 usr 0.00 sys = 5.10 cpu) AppleEvents_C: 5 secs ( 4.80 usr 0.00 sys = 4.80 cpu) OSA: 8 secs ( 8.33 usr 0.00 sys = 8.33 cpu) OSA_C: 6 secs ( 5.43 usr 0.00 sys = 5.43 cpu) Unfortnately, precompiling the OSA script and storing it (in the file resource fork, for instance) is not a good option, because often there will be perl expressions passed to the scripts which would need to be evaluated at run time. So, comments or thoughts? - -- Chris Nandor mailto:pudge@pobox.com http://pudge.net/ %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6']) ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org ------------------------------ Date: Fri, 12 Mar 1999 14:35:46 -0500 From: Chris Nandor <pudge@pobox.com> Subject: Re: [MacPerl-Toolbox] Mac::Glue ponderings At 14.03 -0500 1999.03.12, Chris Nandor wrote: >Unfortnately, precompiling the OSA script and storing it (in the file >resource fork, for instance) is not a good option, because often there will >be perl expressions passed to the scripts which would need to be evaluated >at run time. Although, now that I think of it, this could be done if you _know_ that the script will not change. You could do something like: $script_id = 130; if (script_id_exists_in_this_file($script_id)) { $script = load_script_from_this_file($script_id); } else { $script = construct_script(); $script->save_in_this_file($script_id); } That would speed up execution to be similar to that of the Apple Events methods after the first execution, as long as you didn't mess with the resource fork and remove the script data. And if you rewrote the script to be saved, you could wipe the data with ResEdit or a droplet or something. - -- Chris Nandor mailto:pudge@pobox.com http://pudge.net/ %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6']) ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org ------------------------------ End of macperl-toolbox-digest V1 #5 *********************************** ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" ==== to macperl-toolbox-digest-request@macperl.org