2012-12-20 6 views
14

Obecnie używam tego koduJak mogę użyć PlistBuddy, aby uzyskać dostęp do elementu PreferencesSpecified przez jego właściwość?

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

w części skryptu fazy budowy, aby umieścić produkt w wersji tylko do odczytu dziedzinie ustawieniach aplikacji. To pole ma pozycję 1 (począwszy od 0) tablicy preferencji.

Pytam, czy można użyć czegoś solidniejszego niż 1, aby uzyskać dostęp do tego pola, ponieważ pozycja może zostać przypadkowo zmieniona podczas programowania przeze mnie lub innych programistów.

Czy mogę uzyskać dostęp do tego elementu, podając jego identyfikator, niezależnie od jego pozycji?

Aby lepiej wyjaśnić moje potrzeby, zapisałem przykład. Muszę umieścić coś w rodzaju 1.2.345 w węźle string z 2 dict z array, tj. Muszę zmienić z 0.0.0 na 1.2.345. Czy jest możliwy dostęp do węzła dict bez stwierdzenia, że ​​jest to drugi element w tablicy? Pytam o coś podobnego do wyrażenia xpath, które będzie używane w PlistBuddy (jeśli takie istnieje).

<?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> 
     ... 
+0

Zależy od plist (post it?). Jeśli chcesz wejść do słowa kluczowego, powinieneś umieścić go w słowniku zamiast tablicy. – geowar

+1

Dodałem przykład pliku plist (naprawdę nie jest to plik plist, którego używam, ale nie mam go w tej chwili) – giampaolo

+0

Tak długo, jak używasz na najwyższym poziomie, możesz uzyskać do niego dostęp tylko poprzez indeks; jeśli chcesz uzyskać do nich dostęp za pomocą konkretnych słów kluczowych, musisz zamiast tego przejść do słownika. – geowar

Odpowiedz

13
#!/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 
+0

Tego właśnie potrzebuję: $ {idx}, dzięki! –

7

Trochę poprawa odpowiedzieć na geowar użytkownika. Pobierz wersję produktu z 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 
Powiązane problemy