The easiest way to get the PID of a recently launched application

I want to run a file with the specified application, and I want the running program to immediately become the front-most window.

I know I can do it like this:

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

Then, if I can get the PID of this running application, I can do this to make this application the very latest:

 NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier: PID]; [app activateWithOptions: NSApplicationActivateAllWindows]; 

The question I have is this: what is the easiest, fastest and most reliable way to get this PID application right after launch, so I can make sure that this application is at the very beginning?

It is not as simple as it might seem at first glance. For example, I need a process name to get the PID using Carbon calls or through the application dictionary, accessible through NSRunningApplication . However, in the general case, I do not always know what the process name is, and in some cases the process name is an empty string.

In addition, I may have other instances of the same application that are already running, and I want to always get the PID of the specific instance of the application that I just launched.

Can anyone suggest the ultimate, 100 percent reliable way to get the current PID of the application?

Or, alternatively, is there a way to run this file with the specified application so that the application always opens as the closest application?

+4
source share
2 answers

Have you tried using another version of openFile that will allow you to deactivate your application so that the new application can focus?

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

It is definitely not easy to get the application PID. And the fact that Apple loves it .. These cheeky bastards.

I had to write this beast just to get the PID from the full path of the application that I knew was running. Hey, this is easier than ps aux parsing!

Sorry if there are some of my own personal functions, but you can understand how I did it and what I tried to avoid along the way.

 + (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 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 the same as the string passed in, lol. }]; return n ? [n processIdentifier] : 11000; // if 11000 you messed up. } 

And more ... NSLOG: Pid of /Applications/Utilities/Terminal.app is 46152

+1
source

Source: https://habr.com/ru/post/1400846/


All Articles