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

[MacPerl] Java Scripting with MacPerl



I sent this to the macperl list last night.  A quick addendum appropriate
for this list: I am probably going to add the capability (when I am playing
with alternate targets for scripts) to dynamically get the dictionary each
time a script is executed.  Why?  Because each applet being run can define
all new terminology.  So unless you want a new glue for each applet ...

I envision something like this:

  #!perl -w
  use Mac::Glue;
  $mrj = new Mac::Glue TARGET => $executablepath, DYNAMICGLUE => 1;

So here is a quick script adapted from an AppleScript included with the MRJ
SDK.  Install Mac::Glue, create a glue for Apple Applet Runner (I called my
glue MRJ), run an applet in Apple Applet Runner, and run this script.

You also need UnderMethods, included below.  Note I never use $java-> to
call any methods.  I like it that way, many won't.  :)

#!perl -w
# show AWT components in a window
# pudge@pobox.com
# taken from an AppleScript example in MRJ SDK 2.2 EA1
# doesn't work with MRJ 2.2a1

use strict;
use Mac::Glue;
use UnderMethods;

my $java = new Mac::Glue 'MRJ';

for ($java) {
    print list_comps(obj(window => 1));
}

sub list_comps {
    my $ref = shift;
    my $pre = shift || '';
    my $rst = sprintf "%s%s", $pre, comp_name($ref);
    $pre .= "  ";
    my $c = get(prop(component_count => $ref));
    for my $i (1 .. $c) {
        my $obj = obj(component => $i => $ref);
        if (check($obj, belongs_to => 'Container')) {
            $rst .= list_comps($obj, $pre);
        } else {
            $rst .= sprintf "%s%s", $pre, comp_name($obj);
        }
    }
    return $rst;
}

sub comp_name {
    my $ref   = shift;
    my $obj   = get(prop(name => $ref)) || 'anonymous';
    my $class = (split m/\./, get(prop(name => of => class => $ref)))[-1];
    return "$class $obj\n";
}

__END__

UnderMethods.pm:

package UnderMethods;

use strict;
use overload;
use Carp;
use vars '$AUTOLOAD';

sub import {
    no strict 'refs';
    *{ caller() . "::AUTOLOAD" } = \&AUTOLOAD;
}

sub AUTOLOAD {
    my($ref) = (overload::StrVal($_) =~
/^(?:(.*)\=)?(?:[^=]*)\((?:[^\(]*)\)$/);
    croak "Undefined subroutine &$AUTOLOAD called" unless $ref;
    (my $name = $AUTOLOAD) =~ s/.*:://;
    my $func = $_->can($name);
    confess "Can't call method `$name' in $ref" unless $func;
    unshift @_, $_;
    goto &$func;
}

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

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