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?

bikram990
  • 1,085
  • 1
  • 14
  • 36
Joseph Lennox
  • 3,202
  • 1
  • 27
  • 25
  • 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? – Ken Thomases Jul 05 '14 at 08:06
  • I'm extracting them from a system processes memory. It only contains the PSN. – Joseph Lennox Jul 08 '14 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(!?!). – Ken Thomases Jul 09 '14 at 05: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. – Joseph Lennox Jul 09 '14 at 13:06
  • I think people usually use Accessibility API to find the dock icon window positions. – uliwitness Apr 10 '17 at 10:11

2 Answers2

2

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

bikram990
  • 1,085
  • 1
  • 14
  • 36
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.

rems4e
  • 3,112
  • 1
  • 17
  • 24