2012-10-16 1 views
1

J'ai cette tâche, de ma demande que je dois tuer une autre ma demande, le problème est que l'autre application a une terminaison Confirmation Dialog (il n'y a pas de données critiques à sauvegarder, seulement la confirmation de l'intention de l'utilisateur de quitter).Comment Forcez une autre application dans le cacao Mac OS X 10.5

  • Sur 10.6+ vous utiliserez:

    bool TerminatedAtLeastOne = false; 
    
    // For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method. 
    if ([NSRunningApplication respondsToSelector:@selector(runningApplicationsWithBundleIdentifier:)]) { 
        for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.company.applicationName"]) { 
         [app forceTerminate]; 
         TerminatedAtLeastOne = true; 
        } 
        return TerminatedAtLeastOne; 
    } 
    
  • mais < 10,6 ce couramment utilisé Apple Event:

    // If that didn‘t work either... then try using the apple event method, also works for OS X < 10.6. 
    AppleEvent event = {typeNull, nil}; 
    const char *bundleIDString = "com.company.applicationName"; 
    
    OSStatus result = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication, typeApplicationBundleID, bundleIDString, strlen(bundleIDString), kAutoGenerateReturnID, kAnyTransactionID, &event, NULL, ""); 
    
    if (result == noErr) { 
        result = AESendMessage(&event, NULL, kAENoReply|kAEAlwaysInteract, kAEDefaultTimeout); 
        AEDisposeDesc(&event); 
    } 
    return result == noErr; 
    

    ne peut pas forcer la fermeture !!!

Alors, que pouvez-vous utiliser?

Répondre

4

Vous pouvez utiliser ce code simple que j'ai déterré sur cocoabuilder:

// If that didn‘t work then try shoot it in the head, also works for OS X < 10.6. 
NSArray *runningApplications = [[NSWorkspace sharedWorkspace] launchedApplications]; 
NSString *theName; 
NSNumber *pid; 
for (NSDictionary *applInfo in runningApplications) { 
    if ((theName = [applInfo objectForKey:@"NSApplicationName"])) { 
     if ((pid = [applInfo objectForKey:@"NSApplicationProcessIdentifier"])) { 
      //NSLog(@"Process %@ has pid:%@", theName, pid); //test 
      if([theName isEqualToString:@"applicationName"]) { 
       kill([pid intValue], SIGKILL); 
       TerminatedAtLeastOne = true; 
      } 
     } 
    } 
} 
return TerminatedAtLeastOne; 
Questions connexes