Modifying nested property list items with PlistBuddy

The /usr/bin/defaults command gives us basic command line access to the Mac OS X preferences system. I say ‘basic’ because it provides no facility for directly operating on any keys / values that are not at the top of the heirarchy. For example:

{8} andre@werk [~] % defaults read ComputerOptions
    ComputerListViewColumnFlags = 1; 
    ComputerSidebarWidth = 121; 
    ComputerToolbarVisible = 0; 
    ComputerUseCustomIconViewOptions = 1; 
    ComputerUseCustomListViewOptions = 1; 
    ComputerViewHeight = 385; 
    ComputerViewStyle = clmv; 
    ComputerWindowBounds = {bottom = 726; left = 1589; right = 2277; top = 341; }; 

The ComputerOptions key contains not a string or an integer, but a dict (dictionary). Note that this is different from a ‘top level’ key which is a simple key / value relationship, such as:

{9} andre@werk [~] % defaults read AppleShowAllFiles

So, what do you do when you want to change the value of, for example, the ComputerToolbarVisible attribute in the ComputerOptions dict? With /usr/bin/defaults, you’d have to read out the entire dictionary, make the change in some other mutable environment, then load the whole ComputerOptions dict back in as a plist. It works, but there’s an easier way, using a tool that’s already on your system: PlistBuddy. You’ll find this in several apple-supplied installer packages (on my system at work, I have 18 different copies of PlistBuddy scattered throughout various packages / receipts).

Here’s how you’d twiddle the ComputerToolbarVisible attribute with PlistBuddy. Note that this is all one single command.

/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy -c \\
"set ComputerOptions:ComputerToolbarVisible 1" \\

and then check the results…

{14} andre@werk [~] % defaults read ComputerOptions | grep Toolbar        
    ComputerToolbarVisible = 1; 

Execute PlistBuddy with no arguments, and then again with -h for more information.

About dre

I like all kinds of food.
This entry was posted in OS X. Bookmark the permalink.

Leave a Reply