'How to read a specific value in PLIST file in terminal

I'm trying to access a specific property in a plist file on my MAC OS, but the name of that property has a space in it and I cannot access it.

here is the request that I try but it returns me all SpacesDisplayConfiguration:

$defaults read com.apple.spaces SpacesDisplayConfiguration Space\ Properties

I think that this is just a syntax error, but I can't find the issue.



Solution 1:[1]

If you like doing ugly things, you could do something really ugly like this:

defaults read com.apple.spaces > /tmp/$$.plist
/usr/libexec/PlistBuddy -c 'print :SpacesDisplayConfiguration:Space\ Properties' /tmp/$$.plist

Though this is maybe slightly less ugly:

/usr/libexec/PlistBuddy -c 'print SpacesDisplayConfiguration:Space\ Properties' $HOME/Library/Preferences/com.apple.spaces.plist

The following attempts don't work, and if anyone knows why they can maybe ping me - I presume it has to do with bash process substitutions not being seekable.

defaults read com.apple.spaces | /usr/libexec/PlistBuddy -c 'print :SpacesDisplayConfiguration:Space\ Properties' /dev/stdin

defaults read com.apple.spaces | /usr/libexec/PlistBuddy -c 'print :SpacesDisplayConfiguration:Space\ Properties' -

/usr/libexec/PlistBuddy -c "print" <(defaults read com.apple.spaces)

Solution 2:[2]

4 symbols shorter, than Amin's answer ;-)

grep -A 1 <key> path/to/file.plist

Solution 3:[3]

It's not possible to read specific properties in deeper levels than the root level

Solution 4:[4]

I came around simply using cat and grep. Not so elegant, but simple and working:

cat path/to/file.plist | grep -A 1 <key>

Output example:

    <key>slideScanBlurThreshold</key>
    <real>0.0</real>

Solution 5:[5]

If you are ok to download a tool, Scout can read a nested value with a space in the key.

scout read -i $HOME/Library/Preferences/com.apple.spaces.plist -f plist \
"SpacesDisplayConfiguration.Space Properties"

Solution 6:[6]

The specific property in question is not a string but an array of dictionaries.

The minimum python code (at least v3.5 is required) that will print the space names and the window IDs in each space:

#!/usr/bin/env python3

import plistlib
from pathlib import Path

with (Path.home() / "Library/Preferences/com.apple.spaces.plist").open('rb') as fi:
    plist = plistlib.load(fi)
    spaces = plist['SpacesDisplayConfiguration']['Space Properties']

    for space in spaces:
        print('name:', space['name'])
        print('windows:', space['windows'])

# SAMPLE OUTPUT:
# name: 
# windows: [7227, 7168, 5383, 6338, 6202, 5312, 6093, 6755, 6669, 6438, 6314, 4984, 4637, 3916, 6061, 5977, 5973, 80, 6452]
# name: F10F36F4-2698-4DC0-9D1E-769712C306CC
# windows: [5370, 3565, 213, 7190, 6142, 7105, 7146, 5948, 6138, 6141, 6140, 6139, 1353, 4673, 6021, 5925, 1585, 5892, 6453]

This approach will work with both binary and XML .plist files.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Georgy Goliev
Solution 3 vadian
Solution 4 Amin Benarieb
Solution 5 Woody
Solution 6