In BASH, how do I use defaults write on a plist for a existing element in an array?

假如想象 提交于 2021-02-18 19:14:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!