How to add multiple entries to a plist dictionary with PlistBuddy

China☆狼群 提交于 2019-12-10 17:50:03

问题


In my Info.plist file I want to modify a Plist file on the shell which looks like this:

<plist version="1.0">
<dict>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>urlname-1</string>
        </dict>
    </array>
</dict>
</plist>

Now I want to make it look like this using PlistBuddy, adding the CFBundleURLSchemes key with a string-array value (or every other value):

<plist version="1.0">
<dict>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>urlname-1</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>urlscheme-1</string>
            </array>
        </dict>
    </array>
</dict>
</plist>

How can I achieve this with PlistBuddy?

Assumed the array value of CFBundleURLTypes would be empty: By executing /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLName string 'urlname-1'" Info.plist I'm able to add the dictionary into the array including it's first key/value pair:

<plist version="1.0">
<dict>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>urlname-1</string>
        </dict>
    </array>
</dict>
</plist>

But I don't know how to get the second key, eg CFBundleURLSchemes with a string-array value into the same dictionary.

Can anyone give me a pointer? Is this possible with PlistBuddy at all?


回答1:


Not sure if this is the command you're expecting...

/usr/libexec/PlistBuddy -c "clear dict" -c "add :CFBundleURLTypes array" -c "add :CFBundleURLTypes:0 dict" -c "add :CFBundleURLTypes:0:CFBundleURLName string 'urlname-1'" -c "add :CFBundleURLTypes:0:CFBundleURLSchemes array" -c "add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string urlscheme-1"  Info.plist



回答2:


it is possible to add, PlistBuddy is tricky but once u get, it will be very easy, u can add like below using plistbuddy...

below adds a dictionary for and set the key value pairs, hear "${10}" is the path for plist

/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLName string urlname-1" "${10}"
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes array" "${10}"
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string aSchemeName" "${10}"

angain if you want to add one more dictionary

/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleTypeRole string Viewer" "${10}"
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleURLName string url_type_1" "${10}"
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleURLSchemes array" "${10}"
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleURLSchemes: string scheme_2" "${10}"

finally plist will be like below

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>urlname-1</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>aSchemeName</string>
        </array>
    </dict>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>CFBundleURLName</key>
        <string>url_type_1</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>scheme_2</string>
        </array>
    </dict>

you will get more details here




回答3:


Unless proven otherwise, I think I cannot achieve what I wanted with plistbuddy.

I ended up using defaults write to modify my plist, and it works:

defaults write ~/Path/To/Info.plist CFBundleURLTypes -array-add '<dict><key>CFBundleURLName</key><string>urlname-1</string><key>CFBundleURLSchemes</key><array><string>urlscheme-1</string></array></dict>'


来源:https://stackoverflow.com/questions/35757267/how-to-add-multiple-entries-to-a-plist-dictionary-with-plistbuddy

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