如何仅从 PID 获取应用程序的 Bundle ID(或 plist 路径)?

发布于 2024-12-26 05:28:08 字数 877 浏览 0 评论 0原文

类似于“如何从已知的PID中查找捆绑包标识符?”“如何阅读plist 信息(bundle id)来自 shell 脚本,但不同..因为它们都与 Xcode 构建变量扩展等相关。

我的问题是,在 BASH shell 中,它们只知道值是进程的 PID,如何获取该进程的路径,或唯一的“捆绑包 ID”。

我确信有一个可怕的正则表达式来解析 ps,但我希望更清洁、更便携。 然而,我不认为每台 Mac 上都

BUNDLE_ID=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${BUILD_ROOT}/${INFOPLIST_PATH}")

安装了 plistbuddy,更重要的是,我的问题是在理论脚本中,而不是在 Xcode 构建阶段

。尝试了 plutilplistkitplistdump,但它们似乎都不起作用。

我试图实现这一目标的原因是能够执行defaults read /编写函数,无需硬编码父进程的BundleID。我知道如何将此信息作为参数传递给脚本..但我希望能够在脚本内怀疑检查..。

Similar to "How to find Bundle Identifier from known PID?" and to "How to read plist information (bundle id) from a shell script, but different.. as those are both related to Xcode build variable expansion, etc.

My question is how, in a BASH shell, where they only known value is the process' PID, how can one obtain that process' PATH, or unique "Bundle ID".

I am sure there is a hideous regex to parse ps, but I'm hoping for something cleaner and more portable. The comments in those prior mentioned posts included

BUNDLE_ID=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${BUILD_ROOT}/${INFOPLIST_PATH}")

However, I do not think plistbuddy is installed on every Mac, and more importantly, my question is within a theoretical script, NOT within an Xcode build phase..

I've tried plutil, plistkit, and plistdump, and none of them seem to do the trick..

The reason I am trying to achieve this is to be able to execute defaults read / write functions without hardcoding the BundleID of the parent process. I know how to pass this info as an argument to a script.. but I want to be able to doubt check.. within the script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2025-01-02 05:28:09

不需要可怕的正则表达式来解析 ps 的输出 - ps 实用程序已经完成了您所要求的工作。使用以下选项调用它(诚然有点神秘)

ps [-ww] -o comm= -p <pid>

将返回属于您的 PID 的可执行文件的路径(仅当输出到终端时才需要 -ww 参数,如 ps 将截断返回的路径(没有它)。

从那里检索捆绑包 ID 的问题在于,并非所有进程都映射到具有 Info.plist(定义捆绑包 ID)的捆绑包 - 值得注意的是,*nix 类型的可执行文件不是捆绑包,因此没有捆绑包 ID。此外,即使在应用程序捆绑包的情况下,ps也会返回应用程序的核心可执行文件,而不是捆绑包本身的路径。然而,由于应用程序包内的可执行文件的路径是标准化的(它驻留在 Path.app/Contents/MacOS 中),您可以通过一些目录遍历来检索它。

假设您已将上面 ps 调用的输出存储在变量 execfile 中,这

[[ ${execfile%/*} =~ ^.+/Contents/MacOS$ ]] && defaults read "${execfile%/*/*}"/Info CFBundleIdentifier

将通过使用 defaults 来检索可能路径的包标识符每个应用程序包根目录中包含的 Info.plist 文件的 CFBundleIdentifier 键的值。

No need for a hideous regex to parse the output of ps – the ps utility already does what you ask of it. Calling it with the (admittedly somewhat arcane looking) following options

ps [-ww] -o comm= -p <pid>

will return the path to the executable belonging to your PID (the -ww argument is only needed if you output to a terminal, as ps will truncate the returned path without it. It should not be necessary otherwise).

The problem with retrieving a bundle ID from there is that not all processes map to bundles with an Info.plist (which defines the bundle ID) – notably, *nix type executables are not bundles and thus have no bundle ID. Also, even in the case of app bundles, ps returns the core executable of an application, not the path to the bundle itself. As the path to the executable inside an application bundle is standardized, however (it resides in Path.app/Contents/MacOS), you can retrieve it with a bit of directory traversal.

Assuming you have stored the output of the ps call above in the variable execfile, this

[[ ${execfile%/*} =~ ^.+/Contents/MacOS$ ]] && defaults read "${execfile%/*/*}"/Info CFBundleIdentifier

will retrieve bundle identifiers for likely paths by using defaults to retrieve the value of the CFBundleIdentifier key of the Info.plist file every application bundle contains at its root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文