如何从OSX 10.9中的ProcessSerialNum获取PID? [英] How to get the PID from a ProcessSerialNum in OSX 10.9?

查看:92
本文介绍了如何从OSX 10.9中的ProcessSerialNum获取PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆