How to write a plist with nested dictionaries from bash script

evangardner
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.
2de270f0b3e5415994eb21cf32f2a834

5 REPLIES 5

Look
Valued Contributor III

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

donmontalvo
Esteemed Contributor III

NM

--
https://donmontalvo.com

tlarkin
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.

mm2270
Legendary Contributor III

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: https://scriptingosx.com/2016/11/editing-property-lists/
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.

ryan_ball
Valued Contributor

@evangardner Show us the actual xml.