Apple event from scratch?

(Jean Christophe Helary) #1

How hard is it to create an apple event from scratch ?
I’m seeing this page:
and it doesn’t parse in my brain :slight_smile:

(Shane Stanley) #2

To understand them, I found the illustrations in this useful:

Although these days they’re probably more often created in Cocoa using the NSAppleEventDescriptor class. An Apple event is a special type of NSAppleEventDescriptor.

(Jean Christophe Helary) #3

The document is 11 years old. There is nothing more recent from Apple ?!?
It’s still talking about Java as being native apps…

(Shane Stanley) #4

AppleScript is 25 years old.

(Shane Stanley) #5

Also, it’s not something many people need to do.

(Peter N Lewis) #6

Here is some random code I use that creates AppleEvents:

static void AECreate( AEDesc &desc )
	desc.descriptorType = typeNull;
	desc.dataHandle = nil;

static void AEDestroy( AEDesc &desc )
	(void) AEDisposeDesc( &desc );
	AECreate( desc );

GetAppDetailsFromProcess( const pid_t pid )
	OpaqueAppDetails result = AppDetailsNull();
	AppleEvent event, reply;
	AEDesc target;
	OSStatus err;

	AECreate( reply );

	err = AECreateDesc( typeKernelProcessID, (Ptr)&pid, sizeof(pid), &target );
	verify_noerr_action( err, return result );
	err = AECreateAppleEvent( kKeyboardMaestroEngineSuite, kAEGetAppDetails, &target, kAutoGenerateReturnID, kAnyTransactionID, &event );
	verify_noerr_action( err, return result );
	AEDestroy( target );
	err = AESendMessage( &event, &reply, kAEWaitReply, 60*3 ); // 3 second timeout
	verify_noerr_action( err, return result );

	DescType descdata = typeData;
	Size datasize;
	err = AESizeOfParam( &reply, keyDirectObject, &descdata, &datasize );
	verify_noerr_action( err, return result );
	verify_action( datasize == sizeof(result), return result );
	err = AEGetParamPtr( &reply, keyDirectObject, typeData, nullptr, &result, datasize, nullptr );
	verify_noerr_action( err, return result );

	AEDestroy( event );
	AEDestroy( reply );

	return result;

Nothing has changed in AppleEvents in decades, so it makes no difference how old the docs are…

And nothing is likely ever to change in AppleEvents, except for their eventual demise.

(Shane Stanley) #7

Well, not quite. NSAppleEventDescriptor was updated to expose more stuff to Cocoa in 10.11, so you can now use something like:

NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithProcessIdentifier:pid];
NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kKeyboardMaestroEngineSuite eventID:kAEGetAppDetails targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
NSAppleEventDescriptor *reply = [event sendEventWithOptions:NSAppleEventSendWaitForReply timeout:3.0 error:&error];

(Peter N Lewis) #8

I stand corrected.

And I add characters to make it up to 20 characters. Sigh.

(Jean Christophe Helary) #9

Thank you both for the update !