February 10, 2003
Lazy AppleScript Sending

I keep getting asked over and over again how to make AppleEvents from lame AppleScripts. Might as well post it here because it's easier. This assumes you have the cocoa version of Script editor. Special thanks to George Warner, as always.

Save the below code to a file, I named mine "AEDesc"

define aedesc
  call (void *) malloc(4)
  set $aed_malloc=$
  call (long) AEPrintDescToHandle($arg0, $aed_malloc)
  if $ == 0
    printf "desc @ %p = {\n    type = '%.4s'\n    storage (%p) = %s\n}\n", \
      $arg0, $arg0, ((long *) $arg0)[1], **(char ***) $aed_malloc
    call (void) DisposeHandle(*(char ***) $aed_malloc)
  else
    printf "aedesc failed: error %d.\n", $
  end
  call (void) free($aed_malloc)
end

Then in the terminal type:

gdb /Applications/AppleScript/Script\ Editor.app/Contents/MacOS/Script\ Editor
fut AESend
source <path/to/file/with/below/code>
run

Then run your script in the Script Editor and hit run. I'll use this script as an example:

tell application "iTunes"
	get name
end tell

When it breaks, type:

aedesc $r3

This will print out your AppleEvent. It should look something like this:

core\getd{ ----:obj { form:'prop', want:'prop', seld:'pnam', from:'null'() }, &csig:65536 }

If you get something else, just continue until you get the correct output.

Now that we have the correct call we need to convert it into a real AppleEvent using AEBuildAppleEvent()

ProcessSerialNumber psn; //psn of target app, it's your business to get it.
AppleEvent event={typeNull,0};
AEBuildError error;
NSString* sendString=@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:'null'() }";

OSStatus err = AEBuildAppleEvent('core', 'getd', typeProcessSerialNumber,
	psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID,
	 &event, &error, [sendString lossyCString]);

if (err) // print the error and where it occurs
{
	NSLog(@"%d:%d at \"%@\"",error.fError,error.fErrorPos,
[sendString substringToIndex:error.fErrorPos]);

}
 else
{	
AppleEvent reply;
err=AESend(&event,&reply,kAEWaitReply,kAENormalPriority,kAEDefaultTimeout,NULL,NULL);
AEDisposeDesc(&event); // we must dispose of this and the reply.
if (!err)
{
	// do something to get reply.
}
}

That's pretty much all there is to it.

Digg This!

 Posted by rosyna at February 10, 2003 10:00 PM

Trackback Pings:

TrackBack URL for this entry:
http://www.unsanity.org/mt-tb.cgi/17.

Listed below are links to weblogs that reference Lazy AppleScript Sending:

AppleEvents from Applescript from h4ck3r =boi
[Read More]

Tracked on December 7, 2005 8:33 AM




Related:
Comments

My AETEGizmo does much the same thing:

http://redshed.net/aeteGizmo/

It 'steals' scripting dictionaries and dumps the resulting AppleEvents. It work on the Classic Mac OS, and Mac OS X under Classic environment. It's carbonized, if there's a cry I can make it X-native.

Posted by: Jonathan 'Wolf' Rentzsch on February 11, 2003 11:47 PM

I think an OS X version of AETEGizmo would be very useful!

Posted by: on February 12, 2003 6:59 AM
Post a comment
Keep comments on topic. If a comment is unrelated to this post, it may be removed or moderated.





Remember Me?

(you may use HTML tags for style)