Defaults read sub dictionary

Hi,

If I have a plist file like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>-array</key>
<dict>
<key>test1</key>
<string>hello</string>
<key>test2</key>
<string>bye</string>
</dict>
</dict>
</plist>

is there an easy way to get the value "bye" using key "test2" with defaults read ...

Thanks,

iBook 800, 14.1,, Mac OS X (10.2.x)

Posted on Sep 8, 2006 7:03 PM

Reply
9 replies

Sep 8, 2006 8:13 PM in response to kel

Unix is so frustrating! I'm starting to sweat here. Here's the AppleScript:

--do shell script "defaults write /Users/kel/my d1 -dict"
do shell script "defaults write /Users/kel/my d1 -dict-add a1 -array 1 2"

Firstly, I ran the commented part which created a plist with an empty dictionary. Then I wanted to add an array to the dictionary with line two. I tried a lot of other combinations reading from the man page for defaults.

Basically, I wanted to create a plist with dict, dict, array, dict and know how to read a key from the last dict. Starting to get frustrated here.

Thanks,

Sep 8, 2006 9:22 PM in response to kel

kel,

Unix can be certainly be frustrating, but the problem you're dealing with is more related to the defaults command itself. As you've found, the defaults command is not very well suited for reading or writing nested structures... the man page for defaults pretty much says this down near the bottom.

There is another similar utility called PlistBuddy. It's not part of the basic OS X install but Apple includes it in several of it's installer packages... and as a result it gets left behind inside the package receipt files (in /Library/Receipts). You can probably find several copies of it on your machine by entering this at a command prompt:

locate PlistBuddy

If you find a copy and execute PlistBuddy with a "-h" option it will print out some instructions for how to use it. For example:

/path/to/a/copy/of/PlistBuddy -h

I understand that it's somewhat better suited for working with nested structures but I've never had a need to use it. The help is not terribly detailed but should give you an idea of how to use it. It allows you to use a sort of "keypath" to get to items in nested structures.

If you're looking for portability then PlistBuddy might not be a good choice since it's not part of the base OS. But if you're just working on your own machine and you can find a copy of PlistBuddy on your machine you could certainly copy it out of the receipt file to a more convenient location.

Good luck,
Steve

Sep 8, 2006 10:25 PM in response to kel

Hi kel,

Oops, I didn't notice you were still at 10.2.x... and I don't know if PlistBuddy was being included in installer packages back then. Make sure you used an uppercase "P" if you tried the locate command though... it's PlistBuddy not plistBuddy (and the locate command is case sensitive).

If you do manage to find a copy that works in Jaguar I ran across this link which has a sample shell script that uses PlistBuddy to build what looks like a fairly complex plist file:
http://listserv.cuny.edu/Scripts/wa.exe?A2=ind0606&L=macenterprise&T=0&H=1&P=312 85

Steve

Sep 9, 2006 6:53 PM in response to Steve Herman1

Hi,

I just wanted to create the complex xml as an example to see how to parse the xml.

I ended up just getting the second level dictionary and parsing the text down to the forth level dictionaries' keys and values.

BTW Gary, I knew how to create dictionaries, but that can get very complex converting from AppleScirpt lists and records.

Thanks a lot Steve and Gary.

Have a good day,

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Defaults read sub dictionary

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.