Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 574
Joseph Lennox Avatar asked Oct 01 '22 06:10

Joseph Lennox


1 Answers

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

like image 75
bikram990 Avatar answered Oct 12 '22 22:10

bikram990