2012-12-20 11 views
14

Al momento sto usando questo codiceCome posso utilizzare PlistBuddy per accedere a un elemento di PreferencesSpecified dalla sua proprietà?

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

in parte copione della fase di costruzione di mettere versione del prodotto in un campo di sola lettura delle impostazioni dell'applicazione. Questo campo ha la posizione 1 (a partire da 0) dell'array delle preferenze.

Mi chiedo se sia possibile utilizzare qualcosa di più robusto per accedere a quel campo poiché la posizione può essere modificata accidentalmente durante lo sviluppo da me o da altri sviluppatori.

Posso accedere a quell'elemento specificando il suo identificatore indipendentemente dalla sua posizione?

Per spiegare meglio le mie esigenze, ho annotato un esempio. Devo inserire qualcosa come 1.2.345 nel nodo string del 2o dict di array, ovvero devo passare da 0.0.0 a 1.2.345. È possibile accedere al nodo dict senza affermare che è il secondo nell'array? Sto chiedendo qualcosa di simile a un'espressione xpath da utilizzare in PlistBuddy (se esiste).

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

Dipende dal plist (post-it?). Se si desidera una voce di riferimento a parola chiave, è necessario inserirla in un dizionario anziché in un array. – geowar

+1

Ho aggiunto un esempio di plist (non proprio il plist che sto usando, ma non l'ho disponibile al momento) – giampaolo

+0

Finché si utilizza al livello superiore, è possibile accedervi solo per indice; se si desidera accedervi tramite parole chiave specifiche, sarà necessario passare a un dizionario. – geowar

risposta

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

Questo è ciò di cui ho bisogno: $ {idx}, grazie! –

7

Un piccolo miglioramento della risposta della geowar. Ottieni la versione del prodotto da 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 
Problemi correlati