Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何强制杀死Mac OS X 10.5中的另一个应用程序_Cocoa_Kill_Sigkill_Nsworkspace_Appleevents - Fatal编程技术网

Cocoa 如何强制杀死Mac OS X 10.5中的另一个应用程序

Cocoa 如何强制杀死Mac OS X 10.5中的另一个应用程序,cocoa,kill,sigkill,nsworkspace,appleevents,Cocoa,Kill,Sigkill,Nsworkspace,Appleevents,我有这个任务,从我的应用程序我需要杀死另一个我的应用程序,问题是另一个应用程序有一个终止确认对话框(没有要保存的关键数据,只有用户退出意图的确认) 在10.6+上,您将使用: bool TerminatedAtLeastOne = false; // For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method. if ([NSRunningApplication respondsToSelector:@se

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

  • 在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;
    }
    

  • 但在上,您可以使用我在cocoabuilder上挖掘的以下简单代码:

    //如果这不起作用,那么试着用枪打它的头部,这也适用于OSX<10.6。
    NSArray*运行应用程序=[[NSWorkspace sharedWorkspace]启动应用程序];
    NSString*名称;
    NSNumber*pid;
    用于(运行应用程序中的NSDictionary*applInfo){
    if((名称=[applInfo objectForKey:@“NSApplicationName”])){
    if((pid=[applInfo objectForKey:@“NSApplicationProcessIdentifier”])){
    //NSLog(@“进程%@具有pid:%@”,名称为pid);//测试
    如果([theName IsequalString:@“applicationName”]){
    kill([pid intValue],SIGKILL);
    TerminatedAtLeastOne=真;
    }
    }
    }
    }
    返回终止符;
    
    // 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;
    
    // 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;