如何使用PlistBuddy访问其属性指定的PreferencesS元素?

时间:2012-12-20 10:36:39

标签: xcode plist

目前我正在使用此代码

  /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $productVersion" "Test/Settings.bundle/Root.plist"

在构建阶段的脚本部分,将产品版本放在应用程序设置的只读字段中。该字段具有首选项数组的位置1(从0开始)。

我问是否可以使用更强大的1来访问该字段,因为我或其他开发人员在开发期间可能会意外更改位置。

我可以访问该元素,指定它的标识符,无论其位置如何?

为了更好地解释我的需求,我写下了一个例子。我需要将1.2.345之类的内容放入string dict array的{​​{1}}个节点,即我需要从0.0.0更改为1.2.345。是否可以访问dict节点而不说明它是数组中的第二个节点?我要求在PlistBuddy中使用类似于xpath表达式的东西(如果有的话)。

<?xml version="1.0" encoding="UTF-8"?>
<dict>
<key>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Title</key>
        <string>Application info</string>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
    </dict>
    <dict>
        <key>DefaultValue</key>
        <string>0.0.0</string>
        <key>Key</key>
        <string>version</string>
        <key>Title</key>
        <string>Version</string>
        <key>Type</key>
        <string>PSTitleValueSpecifier</string>
    </dict>
    <dict>
        <key>DefaultValue</key>
        <string>0</string>
        <key>Key</key>
        <string>build</string>
        <key>Title</key>
        <string>Build</string>
        <key>Type</key>
        <string>PSTitleValueSpecifier</string>
    </dict>
         ...

2 个答案:

答案 0 :(得分:14)

#!/bin/tcsh
set productVersion="1.2.345"
set theFile="~/Desktop/PlistBuddy/Root.plist"
set cnt=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:" ${theFile} | grep "Dict"|wc -l`
# echo "the count is: $cnt."
set cnt=`expr "$cnt" '-' '1'`

foreach idx (`seq 0 $cnt`)
    # echo "the index is: $idx."
    set val=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:Title" ${theFile}`
    # echo "the value of PreferenceSpecifiers:${idx}:Title: is ${val}."

    if ( "$val" == "Version" ) then
        echo "the index of the entry whose 'Title' is 'Version' is $idx."
        # now set it
        /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:${idx}:DefaultValue $productVersion" ${theFile}

        # just to be sure that it worked
        set ver=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:DefaultValue" ${theFile}`
        echo 'PreferenceSpecifiers:$idx:DefaultValue set to: ' $ver
    endif
end

答案 1 :(得分:7)

对geowar的回答略有改进。从Info.plist获取产品版本。

#!/bin/tcsh
set infoPlist="Info.plist"
set version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ${infoPlist}`
set bundleVersion=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${infoPlist}`
set productVersion=$version.$bundleVersion
# echo "the product version is ${productVersion}."

set settingsPlist="Settings.bundle/Root.plist"
set settingsCnt=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:" ${settingsPlist} | grep "Dict"|wc -l`
# echo "the count is: $settingsCnt."
set settingsCnt=`expr "$settingsCnt" '-' '1'`

foreach idx (`seq 0 $settingsCnt`)
    # echo "the index is: $idx."
    set val=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:Key" ${settingsPlist}`
    # echo "the value of PreferenceSpecifiers:${idx}:Title: is ${val}."

    if ( "$val" == "version" ) then
        echo "the index of the entry whose 'Key' is 'version' is $idx."
        # now set it
        /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:${idx}:DefaultValue $productVersion" ${settingsPlist}

        # just to be sure that it worked
        set ver=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:DefaultValue" ${settingsPlist}`
        echo 'PreferenceSpecifiers:$idx:DefaultValue set to: ' $ver
    endif
end