在OSX 10.9中将GetProcessPID标记为已弃用以及注释:
@H_403_2@
Use the processIdentifier property of the appropriate
NSRunningApplication object.
问题是NSRunningApplication的构造类方法无法通过ProcessSerialNum获取NSRunningApplication,只能通过PID或包名称获取.
捆绑名称太模糊(可能有多个实例),我没有PID(这是我想要的).
在OSX 10.9中,有没有办法在你有PSN时获得PID?
解决方法
观察NSWorkspaceDidLaunchApplicationNotification通知.
在回调中,获取进程序列号如下:
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];