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

Latest Forum Discussions

See All

How to get started with Prisma
If there's one thing people like to do more than taking pictures with their smartphones, it's tinkering with those photos in some way. Numerous apps have sprung up over the last several years that allow you to use filters and special effects to... | Read more »
6 Pokemon GO updates you can expect, acc...
Pokemon GO had a scheduled appearance at this year's San Diego Comic-Con for a while, but it was only relatively close to the show that it was upgraded to a spot in Hall H. That's the biggest venue at SDCC, one usually reserved for the largest... | Read more »
How to evolve Eevee in Pokemon GO
By now, almost everyone should be hip to how to evolve Pokemon in Pokemon GO (and if not, there's a guide for that). Just gather enough candy of the appropriate type, feed them all to the Pokemon, and evolution happens. It's a miracle that would... | Read more »
CSR Racing 2: Guide to all game modes
It might not seem like there are all that many ways to go fast in a straight line, but CSR Racing 2 begs to differ. [Read more] | Read more »
Bulb Boy (Games)
Bulb Boy 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Multi-award winning 2D point & click horror adventure about a boy with a glowing head. | Read more »
5 top free emoji keyboard apps
If we're not at peak emoji yet as a society, it feels like we definitely should be. The emoji concept has gone far beyond what anyone in Japan could have envisioned when the people there unleashed it on an unsuspecting world, but the West has... | Read more »
How to unlock more characters in Disney...
One of the big charms of Disney Emoji Blitz is seeing a wide variety of beloved Disney and Pixar characters transformed into smiling emojis. Even someone like the sneaky Randall from Monsters Inc., who probably never cracked a smile on film, is... | Read more »
Cubway (Games)
Cubway 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Cubway is a journey with an abstract story of lifecycle of rebirth, called Samsara. Guide the cube through the long way full of dangers... | Read more »
Colorcube (Games)
Colorcube 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Turn pieces and blend colours in this minimal yet visually stunning puzzler.Over 200 handcrafted and challenging levels. Features... | Read more »
Doodle God Griddlers (Games)
Doodle God Griddlers 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: | Read more »

Price Scanner via MacPrices.net

13-inch 1.6GHz/128GB MacBook Air on sale for...
Amazon has the 13″ 1.6GHz/128GB MacBook Air on sale for $200 off MSRP for a limited time. Shipping is free: - 13″ 1.6GHz/128GB MacBook Air (sku MMGF2LL/A): $799.99 $200 off MSRP Their price is the... Read more
13-inch 1.6GHz/256GB MacBook Air on sale for...
Amazon has the 13″ 1.6GHz/256GB MacBook Air on sale for $200 off MSRP for a limited time. Shipping is free: - 13″ 1.6GHz/256GB MacBook Air (sku MMGG2LL/A): $999.99 $200 off MSRP Their price is the... Read more
Free iOS Business App notably* Helps Service...
PayStudio Inc. has introduced their new business app notably* 1.0, developed for iPhone and iPod touch. notably* was specifically developed to help service and trade professionals go digital and... Read more
27-inch iMacs on sale for $200 off MSRP
Amazon has 27″ iMacs on sale for $200 off MSRP including free shipping: - 27″ 3.3GHz iMac 5K: $2099 $200 off MSRP - 27″ 3.2GHz/1TB Fusion iMac 5K: $1799.99 $200 off MSRP - 27″ 3.2GHz/1TB HD iMac 5K... Read more
Mac Pros on sale for $200 off MSRP
B&H Photo has Mac Pros on sale for $200 off MSRP. Shipping is free, and B&H charges sales tax in NY only: - 3.7GHz 4-core Mac Pro: $2799, $200 off MSRP - 3.5GHz 6-core Mac Pro: $3799, $200... Read more
Save up to $600 with Apple refurbished Mac Pr...
Apple has Certified Refurbished Mac Pros available for up to $600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The following... Read more
Apple price trackers, updated continuously
Scan our Apple Price Trackers for the latest information on sales, bundles, and availability on systems from Apple’s authorized internet/catalog resellers. We update the trackers continuously: - 15″... Read more
13-inch 2.5GHz MacBook Pro (Apple refurbished...
Apple has Certified Refurbished 13″ 2.5GHz MacBook Pros available for $829, or $270 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free: - 13″ 2.5GHz MacBook Pros... Read more
21-inch iMacs on sale for up to $120 off MSRP
B&H Photo has 21″ iMacs on sale for up to $120 off MSRP including free shipping plus NY sales tax only: - 21″ 3.1GHz iMac 4K: $1379 $120 off MSRP - 21″ 2.8GHz iMac: $1199.99 $100 off MSRP - 21″ 1... Read more
Charitybuzz Set to Auction Unique Apple-1 Com...
Offering an opportunity to own the computer that sparked a revolution, on Monday, July 25, leading online charity auction platform Charitybuzz will auction what is claimed to be the world’s most... Read more

Jobs Board

*Apple* Retail - Multiple Positions, Willow...
Job Description:SalesSpecialist - Retail Customer Service and SalesTransform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Evangelist - JAMF Software (United S...
The Apple Evangelist is responsible for building and cultivating strategic relationships with Apple 's small and mid-market business development field teams. This 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
*Apple* Professional Learning Specialist - A...
Job Summary The Apple Professional Learning Specialist is a full-time position for one year with Apple in the Phoenix, AZ area. This position requires a high Read more
*Apple* Picker - Apple Hill Orchard (United...
Apple Hill Orchard, Co. Rte. 21,Whitehall, NY 9/7/16-10/228/16. Pick fresh market or processing apples Productivity of 60 boxes and 80 boxes processing fruit per Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.