3

私はこのタスクを実行しました。自分のアプリケーションから別のアプリケーションを強制終了する必要があります。問題は、他のアプリケーションに終了確認ダイアログがあることです(保存する重要なデータはなく、ユーザーが終了する意思を確認するだけです)。

  • 10.6以降では、以下を使用します。

    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;
    }
    
  • しかし、<10.6では、この一般的に使用される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;
    

    強制終了できません!!!

それで、あなたは何を使うことができますか?

4

1 に答える 1

8

あなたは私が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;
于 2012-10-16T16:35:33.600 に答える