2012-12-20 106 views
14

目前我使用這個代碼如何使用PlistBuddy訪問其屬性指定的Preferences元素?

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

在構建階段的腳本部分把產品版本的應用程序設置了一個只讀字段。該字段的首選項數組的位置爲1(從0開始)。

我在問是否有可能使用更強大的功能來訪問該字段,因爲位置可能會在開發過程中由我或其他開發人員意外更改。

我可以訪問指定它的標識符的元素,而不管它的位置嗎?

爲了更好地解釋我的需求,我寫下了一個例子。我需要把類似1.2.345string節點的第二個dictarray即我需要從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

取決於plist(貼吧?)。如果你想要一個關鍵字引用條目,你應該把它放在一個字典而不是一個數組。 – geowar

+1

我添加了一個plist的例子(不是我使用的plist,但我目前還沒有它) – 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 
相關問題