Anisha Saigal
Anisha Saigal

Reputation: 171

How to start and terminate an application from inside a cocoa application on Mac

My Cocoa application needs to start and terminate other applications. Please let me know of any example code which can do the following:

  1. Start an application from inside Cocoa code
  2. Terminate an application from inside Cocoa code

Upvotes: 8

Views: 5949

Answers (3)

Fredric
Fredric

Reputation: 1223

As previously mentioned it‘s quite easy to launch other applications with the help of the NSWorkspace class, for example:

- (BOOL)launchApplicationWithPath:(NSString *)path
{
    // As recommended for OS X >= 10.6.
    if ([[NSWorkspace sharedWorkspace] respondsToSelector:@selector(launchApplicationAtURL:options:configuration:error:)])
        return nil != [[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:path isDirectory:NO] options:NSWorkspaceLaunchDefault configuration:nil error:NULL];

    // For older systems.
    return [[NSWorkspace sharedWorkspace] launchApplication:path];
}

You have to do a bit more work in order to quit another application, especially if the target is pre-10.6, but it‘s not too hard. Here is an example:

- (BOOL)terminateApplicationWithBundleID:(NSString *)bundleID
{
    // For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method.
    if ([[NSWorkspace sharedWorkspace] respondsToSelector:@selector(runningApplications)])
        for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications])
            if ([bundleID isEqualToString:[app bundleIdentifier]])
                return [app terminate];

    // If that didn‘t work then try using the apple event method, also works for OS X < 10.6.

    AppleEvent event = {typeNull, nil};
    const char *bundleIDString = [bundleID UTF8String];

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

    if (result == noErr) {
        result = AESendMessage(&event, NULL, kAEAlwaysInteract|kAENoReply, kAEDefaultTimeout);
        AEDisposeDesc(&event);
    }
    return result == noErr;
}

Upvotes: 7

Rob Napier
Rob Napier

Reputation: 299345

Assuming this is targeted for 10.6, you can use NSRunningApplication along with NSWorkspace. First, you should determine if the application is already running using:

[[NSWorkspace sharedWorkspace] runningApplications]

If it's not running, then you can launch it using NSWorkspace, but I recommend the newer call, launchApplicationAtURL:options:configuration:error:, which will return an NSRunningApplication, which you can use to terminate the application. See NSWorkspace for more details.

Upvotes: 3

FeifanZ
FeifanZ

Reputation: 16316

To launch an app:

[[NSWorkspace sharedWorkspace] launchApplication:@"App"];

From http://forums.macnn.com/79/developer-center/134947/launch-another-application-from-cocoa/

To quit:

NSApplication has a -terminate: method: [NSApp terminate: nil];

From How can I tell my Cocoa application to quit from within the application itself?

Upvotes: 2

Related Questions