如何使用PlistBuddy将多个条目添加到plist词典

der*_*unk 3 macos bash plist

在我的Info.plist文件中,我想修改外壳上的Plist文件,如下所示:

<plist version="1.0">
<dict>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>urlname-1</string>
        </dict>
    </array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

现在,我想使用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>
Run Code Online (Sandbox Code Playgroud)

如何使用PlistBuddy做到这一点?

假设的数组值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>
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何获取第二个键,例如CFBundleURLSchemes将字符串数组值放入同一字典中。

谁能给我指点?使用PlistBuddy可以做到吗?

小智 5

不确定这是否是您期望的命令...

/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
Run Code Online (Sandbox Code Playgroud)


der*_*unk 0

除非另有证明,否则我认为我无法通过 plistbuddy 实现我想要的目标。

我最终使用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>'
Run Code Online (Sandbox Code Playgroud)