16

現時点では、このコードを使用しています

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

ビルド段階のスクリプト部分で、製品バージョンをアプリケーション設定の読み取り専用フィールドに入れます。そのフィールドには、設定配列の位置 1 (0 から開始) があります。

私や他の開発者が開発中に位置を誤って変更する可能性があるため、そのフィールドにアクセスするために 1 よりも堅牢なものを使用することが可能かどうかを尋ねています。

その位置に関係なく、その識別子を指定してその要素にアクセスできますか?

私のニーズをよりよく説明するために、例を書き留めました。1.2.345のようなものを2 番目のstringノードに入れる必要があります。つまり、 からに変更する必要があります。配列の2番目であることを明記せずにノードにアクセスすることは可能ですか? PlistBuddy で使用される xpath 式に似たものを求めています (存在する場合)。dictarray0.0.01.2.345dict

<?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>
         ...
4

2 に答える 2

15
#!/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
于 2012-12-29T20:07:27.247 に答える
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
于 2014-08-13T10:05:59.473 に答える