TweetFollow Us on Twitter

Feb 99 Tips

Volume Number: 15 (1999)
Issue Number: 2
Column Tag: Tips & Tidbits

Feb 99 Tips

by Jeff Clites <online@mactech.com>

AppleEvents in MacPerl

AppleEvents are the standard for interprocess communication on the Macintosh, and they should be part of any programmer's arsenal. Unfortunately, they can be as confusing as they are powerful when you are first trying to learn about them. MacPerl has several different levels at which you can work with AppleEvents, and you can take advantage of this in a number of ways - as a place to experiment while you are learning, as an AppleScript replacement for automation tasks which require a little more processing power, or as a rapid prototyping tool while you are developing your AE code for use in another application.

The following script demonstrates three different ways you can accomplish the same task using AppleEvents in MacPerl. Each retrieves the list of AppleScripts attached to a specified folder, both sending the request event and processing the reply.

#!perl -wl
use strict;
my $folder   = 'MacHD:Desktop Folder:test folder:';

# print the results of each of the three methods 
(they should be the same)
printf("%s:\n%s\n\n%s:\n%s\n\n%s:\n%s\n\n",
   'AppleScript',              join("\n", getscripts_as()),
   'Apple Events',             join("\n", getscripts_ae()),
   'Simple Apple Events',      join("\n", getscripts_ae_simple()),
);

# Retrieve the list of attached scripts...

# ...by compiling an AppleScript
sub getscripts_as
{
   my $reply = MacPerl::DoAppleScript(<<EOS);
   tell application "Folder Actions"
      attached scripts alias "$folder"
   end tell
EOS

   # the reply is a list within a list, so this must be unwound
   $reply =~ s/^{{(.*)}}$/$1/;
   
   # the resulting string is split up into an array and returned
   return($reply =~ /alias "([^"]+)"(?:, |$)/g);
}


# ...by sending an AppleEvent built using the AEGizmos notation, 
# and retrieving the reply using standard Toolbox functions
use Mac::AppleEvents;
use Mac::Files;

sub getscripts_ae
{
   my($evt, $rep, $dobj, $data, @alis);

   $evt = AEBuildAppleEvent(
                  qw(faco lact sign ssrv 0 0), 
                  "'--':alis(\@\@)",
                  NewAliasMinimal($folder)) or die $^E;

   $rep  = AESend($evt, kAEWaitReply) or die $^E;
   $dobj = AEGetParamDesc($rep, keyDirectObject);
   $data = AEGetNthDesc($dobj, 1);

   for (1 .. AECountItems($data))
   {
      my $desc = AEGetNthDesc($data, $_);
      push @alis, scalar ResolveAlias($desc->data);
      AEDisposeDesc($desc);
   }

   for ($evt, $rep, $dobj, $data) {AEDisposeDesc $_}

   return(@alis);
}


# ...by using MacPerl's simplified AppleEvent interface to send and receive 
use Mac::AppleEvents::Simple qw(:all);
use Mac::Files;

sub getscripts_ae_simple
{
   my($evt, $data, @alis);

   $evt = do_event(
                  qw(faco lact ssrv), 
                  "'--':alis(\@\@)",
                  NewAliasMinimal($folder)) or die $^E;

   $data = $evt->data;
   @alis = Mac::AppleEvents::Simple::get($data);
   AEDisposeDesc($data);

   return(@alis);
}

__END__

There is a new MacPerl module under development, called Mac::Glue, which will simplify the process even more, and almost completely hide the details behind perl-style functions. Although it is currently in the alpha stages, you will soon be able to use the following syntax (or something very similar) to achieve the same result:

use Mac::Glue::Folder_Actions qw(:all);

sub getscripts_glue 
{
   my($f, $data, @alis);

   $f = new Mac::Glue::Folder_Actions;
   $data = $f->attached_scripts($folder)->data;
   @alis = Mac::AppleEvents::Simple::get($data);
   AEDisposeDesc($data);
   return(@alis);
}

Chris Nandor <pudge@pobox.com>

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Is there cross-platform play in slither....
So you've sunken plenty of hours into crawling around in slither.io on your iPhone or iPad. You've got your stories of tragedy and triumph, the times you coiled four snakes at one time balanced out by the others when you had a length of more than... | Read more »
Rodeo Stampede guide to running a better...
In Rodeo Stampede, honing your skills so you can jump from animal to animal and outrun the herd as long as possible is only half the fun. Once you've tamed a few animals, you can bring them home with you. [Read more] | Read more »
VoxSyn (Music)
VoxSyn 1.0 Device: iOS Universal Category: Music Price: $6.99, Version: 1.0 (iTunes) Description: VoxSyn turns your voice into the most flexible vocal sound generator ever. Instantly following even subtle modulations of pitch and... | Read more »
Catch Battleplans on Google Play from Ju...
Real-time strategy title Battleplans is due for release on Google Play on June 30th, following its release for iOS systems last month. With its simple interface and pretty graphics, the crowd-pleaser brings a formerly overlooked genre out for the... | Read more »
iDoyle: The interactive Adventures of Sh...
iDoyle: The interactive Adventures of Sherlock Holmes - A Scandal in Bohemia 1.0 Device: iOS Universal Category: Books Price: $1.99, Version: 1.0 (iTunes) Description: Special Release Price $1.99 (Normally $3.99) | Read more »
Five popular free apps to help you slim...
Thanks to retail and advertising, we're used to thinking one season ahead. Here we are just a week into the summer and we're conditioned to start thinking about the fall. [Read more] | Read more »
How to ride longer and tame more animals...
It's hard to accurately describe Rodeo Stampede to people who haven't seen it yet. It's like if someone took Crossy Roadand Disco Zoo and put them in a blender, yet with a unique game mechanic that's still simple and fun for anyone. [Read more] | Read more »
Teeny Titans - A Teen Titans Go! Figure...
Teeny Titans - A Teen Titans Go! Figure Battling Game 1.0.0 Device: iOS Universal Category: Games Price: $3.99, Version: 1.0.0 (iTunes) Description: Teeny Titans, GO! Join Robin for a figure battling RPG of epic proportions! TEENY... | Read more »
NinjAwesome: Tips and tricks to be a mor...
Sorry about that headline, but I'm going to go ahead and assume that GameResort would not have named its game NinjAwesome without expecting some of that. It is, in fact, pretty awesome the way it combines an endless runner and old school arcade... | Read more »
Into Mirror (Games)
Into Mirror 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: "Is all that we see or seem, but a dream within a dream?"- Edgar Allan Poe New game by Lemon Jam Studio, the team behind Pursuit... | Read more »

Price Scanner via MacPrices.net

15-inch Retina MacBook Pros on sale for $200-...
B&H Photo has 15″ Retina MacBook Pros on sale for up to $210 off MSRP. Shipping is free, and B&H charges NY tax only: - 15″ 2.2GHz Retina MacBook Pro: $1799.99 $200 off MSRP - 15″ 2.5GHz... Read more
Mac minis on sale for up to $100 off MSRP
B&H Photo has Mac minis on sale for up to $100 off MSRP including free shipping plus NY sales tax only: - 1.4GHz Mac mini: $449 $50 off MSRP - 2.6GHz Mac mini: $649 $50 off MSRP - 2.8GHz Mac mini... Read more
Clearance 2015 13-inch MacBook Airs available...
B&H Photo has clearance 2015 13″ MacBook Airs available for $300 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 13″ 1.6GHz/4GB/128GB MacBook Air (MJVE2LL/A): $799.... Read more
Apple refurbished Mac minis available for up...
Apple has Certified Refurbished Mac minis available starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free: - 1.4GHz Mac mini: $419 $80 off MSRP - 2.6GHz Mac... Read more
ABBYY TextGrabber: 1,000,000 Installs in 5 Da...
ABBYY, an international OCR technologies provider, has announced that their image-to-text application TextGrabber, got installed 1,000,000 times in just five days while being featured by the App... Read more
New SkinIt Waterproof Case For iPhone 6
With its impact and waterproof design, the Skinit Waterproof case provides security and protection to guarantee your phone will get you through even the most demanding outdoor conditions. The impact-... Read more
iMacs on sale for up to $150 off MSRP
B&H Photo has 21″ and 27″ iMacs on sale for up to $150 off MSRP including free shipping plus NY sales tax only: - 27″ 3.3GHz iMac 5K: $2181.11 $118 off MSRP - 27″ 3.2GHz/1TB Fusion iMac 5K: $1949... Read more
12-inch 1.1GHz Retina MacBooks on sale for $5...
B&H Photo has 2016 12″ 1.1GHz/256GB Retina MacBooks on sale for up to $50 off MSRP. Shipping is free, and B&H charges NY tax only: - 12″ 1.1GHz Space Gray Retina MacBook: $1249 $50 off MSRP... Read more
WWDC Announcements Revisited Still Underwhelm...
I was disappointed that no new MacBook hardware was announced at this year’s all-software World Wide Developer’s Conference. Not even a hint about what’s in the development pipeline. Of course, we... Read more
Twelve South Compass 2 iPad Stand Now Availab...
Twelve South has updated its most popular iPad stand, Compass 2, with the introduction of two new colors — Gold and Rose Gold. These new color options n perfectly complement the new Rose Gold iPad... Read more

Jobs Board

*Apple* iPhone 6s and New Products Tester Ne...
…we therefore look forward to put out products to quality test for durability. Apple leads the digital music revolution with its iPods and iTunes online store, Read more
Music Marketing Lead, iTunes & *Apple*...
…Music Marketing Lead is responsible for developing robust marketing campaigns and programs for Apple Music and iTunes across the whole of Apple ecosystem. This Read more
*Apple* Valley Medical Clinic is Hiring - AP...
Apple Valley Medical Clinic is Hiring! Apple Valley Medical Clinic is an independently owned practice operating a Family Medicine Clinic, a 24/7 Urgent Care, Read more
*Apple* New Products Testers Needed - Apple...
…we therefore look forward to put out products to quality test for durability. Apple leads the digital music revolution with its iPods and iTunes online store, Read more
*Apple* Solutions Consultant - APPLE (United...
Job Summary As an Apple Solutions Consultant, you'll be the link between our future customers and our products. You'll showcase your entrepreneurial spirit as you Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.