【问题标题】:Editing plist file using shell script使用 shell 脚本编辑 plist 文件
【发布时间】:2014-11-29 20:41:35
【问题描述】:

我使用 pkgbuild 创建了一个默认的组件属性列表文件。该文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-     1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>BundleHasStrictIdentifier</key>
        <true/>
        <key>BundleIsRelocatable</key>
        <true/>
        <key>BundleIsVersionChecked</key>
        <true/>
        <key>BundleOverwriteAction</key>
        <string>upgrade</string>
        <key>RootRelativeBundlePath</key>
        <string>MyApp.app</string>
    </dict>
</array>
</plist>

我想使用 shell 脚本修改这个文件。我尝试使用 defaults write 但它没有做任何事情。

有什么办法呢?(例如:我要设置BundleIsRelocatable为false)

【问题讨论】:

    标签: macos shell scripting plist pkgbuild


    【解决方案1】:

    还有:

    plutil -replace BundleIsRelocatable -bool false plistfilename.plist
    

    【讨论】:

      【解决方案2】:

      对于字符串使用

      plutil -replace NameOfProperty -string "yourNewValue" plistFileName.plist
      

      【讨论】:

        【解决方案3】:

        使用PlistBuddy, a simple tutorial HERE.

         /usr/libexec/PlistBuddy -c "Set :BundleIsRelocatable bool false" plistfilename.plist
        

        它可以作为一个命令行运行来更新键/值。 我一般用它来更新CFBundleVersion,可以找到in this post

        【讨论】:

          【解决方案4】:

          有点晚了,但为了记录,您只需要指定绝对路径并将 .plist 扩展名添加到文件名。如果您在 plist 文件所在的同一目录中运行脚本,您的案例将被翻译成:

          defaults write $PWD/YourPlistFilename.plist BundleIsRelocatable -bool false
          

          【讨论】:

            【解决方案5】:

            使用PlistBuddy

            非常简单直接。 示例:

            /usr/libexec/PlistBuddy ComponentPropertyList.plist
            Command: Set :0:BundleIsRelocatable false
            Command: save
            Saving...
            Command: exit
            

            就是这样!现在 BundleIsRelocatable 是假的:D

            【讨论】:

              【解决方案6】:

              使用sed

              sed -i '' '/<key>BundleIsRelocatable</{n;s/true/false/;}' file.plist
              

              如果 plist 不是 XML,请先运行 plutil -convert xml1 file.plist

              【讨论】:

                【解决方案7】:

                Phil-CB here 的最后回复应该很有用。

                【讨论】:

                  猜你喜欢
                  • 2016-08-10
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-08-03
                  • 1970-01-01
                  • 2016-01-21
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多