我有这个任务,我需要从我的应用程序中杀死另一个我的应用程序,问题是另一个应用程序有一个终止确认对话框(没有要保存的关键数据,只有确认用户退出意图)。
在 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;
不能强行退出!!!
那你能用什么?