How to get the PID from a ProcessSerialNum in OSX 10.9?
Asked Answered
T

2

5

GetProcessPID was marked deprecated in OSX 10.9 along with the note:

Use the processIdentifier property of the appropriate NSRunningApplication object.

The problem is the constructing class methods for NSRunningApplication do not have a way to get a NSRunningApplication by a ProcessSerialNum, only by PID or bundle name.

Bundle name is too ambiguous (there could be multiple instances) and I don't have the PID (it's what I want).

In OSX 10.9, is there a way to get the PID when you have a PSN?

Tradition answered 18/3, 2014 at 17:23 Comment(5)
I think the general approach would be to switch from whatever API is giving you a PSN to a modern one. So, how are you obtaining a PSN in the first place?Dipietro
I'm extracting them from a system processes memory. It only contains the PSN.Tradition
That raises the question of why you're doing that. If you explain what you're trying to accomplish at a high level, maybe there's a solution which doesn't involve poking around in the memory of system processes(!?!).Dipietro
I want the UILayer that the Dock displays each icon with and the ability to associate it back with the process. Because this application obviously violates unpublished APIs in general, there's no problem using unpublished APIs to resolve the PSN to the PID. It was largely a curiosity question because it appears Apple is dropping a lot of APIs prematurely.Tradition
I think people usually use Accessibility API to find the dock icon window positions.Buchanan
D
3

Observe the NSWorkspaceDidLaunchApplicationNotification notification.

In the callback, get the process serial number as follows:

NSDictionary* dictionary = [notification userInfo];
NSNumber* psnLow = [dictionary valueForKey: @"NSApplicationProcessSerialNumberLow"];
NSNumber* psnHigh = [dictionary valueForKey: @"NSApplicationProcessSerialNumberHigh"];
ProcessSerialNumber psn;
psn.highLongOfPSN = [psnHigh intValue];
psn.lowLongOfPSN = [psnLow intValue];
NSRunningApplication *newApplication = [dictionary valueForKey:NSWorkspaceApplicationKey];

source

Downstroke answered 3/7, 2014 at 5:19 Comment(4)
But my application starts up after these other applications have.Tradition
couldn't understand! your application starts up after these notifications have been sent? or you want ProcessSerialNumber for other applications?Downstroke
I want ProcessSerialNumber for the other applications :)Tradition
Can you please elaborate actually what you are trying to achieve by ProcessSerialNumber?Downstroke
O
2

If you use the method runningApplicationsWithBundleIdentifier of the class NSRunningApplication, you will get an NSArray of NSRunningApplication. You may then read these objects' properties (bundle URL, localized name…) to identify the object you are interested in, at last get its PID.

Overleap answered 5/7, 2014 at 5:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.