2012-12-20 24 views
14

फिलहाल मैं आवेदन सेटिंग्स की एक रीड ओनली फ़ील्ड में उत्पाद संस्करण डाल करने के लिए इस कोड कोमैं अपनी संपत्ति द्वारा निर्दिष्ट प्राथमिकताओं के तत्व तक पहुंचने के लिए प्लिस्टबड्डी का उपयोग कैसे कर सकता हूं?

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

उपयोग कर रहा हूँ निर्माण चरण के स्क्रिप्ट भाग में। उस फ़ील्ड में वरीयता सरणी की स्थिति 1 (0 से शुरू) है।

मैं पूछ रहा हूं कि क्या उस क्षेत्र तक पहुंचने के लिए कुछ और मजबूत उपयोग करने की संभावना है क्योंकि स्थिति मेरे या अन्य डेवलपर्स द्वारा विकास के दौरान गलती से बदल दी जा सकती है।

क्या मैं उस तत्व को एक्सेस कर सकता हूं जो इसकी पहचान के बावजूद पहचानकर्ता निर्दिष्ट करता है?

मेरी ज़रूरतों को बेहतर ढंग से समझाने के लिए, मैंने एक उदाहरण लिखा। मुझे 1.2.345string के array के नोड को 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> 
     ... 
+0

प्लेस्ट पर निर्भर करता है (इसे पोस्ट करें?)। यदि आप एक कीवर्ड संदर्भित प्रविष्टि चाहते हैं तो आपको इसे सरणी के बजाय एक शब्दकोश में रखना चाहिए। – geowar

+1

मैंने प्लिस्ट का एक उदाहरण जोड़ा (वास्तव में मैं जिस प्लिस्ट का उपयोग कर रहा हूं, लेकिन मैं इस समय उपलब्ध नहीं हूं) – giampaolo

+0

जब तक आप शीर्ष स्तर पर का उपयोग करते हैं तो आप इसे केवल अनुक्रमणिका द्वारा एक्सेस कर सकते हैं; यदि आप विशिष्ट कीवर्ड द्वारा इन तक पहुंचना चाहते हैं तो आपको इसके बजाय एक शब्दकोश में स्विच करना होगा। – geowar

उत्तर

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

यही मुझे चाहिए: $ {idx}, धन्यवाद! –

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