我有一个AppleEventDescriptor,在这里我需要获取发送应用程序的包标识符。Apple包含一个typeProcessSerialNumber,它可以被强制转换为一个ProcessSerialNumber。
问题是,GetProcessPID()在10.9中被否决了,而且似乎没有被批准的方法来获得一个可以用来使用-runningApplicationWithProcessIdentifier:实例化NSRunningApplication的pid_t。
我发现的所有其他选项都在Processes.h中,并且也被否决了。
我是错过了什么,还是必须忍受这个贬义警告?
发布于 2014-06-24 23:50:30
布莱恩和丹尼尔都提供了很好的线索,帮助我找到了正确的答案,但他们提出的建议只是有点离谱。这就是我最后是如何解决问题的。
Brian对于获取进程id而不是序列号的Apple事件描述符的代码是正确的:
// get the process id for the application that sent the current Apple Event
NSAppleEventDescriptor *appleEventDescriptor = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];
NSAppleEventDescriptor* processSerialDescriptor = [appleEventDescriptor attributeDescriptorForKeyword:keyAddressAttr];
NSAppleEventDescriptor* pidDescriptor = [processSerialDescriptor coerceToDescriptorType:typeKernelProcessID];问题是,如果从描述符中提取-int32Value,则返回值为0(即不返回进程id)。我不知道为什么会发生这种情况:理论上,pid_t和SInt32都是有符号整数。
相反,您需要获取字节值(这些字节值存储为小endian),并将它们转换为进程id:
pid_t pid = *(pid_t *)[[pidDescriptor data] bytes];从这一点出发,获取有关正在运行的进程的信息非常简单:
NSRunningApplication *runningApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
NSString *bundleIdentifer = [runningApplication bundleIdentifier];而且,Daniel关于使用keySenderPIDAttr的建议在很多情况下都行不通。在我们新的沙箱世界中,存储在其中的值很可能是/usr/libexec/lsboxd的进程id,也称为启动服务沙箱守护进程,而不是发起事件的应用程序的进程id。
再次感谢布莱恩和丹尼尔的帮助,导致这一解决方案!
发布于 2014-06-24 19:48:09
您可以使用Apple描述符强制将ProcessSerialNumber描述符转换为pid_t描述符,如下所示:
NSAppleEventDescriptor* processSerialDescriptor = [myEvent attributeDescriptorForKeyword:keyAddressAttr];
NSAppleEventDescriptor* pidDescriptor = [processSerialDescriptor coerceToDescriptorType:typeKernelProcessID];
pid_t pid = [pidDescriptor int32Value];https://stackoverflow.com/questions/24394783
复制相似问题