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

十年热恋 提交于 2020-01-13 10:16:15

问题


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?


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/22486595/how-to-get-the-pid-from-a-processserialnum-in-osx-10-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!