如何仅从 PID 获取应用程序的 Bundle ID(或 plist 路径)?
类似于“如何从已知的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 构建阶段
。尝试了 plutil
、plistkit
和 plistdump
,但它们似乎都不起作用。
我试图实现这一目标的原因是能够执行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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要可怕的正则表达式来解析 ps 的输出 - ps 实用程序已经完成了您所要求的工作。使用以下选项调用它(诚然有点神秘)
将返回属于您的 PID 的可执行文件的路径(仅当输出到终端时才需要
-ww
参数,如ps
将截断返回的路径(没有它)。从那里检索捆绑包 ID 的问题在于,并非所有进程都映射到具有 Info.plist(定义捆绑包 ID)的捆绑包 - 值得注意的是,*nix 类型的可执行文件不是捆绑包,因此没有捆绑包 ID。此外,即使在应用程序捆绑包的情况下,
ps
也会返回应用程序的核心可执行文件,而不是捆绑包本身的路径。然而,由于应用程序包内的可执行文件的路径是标准化的(它驻留在Path.app/Contents/MacOS
中),您可以通过一些目录遍历来检索它。假设您已将上面
ps
调用的输出存储在变量execfile
中,这将通过使用
defaults
来检索可能路径的包标识符每个应用程序包根目录中包含的Info.plist
文件的CFBundleIdentifier
键的值。No need for a hideous regex to parse the output of
ps
– theps
utility already does what you ask of it. Calling it with the (admittedly somewhat arcane looking) following optionswill return the path to the executable belonging to your PID (the
-ww
argument is only needed if you output to a terminal, asps
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 inPath.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 variableexecfile
, thiswill retrieve bundle identifiers for likely paths by using
defaults
to retrieve the value of theCFBundleIdentifier
key of theInfo.plist
file every application bundle contains at its root.