如何使用PlistBuddy访问由其属性指定的Preferences元素? [英] How can I use PlistBuddy to access an element of PreferencesSpecified by its property?

查看:64
本文介绍了如何使用PlistBuddy访问由其属性指定的Preferences元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我正在使用此代码

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

在构建阶段的脚本部分中

可以将产品版本放入应用程序设置的只读字段中.该字段在首选项数组中的位置为1(从0开始).

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更强大的功能来访问该字段,因为在开发过程中我或其他开发人员可能会意外更改位置.

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.

我可以访问指定其标识符的元素,而不论其位置如何?

Can I access that element specifying it's identifier regardless of its position?

为了更好地解释我的需求,我写下了一个例子.我需要将1.2.345之类的内容放入array的第二个dictstring节点中,即我需要从0.0.0更改为1.2.345.是否可以访问dict节点而不声明它是数组中的第二个节点?我要的是类似于PlistBuddy中使用的xpath表达式的东西(如果有的话).

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).

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

推荐答案

#!/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

这篇关于如何使用PlistBuddy访问由其属性指定的Preferences元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆