Как я могу использовать PlistBuddy для доступа к элементу PreferencesSpecified по его свойству? - программирование
Подтвердить что ты не робот

Как я могу использовать PlistBuddy для доступа к элементу PreferencesSpecified по его свойству?

В данный момент я использую этот код

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

в script часть фазы сборки, чтобы поместить версию продукта в поле только для чтения параметров приложения. Это поле имеет позицию 1 (начиная с 0) массива предпочтений.

Я спрашиваю, возможно ли использовать что-то более надежное, чтобы 1 получить доступ к этому полю, поскольку позиция может быть случайно изменена во время разработки мной или другими разработчиками.

Могу ли я получить доступ к этому элементу, определяя его идентификатор, независимо от его положения?

Чтобы лучше объяснить мои потребности, я написал пример. Мне нужно положить что-то вроде 1.2.345 в string node из 2-го dict из array, то есть мне нужно изменить от 0.0.0 до 1.2.345. Можно ли получить доступ к dict node, не указав, что это второй в массиве? Я прошу что-то похожее на выражение xpath, которое будет использоваться в PlistBuddy (если оно существует).

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

Ответ 1

#!/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

Ответ 2

Небольшое улучшение ответа на 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