QuitApplicationUsingAppleEvent

last edited January 18, 2008 22:47:53 (216.221.36.73)
CocoaDev is sponsored by: Panic: Shockingly good Mac software!

Code to quit an application, identified by a bundle ID, by sending it a Quit Apple Event.

Sending a Quit Apple Event is the "friendly" way to quit an application. In response, an application may display some UI to the user such as asking him to save unsaved documents. This code executes much faster than using either NSAppleScript or NSTask to quit an application. It requires 10.3 or later.

-(OSStatus)quitApplicationWithBundleID:(NSString *)bundleID {
    OSStatus err;
    AppleEvent event, reply;

    const char *bundleIDString = [bundleID UTF8String];

    err = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication,
                            typeApplicationBundleID, 
                            bundleIDString, strlen(bundleIDString),
                            kAutoGenerateReturnID, kAnyTransactionID,
                            &event, NULL, "");

    if (err == noErr) {
        err = AESendMessage(&event, &reply, kAENoReply, kAEDefaultTimeout);
        (void)AEDisposeDesc(&event);
    }
    return err;
}

If you're actually interested in whether you successfully quit the app or not, change the kAENoReply flag to kAEWaitReply, set a saner timeout (it's in ticks, 1 tick = 1/60 second), and check if the return value is noErr or not.

If you want to force kill a process without the user having any chance to save his work, you can use the Carbon KillProcess?() or POSIX kill() APIs.

Lastly, note that it is possible for there to be more than one application running with the same bundle id. I'm not sure what happens then.

-- Please keep this article encyclopedic not in MailingListMode.