|
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. Trackback Pings: TrackBack URL for this entry: Listed below are links to weblogs that reference Lazy AppleScript Sending: AppleEvents from Applescript from h4ck3r =boi Tracked on December 7, 2005 8:33 AM Related:
Comments
My AETEGizmo does much the same thing: 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 PMKeep comments on topic. If a comment is unrelated to this post, it may be removed or moderated. |

