简体   繁体   English

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

[英]How to obtain an application's Bundle ID (or plist path) from only the PID?

Similar to "How to find Bundle Identifier from known PID?" 类似于“如何从已知的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. 以及“如何从shell脚本中读取plist信息(bundle ID) ,但是有所不同。因为它们都与Xcode构建变量扩展等有关。

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". 我的问题是,在BASH Shell中,他们唯一已知的值是进程的PID,如何获得该进程的PATH或唯一的“捆绑ID”。

I am sure there is a hideous regex to parse ps, but I'm hoping for something cleaner and more portable. 我敢肯定有一个丑陋的正则表达式可以解析ps,但我希望有一种更干净,更轻便的东西。 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.. 但是,我不认为plistbuddy不会安装在每台Mac上,更重要的是,我的问题是在理论脚本内,而不是在Xcode构建阶段内。

I've tried plutil , plistkit , and plistdump , and none of them seem to do the trick.. 我已经尝试过plutilplistkitplistdump ,但似乎没有一个能解决问题。

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. 我试图实现此目的的原因是能够执行defaults read / write功能,而无需对父进程的BundleID进行硬编码。 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. 我知道如何将此信息作为参数传递给脚本..但是我希望能够怀疑在脚本中检查..。

No need for a hideous regex to parse the output of ps – the ps utility already does what you ask of it. 不需要难看的正则表达式来解析ps的输出ps实用程序已经可以满足您的要求。 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). 会将路径返回到属于您的PID的可执行文件(仅当您输出到终端时才需要-ww参数,因为ps会截断没有路径的返回路径。否则就没有必要)。

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. 从那里获取包ID的问题在于,并非所有进程都映射到具有Info.plist(定义了包ID)的包–尤其是* nix类型的可执行文件不是包,因此没有包ID。 Also, even in the case of app bundles, ps returns the core executable of an application, not the path to the bundle itself. 同样,即使在应用程序捆绑包的情况下, ps也会返回应用程序的核心可执行文件,而不是捆绑包本身的路径。 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. 但是,由于应用程序包中可执行文件的路径是标准化的(位于Path.app/Contents/MacOS ),因此可以通过一些目录遍历来检索它。

Assuming you have stored the output of the ps call above in the variable execfile , this 假设您已将上面ps调用的输出存储在变量execfile ,这

[[ ${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. 通过使用defaults来检索每个应用程序捆绑包根目录包含的Info.plist文件的CFBundleIdentifier键的值,它将检索可能路径的捆绑包标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM