gpt4 book ai didi

cocoa - 如何在 cocoa Mac OS X 10.5 中强制终止另一个应用程序

转载 作者:行者123 更新时间:2023-12-03 16:25:14 25 4
gpt4 key购买 nike

我有这个任务,我需要从我的应用程序杀死另一个我的应用程序,问题是其他应用程序有一个终止确认对话框(没有要保存的关键数据,仅确认用户退出意图)。

  • 在 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 事件:

    // 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;

    无法强制退出!!!

那么你能用什么?

最佳答案

您可以使用我在 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;

关于cocoa - 如何在 cocoa Mac OS X 10.5 中强制终止另一个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12919389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com