2012-12-20 3 views
14

나는 응용 프로그램 설정의 읽기 전용 필드에 제품 버전을 넣어 빌드 단계의 스크립트 부분에이 코드PlistBuddy를 사용하여 해당 속성으로 PreferencesSpecified 요소에 액세스하려면 어떻게해야합니까? 순간

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

을 사용하고 있습니다. 이 필드는 우선 순위 배열의 위치 1 (0부터 시작)을가집니다.

나 또는 다른 개발자가 개발하는 동안 위치가 실수로 변경 될 수 있으므로 해당 필드에 액세스하기 위해 1보다 강력한 것을 사용하는 것이 가능한지 묻습니다.

위치에 관계없이 식별자를 지정하여 해당 요소에 액세스 할 수 있습니까?

내 요구를 더 잘 설명하기 위해 예제를 적어 둡니다. 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 
관련 문제