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>

 
AAPL
$100.75
Apple Inc.
+0.64
MSFT
$46.36
Microsoft Corpora
-0.08
GOOG
$577.36
Google Inc.
+1.00

MacTech Search:
Community Search:

Software Updates via MacUpdate

QuickBooks 2015 16.0.0.1352 R1 - Financi...
QuickBooks 2015 helps you manage your business easily and efficiently. Organize your finances all in one place, track money going in and out of your business, and spot areas where you can save.... Read more
Mac DVDRipper Pro 5.0.1 - Copy, backup,...
Mac DVDRipper Pro is the DVD backup solution that lets you protect your DVDs from scratches, save your batteries by reading your movies from your hard disk, manage your collection with just a few... Read more
Apple OS X bash Update 1.0 - Fix for sec...
The OS X bash Update fixes a security flaw in the bash UNIX shell on OS X 10.9.5 (also on OS X 10.8 and 10.7 [see Related Links below]). OS X 10.9.5 or later Downloads for OS X 10.8 and OS X 10.7 in... Read more
SyncTwoFolders 2.0.5 - Syncs two user-sp...
SyncTwoFolders simply synchronizes two folders. It supports synchronization across mounted network drives and it is a possibility to run a simulation showing in a log what will be done. Please visit... Read more
FinderPop 2.5.7 - Classic Mac utility, n...
FinderPop is a Universal preference pane that extends OS X's contextual menus using a FinderPop Items folder much as the Apple Menu Items folder used to do for the Apple menu. It has other features... Read more
VueScan 9.4.45 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more
LibreOffice 4.3.2.2 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
calibre 2.4 - Complete e-library managem...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital... Read more
Default Folder X 4.6.9b1 - Enhances Open...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click... Read more
Beamer 2.0.4 - Stream any movie file fro...
Beamer streams to your Apple TV.... Plays any movie file - Just like the popular desktop movie players, Beamer accepts all common formats, codecs and resolutions. AVI, MKV, MOV, MP4, WMV, FLV. To... Read more

Latest Forum Discussions

See All

WW2: Sandbox. Strategy & Tactics (G...
WW2: Sandbox. Strategy & Tactics 1.0.0 Device: iOS Universal Category: Games Price: $9.99, Version: 1.0.0 (iTunes) Description: Sandbox is the unlimited sequel to our Strategy & Tactics: World War II. We've abandoned... | Read more »
apeFilter (Music)
apeFilter 1.0 Device: iOS Universal Category: Music Price: $6.99, Version: 1.0 (iTunes) Description: | Read more »
Shred It! Review
Shred It! Review By Jennifer Allen on September 30th, 2014 Our Rating: :: GORGEOUS BUT BASICUniversal App - Designed for iPhone and iPad It might look lovely, but Shred It! is a pretty shallow endless runner/snowboarding game.   | Read more »
It Came From Canada: Angry Birds: Transf...
Anyone afraid that throwing Transformers into the Angry Birds mix would result in a Michael Bay-level of childhood pillaging can rest easy. While Rovio’s famous fowls may be a 21st century staple, Angry Birds: Transformers wears its affection for... | Read more »
Galaxy Trucker Review
Galaxy Trucker Review By Rob Thomas on September 30th, 2014 Our Rating: :: IN FOR THE LONG HAULiPad Only App - Designed for the iPad We got a great big convoy, rockin’ through the night… the endless night of outer space, that is.... | Read more »
Moon and Sun – children’s book Review
Moon and Sun – children’s book Review By Amy Solomon on September 30th, 2014 Our Rating: Moon and Sun is a fable-like children’s book with lush illustrations.   Developer: Marcio Monteiro Price: $3.99 Version: 1.0 App Reviewed on... | Read more »
Kairosoft Finally Brings The Pyraplex to...
Kairosoft Finally Brings The Pyraplex to iOS Posted by Jessica Fisher on September 30th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Yeti’s Parole Officer Review
Yeti’s Parole Officer Review By Jennifer Allen on September 30th, 2014 Our Rating: Universal App - Designed for iPhone and iPad Yeti’s Parole Officer proves that looks aren’t everything in this entertaining, if plain, piece of... | Read more »
Record Keeper and World Wide Words – Two...
Record Keeper and World Wide Words – Two New Final Fantasy Games Arrive on iOS in Japan Posted by Ellis Spice on September 30th, 2014 [ permalink ] | Read more »
iPollute Review
iPollute Review By Nadia Oxford on September 30th, 2014 Our Rating: :: iPOLLUTE, uPOLLUTEUniversal App - Designed for iPhone and iPad iPollute is a delightful-looking pollution simulator, though it’s a bit light on “game” content... | Read more »

Price Scanner via MacPrices.net

Apple Boycotts German Magazine Computer Bild...
Apple has revoked its PR accreditation of Germany’s Computer Bild, Europe’s best-selling PC magazine, in reaction to Bild’s posting of a “#Bentgate” YouTube video. Axel Telzerow, editor in chief of... Read more
iPhone 6 & iPhone 6 Plus Available in Chi...
Apple has announced that iPhone 6 and iPhone 6 Plus will be available in China beginning Friday, October 17 from the Apple Online Store (http://www.apple.com), Apple’s retail stores, and an expansive... Read more
MacBook Airs on sale for $100 off MSRP, start...
Best Buy has the new 2014 MacBook Airs on sale for $100 off MSRP on their online store. Choose free home shipping or free local store pickup (if available). Prices valid for online orders only, in-... Read more
Apple Releases OS X Mavericks bash Update 1.0...
Apple has released a patch update for OS X Mavericks users to address the recently-detected “Shellshock” security bug in BSD UNIX’s bash shell. Apple says only a few Mac users who had manually... Read more
Pivotal Payments Ready for Apple Pay – FlexPo...
Pivotal Payments, a provider of merchant services and global payment processing solutions, has announced its proprietary FlexPoint platform will support credit and debit transactions through Apple’s... Read more
iStabilizer Announces Tabarm — First Friction...
iStabilizer, a specialist in universal lightweight compact tripods, steady cams, dollies, mounts, and remotes for smartphones, tablets, and cameras, announced today the iStabilizer tabArm, the first... Read more
IStabilizer Flex Smartphone Tripod Wins Usa T...
iStabilizer, a specialist in universal lightweight compact tripods, steady cams, and other products for smartphones, tablets, and cameras, has announced today that its iStabilizer Flex smartphone... Read more
13-inch 2.8GHz Retina MacBook Pro on sale for...
B&H Photo has the new 2014 13″ 2.8GHz Retina MacBook Pro on sale for $1699.99 including free shipping plus NY sales tax only. They’ll also include free copies of Parallels Desktop and LoJack for... Read more
15-inch Retina MacBook Pros on sale for up to...
B&H Photo has the new 2014 15″ Retina MacBook Pros on sale for up to $150 off MSRP. Shipping is free, and B&H charges NY sales tax only. They’ll also include free copies of Parallels Desktop... Read more
Get Down to Business On Your iPad With Free N...
Utah based Poem LLC has introduced Nexticy 1.0, a full-featured, affordable (Free) forms application for iPad. Nexticy makes it ‘way easy’ to create, store, retrieve and analyze paper-like forms of... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.