bash 如何使用 PlistBuddy 将多个条目添加到 plist 字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35757267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add multiple entries to a plist dictionary with PlistBuddy
提问by derFunk
In my Info.plist
file I want to modify a Plist file on the shell which looks like this:
在我的Info.plist
文件中,我想修改外壳上的 Plist 文件,如下所示:
<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):
现在我想使用 PlistBuddy 使它看起来像这样,添加CFBundleURLSchemes
带有字符串数组值(或其他所有值)的键:
<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?
如何使用 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:
假设的数组值为CFBundleURLTypes
空:通过执行/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLName string 'urlname-1'" Info.plist
我能够将字典添加到数组中,包括它的第一个键/值对:
<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.
但我不知道如何获取第二个键,例如CFBundleURLSchemes
将字符串数组值放入同一个字典中。
Can anyone give me a pointer? Is this possible with PlistBuddy at all?
谁能给我一个指针?PlistBuddy 完全可以做到这一点吗?
回答by Mic Kimbara
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
回答by Shankar BS
it is possible to add, PlistBuddy
is tricky but once u get, it will be very easy, u can add like below using plistbuddy...
可以添加,这PlistBuddy
很棘手,但是一旦你得到,这将非常容易,你可以使用 plistbuddy 添加如下...
below adds a dictionary for and set the key value pairs, hear "${10}"
is the path for plist
下面添加了一个字典并设置了键值对,hear"${10}"
是 plist 的路径
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLName string urlname-1" ""
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes array" ""
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string aSchemeName" ""
angain if you want to add one more dictionary
如果您想再添加一本字典,请再次
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleTypeRole string Viewer" ""
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleURLName string url_type_1" ""
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleURLSchemes array" ""
/usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:1:CFBundleURLSchemes: string scheme_2" ""
finally plist will be like below
最后 plist 将如下所示
<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
回答by derFunk
Unless proven otherwise, I think I cannot achieve what I wanted with plistbuddy.
除非另有证明,否则我认为我无法通过 plistbuddy 实现我想要的。
I ended up using defaults write
to modify my plist, and it works:
我最终使用defaults write
修改我的 plist,它的工作原理:
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>'