How to read plist information (bundle id) from a shell script

爷,独闯天下 提交于 2019-12-17 22:45:38

问题


I'd like to write a script that can read info like Bundle Identifier or maybe version number from the Info.plist of the app. Xcode doesn't seem to give that information in it's environment variables. Is there any other way to get them in sh/bash?


回答1:


The defaults command can read/write to any plist file, just give it a path minus the .plist extension:

$ defaults read /Applications/Preview.app/Contents/Info CFBundleIdentifier

com.apple.Preview

This pulls the CFBundleIdentifier value directly from the application bundle's Info.plist file.

Defaults also works with binary plists without any extra steps.




回答2:


Using PlistBuddy, an app by Apple it is possible to assign the string to var like this:

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

Where BUILD_ROOT and INFOPLIST_PATH are variables set by Xcode if you run this script in a "Run Script" build phase.




回答3:


You can just read the file directly from the built product. However, if you look at the info.plist file itself in the editor you will see the shell variables themselves. E.g. the Bundle ID is has the following shell command:

com.yourcompany.${PRODUCT_NAME:rfc1034identifier}

You can call ${PRODUCT_NAME:rfc1034identifier} in any shell script that Xcode runs and it should populate.




回答4:


There is a command line program installed on the Mac called PlistBuddy that can read/write values in a plist. Type 'man PlistBuddy' in Terminal to get more info.




回答5:


This command worked for me:

/usr/libexec/PlistBuddy -c 'print ":CFBundleIdentifier"' Info.plist


来源:https://stackoverflow.com/questions/4328501/how-to-read-plist-information-bundle-id-from-a-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!