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

5

42

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?

Ortega answered 1/12, 2010 at 19:58 Comment(0)
K
89

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.

Kippie answered 15/7, 2013 at 2:46 Comment(12)
This is the better answer (esp. since PlistBuddy is no longer default installed); you should select it.Fibrin
Also note that defaults seems to want a full path, not a relative path, not even if you're currently in the directory with the plist file you're trying to read. (Weird!)Fibrin
So, when you do it inside of some directory, you can use: defaults write $PWD/SomeMyDirectory/Info.plist CFBundleIdentifier "com.apple.Preview"Lashoh
If a script running inside a resources folder, you can do it like so: BUNDLEID=$(defaults read $(dirname $PWD)/Info CFBundleIdentifier). This is because the Info.plist is in the parent folder above the Resources folder.Iniquitous
Note, on 10.12, man defaults says: WARNING: The defaults command will be changed in an upcoming major release to only operate on preferences domains. General plist manipulation utilities will be folded into a different command-line program.Featherston
That`s awesome!Melson
Doesn't work if you want to read plist before it is packed into the app - you need to use PlistBuddy in that case. discussions.apple.com/thread/6600376Runesmith
Although this seems to be the nicest way - it doesn't work for me. I always get an error: "Domain MyApp.app/Contents/Info.plist does not exist" Although it DOES exist. It seems that defaults needs something to tell it I'm reading from a .plist and not from the defaults system.Enfeeble
plutil replaces PlistBuddyCalicut
@Fibrin More details about “PlistBuddy no longer default installed”? I searched online, and found no source verifying this claim. I only know that it is no longer in your $PATH, but one can always use the full path in the script.Epochal
@MottiShneor At least on the latest versions of macOS (tested on macOS Sonoma 14.1.2), you need to start the path with /System/Application and NOT just /Application as it is shown in the original answer. Here is an example for Apple Mail app: defaults read /System/Applications/Mail.app/Contents/Info CFBundleIdentifier. This way it works without a problem.Unpack
How do you read a nested property?Meggie
O
57

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.

Ortega answered 2/12, 2010 at 1:39 Comment(1)
PlistBuddy is not always present in the system. If you distribute the script, you may consider using defaults, as the other answers pointed.Epact
F
11

This command worked for me:

/usr/libexec/PlistBuddy -c 'print ":CFBundleIdentifier"' Info.plist
Fridafriday answered 21/5, 2019 at 12:53 Comment(0)
S
4

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.

Spire answered 1/12, 2010 at 20:58 Comment(1)
That sounds good though I haven't tested it. The drawback is that you dont get back the bundle id but only the last part of it, so you would have to hardcode the "com.mycompanyname" part somewhere...Ortega
I
3

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.

Immunoreaction answered 1/12, 2010 at 21:52 Comment(1)
I've tried PlistBuddy and played with it a lot. The problem is that PlistBuddy can print the bundle id but not return it (it only returns 0 or 1). So being a shell noob I had some trouble getting that assigned to a variable. It was however how I managed to get it to work. I'll post my answer now.Ortega

© 2022 - 2024 — McMap. All rights reserved.