Wie bereits erwähnte es ganz einfach ist, andere Anwendungen mit Hilfe der NSWorkspace Klasse zu starten, zum Beispiel:
- (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];
}
Sie haben ein bisschen mehr Arbeit zu tun, um eine andere Anwendung zu beenden, vor allem wenn das Ziel vor 10,6 ist, aber es ist nicht zu schwer. Hier ein Beispiel:
- (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;
}