问题
I want to change the value of DEFAULT_VALUE_PLACEHOLDER
in the following plist using the command line tool defaults
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>DefaultValue</key>
<string>DEFAULT_VALUE_PLACEHOLDER</string>
<key>Type</key>
<string>PSTitleValueSpecifier</string>
<key>Title</key>
<string>Version</string>
<key>Key</key>
<string>prefs_item_version_title</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
I realise that a simple find and replace will do it (e.g. sed), however, I want a more robust way of doing it.
I think is something like this, but the documentation for the syntax isn't good enough.
defaults write $PLIST_PATH 'PreferenceSpecifiers { 1 = { DefaultValue = $NEW_DETAULT_VALUE; }; }'
回答1:
I don't think there's any way to do this with defaults
(that isn't completely ugly) -- you're better off doing things like this with PlistBuddy instead:
/usr/libexec/PlistBuddy -c "set :PreferenceSpecifiers:0:DefaultValue '$NEW_DEFAULT_VALUE'" "$PLIST_PATH"
Note that unlike defaults
, PlistBuddy expects the filename you give it to include the ".plist"; also, (as seen above), array indexes start at 0.
来源:https://stackoverflow.com/questions/14355850/in-bash-how-do-i-use-defaults-write-on-a-plist-for-a-existing-element-in-an-arr