Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i parse the output of `defaults read` on OS X?

how can i parse the output of the OS X defaults read terminal command?

it appears to output the 'old' NeXTSTEP plist format; things that look like:

{
"Apple Global Domain" =     {
    AppleAntiAliasingThreshold = 4;
    AppleCollationOrder = root;

i tried writing the output to a file and converting with plutil, but it chokes:

> defaults read > defaults.txt
> plutil -convert xml1 defaults.txt
2014-02-02 21:29:14.856 plutil[56896:707] CFPropertyListCreateFromXMLData(): Old-style
plist parser: missing semicolon in dictionary on line 10835. Parsing will be abandoned.
Break on _CFPropertyListMissingSemicolon to debug.
defaults.txt: Property List error: Unexpected character { at line 1 / JSON error: No
value for key in object around character 28.

why, you ask?

i'd like to store the defaults values in git so i can keep a record as a change setting and diff after applying changes, but it seems the serialization in defaults read is not 'line order stable': dictionaries do not dump their keys in consistent order, causing a huge amount of noise. if i can parse defaults read, i can then pipe the data out through an order-consistent serializer.

like image 452
nrser Avatar asked Feb 02 '14 14:02

nrser


People also ask

What is user defaults in macOS?

defaults Set preferences, the macOS user defaults system works with both the OS and with individual applications.

What is set preferences for macOS?

Set preferences, the macOS user defaults system works with both the OS and with individual applications.

How to read a user’s UID on macOS?

On macOS dscl is a very useful to access data in the local user directory or another directory the Mac is bound to. For example you can read a user’s UID with: This output looks easy enough to parse, you can just use cut or awk: However, dscl is a treacherous. Its output format changes, depending on the contents of an attribute.

How to change application preferences from the command line on macOS?

How To Change Preferences from the Command Line on macOS? The user friendly way to change an application preferences (aka settings) in macOS is to use the UI menu as Application Name > Preferences... or by typing the shortcut ⌘ +, from the given application.


2 Answers

You’re a lucky guy, just few days ago someone released a parser of the NeXTSTEP plist format on PyPi – nsplist.

like image 87
Jakub Jirutka Avatar answered Nov 15 '22 06:11

Jakub Jirutka


plutil can convert to xml or json

#!/bin/bash
f='~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure'
x="$(plutil -convert xml1 -o - "$f")"
j="$(plutil -convert json -o - "$f")"

-o - means: print result to stdout
$f is the input file in plist format

like image 23
Mila Nautikus Avatar answered Nov 15 '22 06:11

Mila Nautikus