如何从OSX 10.9中的ProcessSerialNum获取PID? [英] How to get the PID from a ProcessSerialNum in OSX 10.9?
问题描述
GetProcessPID
在OSX 10.9中已与注释一起标记为不推荐使用:
GetProcessPID
was marked deprecated in OSX 10.9 along with the note:
使用适当的processIdentifier属性 NSRunningApplication对象.
Use the processIdentifier property of the appropriate NSRunningApplication object.
问题是NSRunningApplication
的构造类方法没有方法只能通过PID或捆绑软件名称来通过ProcessSerialNum
获取NSRunningApplication
.
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.
捆绑名称太含糊(可能有多个实例),而且我没有PID(这就是我想要的).
Bundle name is too ambiguous (there could be multiple instances) and I don't have the PID (it's what I want).
在OSX 10.9中,有PSN时是否可以获取PID?
In OSX 10.9, is there a way to get the PID when you have a PSN?
推荐答案
观察NSWorkspaceDidLaunchApplicationNotification
通知.
在回调中,获取进程序列号,如下所示:
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];
这篇关于如何从OSX 10.9中的ProcessSerialNum获取PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!