我有一个任务,需要从我的应用程序中“结束”另一个应用程序。问题是,另一个应用程序会弹出一个“终止确认对话框”(没有重要数据需要保存,只是确认用户意图退出)。
On 10.6+ you will use:
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; }
but on <10.6 this commonly used 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;
can't Force Quit!!!