My Cocoa application needs to start and terminate other applications. Please let me know of any example code which can do the following:
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With