Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple entries to a plist dictionary with PlistBuddy

Tags:

bash

macos

plist

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?

like image 265
derFunk Avatar asked Dec 06 '22 18:12

derFunk


2 Answers

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
like image 179
Mic Kimbara Avatar answered Dec 08 '22 06:12

Mic Kimbara


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

like image 30
Shankar BS Avatar answered Dec 08 '22 07:12

Shankar BS