gpt4 book ai didi

objective-c - 使用 ProcessSerialNumber 获取 NSRunningApplication

转载 作者:太空狗 更新时间:2023-10-30 03:36:37 30 4
gpt4 key购买 nike

我有一个 AppleEventDescriptor,我需要在其中获取发送应用程序的包标识符。 Apple 事件包含一个 typeProcessSerialNumber,可以将其强制转换为 ProcessSerialNumber

问题是 GetProcessPID() 在 10.9 中被弃用了,并且似乎没有被批准的方法来获取可用于实例化的 pid_t NSRunningApplication 使用 -runningApplicationWithProcessIdentifier:

我发现的所有其他选项都存在于 Processes.h 中,并且也已弃用。

我是否遗漏了什么,或者我是否必须接受这个弃用警告?

最佳答案

Brian 和 Daniel 都提供了很好的线索,帮助我找到了正确的答案,但他们建议的内容有点不对劲。这是我最终解决问题的方法。

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_tSInt32 是有符号整数。

相反,您需要获取字节值(存储在小端)并将它们转换为进程 ID:

pid_t pid = *(pid_t *)[[pidDescriptor data] bytes];

从那时起,获取有关正在运行的进程的信息就很简单了:

NSRunningApplication *runningApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
NSString *bundleIdentifer = [runningApplication bundleIdentifier];

此外,Daniel 关于使用 keySenderPIDAttr 的建议在很多情况下都行不通。在我们新的沙盒世界中,存储在那里的值可能是 /usr/libexec/lsboxd 的进程 ID,也称为 Launch Services 沙箱守护进程,而不是发起的应用程序的进程 ID事件。

再次感谢 Brian 和 Daniel 提供的帮助促成了这个解决方案!

关于objective-c - 使用 ProcessSerialNumber 获取 NSRunningApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24394783/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com