Reputation: 1630
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?
Upvotes: 5
Views: 10845
Reputation: 8402
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
Upvotes: 2
Reputation: 171
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
Upvotes: 17
Reputation: 1630
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>'
Upvotes: 0