modifying a Plist from command line on Mac using Defaults
Asked Answered
A

4

29

Does any one know how to modify a Plist file from command line using defaults? Currently there are two Dictionaries under the URL types array; I need to add another.

enter image description here

Every command i've tried have either replaced the entire dictionary, or created a new array called URL types instead of editing it. Any ideas of how this can be done in defaults (the console Mac app) and not PlistBuddy?

Antipater answered 6/12, 2012 at 9:15 Comment(2)
$ defaults write domain key 'value', where domain relates to the app. developer.apple.com/library/mac/#documentation/Darwin/Reference/…Plausive
FYI (not really an answer to your question): Mac OS X Prefs Editor - A GUI for the 'defaults' commandThorpe
F
19

Open the Info.plist in a text editor to see the actual identifiers.

defaults write Absolute/Path/to/Info.plist CFBundleURLTypes -array-add '<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>Mac App Store URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>macappstore</string>
</array>
</dict>'

pbpaste | pl converts the XML to the old-style format.

defaults write Info.plist CFBundleURLTypes -array-add '{CFBundleTypeRole=Viewer; FBundleURLName="Mac App Store URL";CFBundleURLSchemes=(macappstore);}'

Figurehead answered 6/12, 2012 at 15:3 Comment(4)
man! this had me so confused. XCode changes the names of the keys to the values you see displayed in the image above (in my question). So thats why it was adding new entries. thanks dude.Antipater
You can also display the text representation of the plist in XCode by right clicking the plist in the Project Navigator and selecting "Open As > Source Code" (much quicker than opening the file externally...)Lekishalela
It is extremely important that the path provided to the plist file has to be an absolute one. If it is relative like in this example, you'll end up creating or modifying ~/Library/Preferences/Info.plistOath
Absolute paths are critical as derFunk mentioned.Bucella
P
31

XML property lists can be viewed in a text editor directly as Lauri's answer above suggests.

Binary property lists (found in many of Apple's own shipping applications) need to be converted to an XML property list format first.

plutil may be used to do this, in either direction. Take care though as the property list is modified in place, so you make wish to make a copy of the property list first.

plutil -convert xml1 binary-property-list-to-convert.plist

And to convert it back to binary:

plutil -convert binary1 XML-property-list-to-convert.plist
Puffery answered 31/10, 2013 at 17:19 Comment(0)
F
19

Open the Info.plist in a text editor to see the actual identifiers.

defaults write Absolute/Path/to/Info.plist CFBundleURLTypes -array-add '<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>Mac App Store URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>macappstore</string>
</array>
</dict>'

pbpaste | pl converts the XML to the old-style format.

defaults write Info.plist CFBundleURLTypes -array-add '{CFBundleTypeRole=Viewer; FBundleURLName="Mac App Store URL";CFBundleURLSchemes=(macappstore);}'

Figurehead answered 6/12, 2012 at 15:3 Comment(4)
man! this had me so confused. XCode changes the names of the keys to the values you see displayed in the image above (in my question). So thats why it was adding new entries. thanks dude.Antipater
You can also display the text representation of the plist in XCode by right clicking the plist in the Project Navigator and selecting "Open As > Source Code" (much quicker than opening the file externally...)Lekishalela
It is extremely important that the path provided to the plist file has to be an absolute one. If it is relative like in this example, you'll end up creating or modifying ~/Library/Preferences/Info.plistOath
Absolute paths are critical as derFunk mentioned.Bucella
S
9

OSX has PlistBuddy, which makes this a lot simpler.

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.html

See also: https://github.com/kevinSuttle/OSXDefaults/blob/master/REFERENCE.md

Savdeep answered 6/12, 2013 at 18:28 Comment(1)
I used this command: /usr/libexec/PlistBuddy -c "set CFBundleURLTypes:CFBundleURLSchemes fb$FacebookAppID" But it is not work. what should i doTroth
R
6

Use the -array-add value type:

defaults write /path/to/plist/file "URL Types" -array-add '{"URL Identifier" = "com.myapp.2"; "URL Schemes" = { "two"; }; }'
Racing answered 6/12, 2012 at 14:50 Comment(1)
I'm on macOS Catalina (10.15) and it seems defaults no longer works with file paths. In the man it 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."Definition

© 2022 - 2024 — McMap. All rights reserved.