Apple event from scratch?

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:

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.

1 Like

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

AppleScript is 25 years old.


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

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.

1 Like

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];
1 Like

I stand corrected.

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

1 Like

Thank you both for the update !