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?

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? Commented Jul 5, 2014 at 8:06
  • I'm extracting them from a system processes memory. It only contains the PSN. Commented Jul 8, 2014 at 17:45
  • 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(!?!). Commented Jul 9, 2014 at 5:29
  • 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. Commented Jul 9, 2014 at 13:06
  • I think people usually use Accessibility API to find the dock icon window positions.
    – uliwitness
    Commented Apr 10, 2017 at 10:11

2 Answers 2

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

4
  • But my application starts up after these other applications have. Commented Jul 4, 2014 at 0:30
  • couldn't understand! your application starts up after these notifications have been sent? or you want ProcessSerialNumber for other applications?
    – bikram990
    Commented Jul 4, 2014 at 3:06
  • I want ProcessSerialNumber for the other applications :) Commented Jul 4, 2014 at 17:48
  • Can you please elaborate actually what you are trying to achieve by ProcessSerialNumber?
    – bikram990
    Commented Jul 5, 2014 at 5:00
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.