• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT

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>

 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live