OSX: Dealing with property list files

During my last CareerBuilder Hackathon, I worked finished a project that will incredibly help our growing tech teams. If you ever worked in a consulting company, you know how painful it is to wait to gather server credentials and set up them on your machine to be fully operational.

My project is very simple but damn useful: it generates several type of configuration directly from our platform API. You need to have SSH access to servers or connect to database servers? No problems, you provide your SSH user/key path and I take care of generating the file for you according to your tech user profile.

Then, from 2-3 commands, you have your machine ready to connect to all the servers you will work with. Useful right?

Ironing out .plist files

I used to use the defaults command to hack into my OSX but when it comes to do more complex operations, the defaults command isn't the right tool.

PlistBuddy allows to create dictionnaries, remove, append nodes, etc. Even with this tool, it's not very easy to handle plist trees but it's designed to.

If installed, the plistbuddy command is probaly not available in your $PATH and requires to add the path of target file at the end of each command which could add a lot of noise in your commands if the file is deeply hidden in the OS. That's why, it's better to use variables for the current scope:

If you load this favorite plist file in Sequel Pro, you'll have nothing shown into the FAVORITES section since the Local dictionary in laying at the plist file root instead of being in the Favorite Root/Childrenarray.

I will move it later. First I need to fill the Local folder with the server favorites. But why?
After a few tries, I found out that it was very hard to add items way down a tree than building a sub-tree then appending it at the right place once it's done. Trying to iterate over Favorite Root/Childrenarray keys didn't work out...

Server favorite creation

For the same reason as above, a server favorite tree won't be created directly into the Local/Childrenarray but at the root of the file:

About the random number, I couldn't find what this number should be in the Sequel Pro plist file, I believe it's a sort of unique ID but no way to figure out how or what it's generated from; even after digging into Sequel Pro's source code.

Replace the variables (surrounded by %) by your values. I didn't really figure out the -a (account) option of the subcommand because in some case, if you do SSH tunneling, the -a option value becomes -a @127.0.0.1/.
You might wanna play with it to guess what's best for your configuration needs.

Beware of the -s option value format. Apparently, this is strict enough. When I tried to remove the space before the colon, the Keychain entry wasn't recognized by Sequel Pro anymore!

Well, that's all folks. Now you can play around with plist files and do much more than flag switching using defaults.