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

和自甴很熟 提交于 2019-12-05 10:48:31

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

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.

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