Source

CustomReturnID / AppDelegate.m

Full commit
#import "AppDelegate.h"


@implementation AppDelegate

- (NSAppleEventDescriptor *)finderDescriptor
{
    NSData * data = [@"com.apple.finder" dataUsingEncoding:NSUTF8StringEncoding];
    NSAppleEventDescriptor * finder = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID
                                                                                      data:data];
    return finder;
}

- (void)sendWithReturnID:(AEReturnID)returnID
{
    NSAppleEventDescriptor * finder = [self finderDescriptor];
    NSAppleEventDescriptor * event = [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass
                                                                              eventID:kAEOpenApplication
                                                                     targetDescriptor:finder
                                                                             returnID:returnID
                                                                        transactionID:kAnyTransactionID];
    NSLog(@"Sending event with returnID: %d (specified as: %d)", [event returnID], returnID);
    
    AppleEvent aeReply;
    OSStatus err = AESendMessage([event aeDesc], &aeReply, kAEWaitReply, kAEDefaultTimeout);
    if (err != noErr)
    {
        NSLog(@"AESendMessage failed: %s: %s", GetMacOSStatusErrorString(err), GetMacOSStatusCommentString(err));
    }
    else
    {
        NSAppleEventDescriptor * reply = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&aeReply];
        [reply autorelease];
        NSLog(@"Reply has returnID: %d", [reply returnID]);
    }
}

- (IBAction)sendWithAutoGeneratedReturnID:(id)sender;
{
    [self sendWithReturnID:kAutoGenerateReturnID];
}

- (IBAction)sendWithCustomReturnID:(id)sender;
{
    static AEReturnID nextReturnID = 42;
    [self sendWithReturnID:nextReturnID++];
}

@end