How to add multiple entries to a plist dictionary with PlistBuddy
Asked Answered
F

3

5

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?

Felicafelicdad answered 2/3, 2016 at 20:18 Comment(0)
H
17

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
Hako answered 10/5, 2016 at 6:56 Comment(0)
H
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

Haw answered 2/12, 2016 at 7:45 Comment(0)
F
0

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>'
Felicafelicdad answered 4/3, 2016 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.