How to write a plist with nested dictionaries from bash script

New Contributor

I'm trying to write a plist from a bash script that has multiple nested dictionaries (dictionary within a dictionary within an array for example).

Right now I'm using the defaults command, which works for normal keys and dictionaries/arrays, but doesn't seem to have support for nested dictionaries/arrays. I need the script to run independently on any mac, so I can't use third party tools that don't come preinstalled on MacOS.
The structure I want to achieve is similar to this image.


Valued Contributor III

Google using /usr/libexec/PlistBuddy it's in there but hidden away and pretty sure it can do nested stuff.

Esteemed Contributor II



Honored Contributor

PlistBuddy is a plist editor binary that can do nested values, but PlistBuddy is not cfprefsd aware, meaning you will have to kill the app after changing it for the changes to take effect. Just so you don't lose sleep over this small difference thought I would post it.

Legendary Contributor II

It's not well known (I only discovered it recently), but plutil is able to add or insert various values into plist, including adding arrays and entries to those. Here's a page that discusses using it that might help:
They even suggest that it's a good potential replacement to PlistBuddy.

Edit: Just wanted to add that plutil also may not be cfprefsd aware, so it could require restarting cfprefsd to get the altered values read in.

Valued Contributor

@evangardner Show us the actual xml.