[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] 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-request@macperl.org