How to read a specific value in PLIST file in terminal
Asked Answered
O

6

5

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.

Oleaginous answered 17/10, 2020 at 16:11 Comment(0)
C
5

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)
Cadent answered 17/10, 2020 at 17:4 Comment(0)
E
4

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

grep -A 1 <key> path/to/file.plist
Emotive answered 23/4, 2022 at 7:5 Comment(2)
Oh, go on... remove the space between the A and the 1 and make it 5 shorter🤣Cadent
@MarkSetchell, thank you! I will keep working on improvements!🤣Emotive
B
1

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>
Birdcage answered 28/3, 2021 at 3:7 Comment(0)
M
0

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

Morbilli answered 17/10, 2020 at 16:52 Comment(0)
W
0

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"
Whenever answered 29/4, 2021 at 17:51 Comment(0)
D
0

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.

Dieselelectric answered 9/5, 2022 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.