2011-01-07 6 views

Antwort

3

dies für 10,6 zielt Unter der Annahme, können Sie NSRunningApplication zusammen mit NSWorkspace verwenden. Zunächst sollten Sie bestimmen, ob die Anwendung bereits läuft mit:

[[NSWorkspace sharedWorkspace] runningApplications] 

Wenn es nicht läuft, dann können Sie es starten NSWorkspace, aber ich empfehle die neueren Anruf, launchApplicationAtURL:options:configuration:error:, die ein NSRunningApplication zurückkehren, die Sie kann zum Beenden der Anwendung verwenden. Weitere Informationen finden Sie unter NSWorkspace.

7

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; 
} 
Verwandte Themen