xcode 如何使用 PlistBuddy 访问由其属性指定的 PreferencesSpecified 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13970218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use PlistBuddy to access an element of PreferencesSpecified by its property?
提问by giampaolo
At the moment I'm using this code
目前我正在使用此代码
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $productVersion" "Test/Settings.bundle/Root.plist"
in script part of build phase to put product version in a read only field of the application settings. That field has position 1 (starting from 0) of the preferences array.
在构建阶段的脚本部分,将产品版本放在应用程序设置的只读字段中。该字段具有首选项数组的位置 1(从 0 开始)。
I'm asking if it's possibile to use something more robust that 1 to access that field since position can be accidentally changed during development by me or by other developers.
我在问是否可以使用比 1 更强大的东西来访问该字段,因为我或其他开发人员在开发过程中可能会意外更改位置。
Can I access that element specifying it's identifier regardless of its position?
我可以访问指定它的标识符的元素而不管它的位置吗?
To better explain my needs, I wrote down an example. I need to put something like 1.2.345
into string
node of 2nd dict
of array
ie I need to change from 0.0.0
to 1.2.345
. Is it possible to access to dict
node without stating that it's the second in the array? I'm asking for something similar to an xpath expression to be used in PlistBuddy (if any exists).
为了更好地说明我的需求,我写了一个例子。我需要将类似的东西1.2.345
放入string
第二个节点dict
,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>
...
采纳答案by geowar
#!/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
回答by LordPingvin
A little improvement to the geowar's answer. Get product version from Info.plist.
对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