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

[MacPerl-Toolbox] Fun With Compiled Scripts



This will save your MacPerl script as a compiled AppleScript.  It requires
a new Mac::OSA::Simple, which I have if you want to try it.  This is cool
for things that require compiled AppleScripts, like OSA Menu, attached
scripts to folders, ircle scripts, etc.

Of course, you can also do something like:

  tell application "MacPerl" to Do Script alias "path:to:script"

It is simple to adapt this script to do that, and I will likely do it, so
people have a choice of how they want to store their compiled scripts.  The
alias method is probably better in most cases, actually, since you can edit
the script more easily and it is less data to send via Apple Events to the
MacPerl app.  OTOH, if you want to distribute something, or if paths might
change, and the script is not going to change much, then this method might
be better.  So whatever floats your boat.  :)


#!perl -w
# p2as  :)
# pudge@pobox.com
# 1999.03.09

use Mac::OSA::Simple;
use Mac::Resources;
use Mac::Memory;
use strict;
my($comp, $data, $res, $script, $len, $file, $text);
local $/;

$text = fix_text(<>) or die "No text?";
$file = $ARGV . '.scr';

CreateResFile($file) or die $^E;
MacPerl::SetFileInfo('ToyS', 'osas', $file);

$comp = compile_applescript($text);
$data = $comp->compiled or die "No data?";
$len = length($data);
$script = NewHandle($len);
$script->set(0, $len, $data);

$res = FSpOpenResFile $file, 0 or die $^E;
AddResource($script, 'scpt', 128, 'MacPerl Script') or die $^E;
UpdateResFile($res) or die $^E;
CloseResFile($res);

sub fix_text {
    # more to do than just fix " marks?
    my $text = shift;
    $text =~ s/"/\\"/g;
    $text = <<EOS;
tell application "MacPerl" to Do Script "
$text
"
EOS
    $text;
}

__END__

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org