View Full Version : defaults write to child property
neuralstatic
04-12-2006, 03:01 PM
i can't find this online anywhere.
i need to set a preference property for all users via a command line.
unfortunately it's a property that is a child of another
it's for com.apple.mail.plist, and this approximates how it would look in the plist editor
MailAccounts
__1
____CachePolicy String OFF
defaults write com.fake.fake MailAccounts '{ 1 = { CachePolicy = OFF; }; }'
this works b ut wipes out other entries under the "1" child.
i think i need to kill the "=" sign, but i can't get the syntax.
biovizier
04-12-2006, 04:05 PM
For writing to that type of an array, 'defaults' can't do it easily but 'PlistBuddy' can. Apparently 'PlistBuddy' has been around for a while but I only became aware of it earlier this year when "cgerke" mentioned it on this site - it isn't a part of the default installation, but is often included with the scripts, etc. left over in "/Library/Receipts". A typical 10.3 or 10.4 installation with iTunes updated will have this on their system.
It has some good instructions included (try '/path/to/PlistBuddy -h'). But for the example in your post, try something like:PlistBuddy -c "Delete MailAccounts:1:CachePolicy" ~/Library/Preferences/com.apple.Mail.plist
PlistBuddy -c "Add MailAccounts:1:CachePolicy string OFF" ~/Library/Preferences/com.apple.Mail.plistVerbs include "Set", "Add" and "Delete" but it is less convenient than the 'defaults' counterparts since "Set" doesn't seem to work if the key doesn't already exist, and "Add" doesn't seem to overwrite the key if it does, which is why a "Delete" was included at the beginning...
Edit: I just checked my "/Library/Receipts" and 'PlistBuddy' seems to be included with eg. SecUpd2006-002, as well as various OS X system updates so odds are most people will have at least one copy...
bramley
04-12-2006, 04:35 PM
Apparently 'PlistBuddy' has been around for a while but I only became aware of it earlier this year when "cgerke" mentioned it on this site <snip>
Likewise, except it was this post that made me drag it out and have a look.
My only other comment would be that because the property being written to an element in an array, the write will only be to the correct element if it always element 1 of the array. You may need to check you have the right element first if this won't always be true.
Mikey-San
04-12-2006, 04:43 PM
Something like this?
$ defaults write [domain] dictName -dict keyname valuestring keyname2 valuestring2
Will edit the values of the two keys "keyname" and "keyname2" of the dictionary "dictName". Result of that example (after converted with plutil for readbility):
<?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>dictName</key>
<dict>
<key>keyname</key>
<string>valuestring</string>
<key>keyname2</key>
<string>valuestring2</string>
</dict>
</dict>
</plist>
neuralstatic
04-12-2006, 05:41 PM
amazingly good help on this one. thanks CA,UK,USA
su1c1de
07-16-2007, 08:41 PM
i need a similar sort of thing, but i need to go one step further.
i need to go in an array > dictionary > edit a key
i need to use defaults and everything i have tried has either removed all other keys from the dict or has just created a new array.
help, please
Alex Yeh
07-19-2007, 02:20 PM
…A typical 10.3 or 10.4 installation with iTunes updated will have [PlistBuddy] on their system.
I just checked, and it isn't available in Panther. Just Tiger. Oh, well.
You can always use perl to do this kind of replacement - perl is marvelous at find & replace.
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.