Re: Exposing Tcl commands via Perl Tkx for Apple Event IPC

[email protected] (Kevin Walzer) Mon, 23 Jun 2014 10:57:17 -0400
Newsgroups perl.tcltk
Organization WordTech Communications LLC
Message-ID <[email protected]>
On 6/22/14, 12:13 PM, Kevin Walzer wrote:
> 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?

After doing more investigation, I believe the command was not being 
picked up because the call to Tcl->new produced a new Tcl interpreter. 
What I was looking to do was to have the command picked up by the main 
Tcl interpreter, running the GUI code.

The solution was so simple and elegant it astounded me:

Tkx::tclAE__installEventHandler ("Perl", "hihi", \&hello);

In other words, pass the reference to the subroutine, just like a 
command callback, and the Tkx bridge takes care of mapping it internally 
to a Tcl command and then returning the correct output first to the app, 
then to the calling AppleScript.

Amazing. I've been beating my head against a wall for a week trying to 
get this to work at the C level, without results. This opens up all 
kinds of things for my projects--thank you so much for the terrific work!

--Kevin






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