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?

animuson
  • 53,861
  • 28
  • 137
  • 147
Dimitris
  • 13,480
  • 17
  • 74
  • 94

5 Answers5

80

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.

joemaller
  • 19,579
  • 7
  • 67
  • 84
  • 3
    This is the better answer (esp. since PlistBuddy is no longer default installed); you should select it. – Olie Sep 22 '14 at 17:34
  • 26
    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!) – Olie Sep 22 '14 at 17:50
  • 8
    So, when you do it inside of some directory, you can use: defaults write $PWD/SomeMyDirectory/Info.plist CFBundleIdentifier "com.apple.Preview" – iVader Jul 31 '15 at 13:22
  • 4
    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. – Volomike Dec 03 '15 at 04:28
  • 4
    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. – Ben Lings Jul 04 '17 at 07:48
  • That`s awesome! – Beyond Chao Jan 16 '18 at 08:02
  • Doesn't work if you want to read plist before it is packed into the app - you need to use PlistBuddy in that case. https://discussions.apple.com/thread/6600376 – Matej Ukmar Feb 13 '18 at 14:35
  • 2
    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. – Motti Shneor May 15 '18 at 13:08
  • plutil replaces PlistBuddy – malhal May 01 '22 at 15:38
  • @Olie 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. – Franklin Yu Aug 07 '22 at 01:31
54

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.

Dimitris
  • 13,480
  • 17
  • 74
  • 94
  • 1
    PlistBuddy is not always present in the system. If you distribute the script, you may consider using defaults, as the other answers pointed. – Boris Vidolov Dec 23 '14 at 22:44
9

This command worked for me:

/usr/libexec/PlistBuddy -c 'print ":CFBundleIdentifier"' Info.plist
radrow
  • 6,419
  • 4
  • 26
  • 53
djonik1562
  • 91
  • 1
  • 3
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.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • 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... – Dimitris Dec 02 '10 at 01:33
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.

donarb
  • 184
  • 1
  • 6
  • 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. – Dimitris Dec 02 '10 at 01:35