As mentioned earlier, it is quite simple to launch other applications using 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 need to do a little more work to exit another application, especially if the goal is before 10.6, but it is not so difficult. 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; }
Fredric
source share