Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.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
Objective c 获取最近启动的应用程序的PID的最简单方法_Objective C_Macos_Cocoa_Launch_Pid - Fatal编程技术网

Objective c 获取最近启动的应用程序的PID的最简单方法

Objective c 获取最近启动的应用程序的PID的最简单方法,objective-c,macos,cocoa,launch,pid,Objective C,Macos,Cocoa,Launch,Pid,我想用指定的应用程序启动一个文件,我想启动的程序立即成为最前面的窗口 我知道我可以这样做: [[NSWorkspace sharedWorkspace] openFile:fileName withApplication:appName]; 然后,如果我可以获得启动的应用程序的PID,我可以这样做,使该应用程序位于最前端: NSRunningApplication* app = [NSRunningApplication running

我想用指定的应用程序启动一个文件,我想启动的程序立即成为最前面的窗口

我知道我可以这样做:

[[NSWorkspace sharedWorkspace] openFile:fileName withApplication:appName];
然后,如果我可以获得启动的应用程序的PID,我可以这样做,使该应用程序位于最前端:

NSRunningApplication* app = [NSRunningApplication
                             runningApplicationWithProcessIdentifier: PID];
[app activateWithOptions: NSApplicationActivateAllWindows];
我的问题是:什么是最简单、最快速、最可靠的方法,可以在启动后立即获取此应用程序的PID,从而确保此应用程序位于最前端

这并不像乍一看那样直截了当。例如,我需要一个进程名,以便使用Carbon调用或通过可通过
NSRunningApplication
访问的应用程序字典获取PID。然而,在一般情况下,我并不总是知道进程名是什么,在某些情况下,进程名是一个空字符串

此外,我可能已经运行了同一应用程序的其他实例,我希望始终获得我刚刚启动的应用程序的特定实例的PID

有人能提出一个确定的、100%可靠的方法来获取当前启动的应用程序的PID吗


或者,是否有一种方法可以使用指定的应用程序启动给定的文件,使该应用程序始终作为最前面的应用程序打开?

您是否尝试过使用其他版本的openFile来停用您的应用程序,从而使新的应用程序能够聚焦

[[NSWorkspace sharedWorkspace] openFile:fileName withApplication:appName andDeactivate:YES];

获取应用程序的PID绝对不容易。这就是苹果喜欢它的方式。。那些厚颜无耻的混蛋

我必须编写这个beast,只是为了从我知道正在运行的应用程序的完整路径中获取PID。。嘿,这比解析ps aux更容易

对不起,如果有我自己的一些私人功能在那里,但你可以得到的想法,我是如何去做的,我试图避免一路上

+ (NSUInteger)pidFromAppPath:(NSString*)path
{
   NSRunningApplication *n = [[[NSWorkspace sharedWorkspace]runningApplications] filterOne:^BOOL(NSRunningApplication *runner) {
      // optional:  avoid totally faceless apps and "Desk Accesory"-type background apps.
      if (runner.activationPolicy == NSApplicationActivationPolicyProhibited) ||
          runner.activationPolicy == NSApplicationActivationPolicyAccessory) 
          return nil;
      id runPath = [runner valueForKeyPath:@"bundleURL"];
      NSString *runString = [runPath isKindOfClass:[NSString class]]
                          ? runPath
                          : [runPath isKindOfClass:NSURL.class] ? [((NSURL*)runPath) path] 
                          : nil;
     // optional: filter out Google Chrome's mockery of a once sane process-table
     if ( !runString
          || [[runString lastPathComponent]contains:@"Google Chrome Helper.app"]    
          || [[runString lastPathComponent]contains:@"Google Chrome Worker.app"]    
          || [[runString lastPathComponent]contains:@"Google Chrome Renderer.app"] ) 
     return nil;
     return  [runString isEqualToString:path] ?: nil;  // This is where you actually test to see if it's the same as the string passed in, lol.
    }];
    return  n ? [n processIdentifier] : 11000;  //  if 11000 you messed up.
}

NSLOG:Pid of/Applications/Utilities/Terminal.app是46152

我现在意识到让我的应用最前端的更可靠的方法是通过
SetFrontProcess(&psn)
,其中“psn”是启动的应用的
ProcessSerialNumber
。但是我仍然有同样的问题,因为我需要一个进程名来检索应用程序的
ProcessSerialNumber
,并且存在与我上面描述的关于如何获取应用程序PID的竞争条件相同的竞争条件。谢谢。是的,我试过了,但它并不总是让新发布的应用程序成为最前沿的应用程序。似乎我仍然需要调用
SetFrontProcess(&psn)
或其等效程序,以确保在所有情况下都会发生这种情况。