两个布赖恩和丹尼尔提供了极大的线索,帮助我找到正确的答案,但东西,他们建议只是有点关闭。以下是我最终解决问题的方法。
布赖恩是正确的有关代码以获取一个进程ID,而不是一个序列号的苹果事件描述:
// 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];
的问题是,如果从描述符采取0,一值0返回(即没有进程ID)。我不知道为什么会发生这种情况:理论上,pid_t
和SInt32
都是有符号整数。
相反,你需要得到字节值(存储小端)扔一个进程ID:
pid_t pid = *(pid_t *)[[pidDescriptor data] bytes];
从这一点来说,这是简单的,以获取有关正在运行的进程的信息:
NSRunningApplication *runningApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
NSString *bundleIdentifer = [runningApplication bundleIdentifier];
此外,丹尼尔的建议使用keySenderPIDAttr
将在许多情况下不起作用。在我们的新沙箱世界中,存储的值可能是/usr/libexec/lsboxd
(也称为Launch Services沙箱守护程序)的进程ID,而不是发起该事件的应用程序的进程ID。
再次感谢Brian和Daniel提供的解决方案!
或者,您可以使用keySenderPIDAttr在没有查找发件人并强制它的情况下获取PID:[[event attributeDescriptorForKeyword:keySenderPIDAttr] int32Value] – danielpunkass