Exposing Tcl commands via Perl Tkx for Apple Event IPC

[email protected] (Kevin Walzer) Sun, 22 Jun 2014 12:13:45 -0400
Newsgroups perl.tcltk
Organization WordTech Communications LLC
Message-ID <[email protected]>
I've been trying, without success, to write an Apple Event library for 
Perl that will allow my Perl Tkx apps to respond to and send Apple 
Events. (Apple Events are a form of IPC on the Mac that allows native 
apps to communicate with each other, exchange data, and even drive other 
apps--somewhat analogous to COM or DDE on Windows.) Perl's C API is very 
gnarly and so is the Apple Events API from C, and so I've made little 
progress there.  There is a long-established support for Apple Events in 
the Mac::Carbon module at CPAN, but this module does not support 64-bit 
(Apple has deprecated many of the API's the module wraps) and its author 
is disinclined to update it, instead advising users to simply run Perl 
as a 32-bit app--not a viable option for me.

As a result, I have turned to a native Tcl library that integrates with 
the Apple Event API, tclAE (http://tclae.sourceforge.net). I've used 
this library directly from Tcl with no problem, and would like to use 
Tkx's integration to use it in my Perl Tk apps as well. However, thus 
far I have not had success.

tclAE works by allowing you to register specific Apple Events with the 
system and to register an internal Tcl command that implements the Apple 
Event functionality. (Externally the command is mapped to an AppleScript 
command that is registered elsewhere.) Doing this via Tkx, I have 
started with this command:

Tkx::tclAE__installEventHandler ("Perl", "hihi", "perl::hello");

"Perl" and "hihi" are the Apple Event fields (they are arbitrary 
four-char fields) and "perl::hello" is the internal subroutine that 
responds to the command. That should work, but when I actually run an 
AppleScript command targeting my test app, I get this error:

perlapp got an error: invalid command name "perl::hello"

That error string means that it's a non-existent Tcl command.

Here is where I am not sure if I am doing things correctly. I am 
exposing the Perl command to the Tcl interpreter by defining it thus as 
a Perl subroutine:

sub tcl::hello (theAppleEvent theReturnAE)

and then exporting it thus:

my $tcl = Tcl->new;

$tcl->export_to_tcl(subs_from=>'tcl', vars_from=>'tcl');

This seems to provide the cleanest and simplest mapping between Perl 
subroutines and Tcl commands. I am also aware that I can define the 
command as this:

$tcl->CreateCommand(hello, \&hello):

Is one method better than the other?

In any event, tclAE is not picking up on the Tcl command I'm passing to 
the Tcl interpreter, and I believe it should. Can anyone suggest how I 
can get this command exposed cleanly to the Tcl interpreter if I am not 
doing it correctly?

--Kevin
-- 
Kevin Walzer
Code by Kevin/Mobile Code by Kevin
http://www.codebykevin.com
http://www.wtmobilesoftware.com