PlistBuddy简单使用

PlistBuddy简单使用

由于PlistBuddy并不在Mac默认的Path里,所以我们得通过绝对路径来引用这个工具:

  • 查看帮助
/usr/libexec/PlistBuddy --help

下面我们来看看PlistBuddy的简单使用

打印:
  • 初始化一个 info.plist 文件

初始化info.plist
  • 打印info.plist文件
/usr/libexec/PlistBuddy -c "print" info.plist
  • 在终端输入上述命令后如下所示:
添加

  • 添加普通字段:
/usr/libexec/PlistBuddy -c 'Add :Version string 1.0' info.plist
  • 添加数组字段,分两步走,注意:key之间用 : 隔开,且不能有空格
# 先添加key值
/usr/libexec/PlistBuddy -c 'Add :Application array' info.plist
# 添加value值
yans67deMacBook-Pro:needfiles huangyg$ /usr/libexec/PlistBuddy -c 'Add :Application: string app1' info.plist
yans67deMacBook-Pro:needfiles huangyg$ /usr/libexec/PlistBuddy -c 'Add :Application: string app2' info.plist
yans67deMacBook-Pro:needfiles huangyg$ /usr/libexec/PlistBuddy -c 'Add :Application: string app3' info.plist
  • 添加字典字段,分两步走:
# 先添加key值
/usr/libexec/PlistBuddy -c 'Add :Person dict' info.plist
# 添加value值,
/usr/libexec/PlistBuddy -c 'Add :Age string secret' info.plist
/usr/libexec/PlistBuddy -c 'Add :Person:Name string yans67' info.plist
/usr/libexec/PlistBuddy -c 'Add :Person:sex string boy' info.plist
/usr/libexec/PlistBuddy -c 'Add :Person:weight string 65' info.plist
输出

  • 打印字段相应的值:
 /usr/libexec/PlistBuddy -c 'Print :Person' info.plist
  • 在array中我们还可以根据下标打印某个特定的值
/usr/libexec/PlistBuddy -c 'Print :Application:2' info.plist
删除

  • 删除字段相应的值:
/usr/libexec/PlistBuddy -c 'Delete :Version' info.plist
修改

  • 修改某个字段相应的值:
/usr/libexec/PlistBuddy -c 'Set :Application:1 string "thi is app1"' info.plist
合并

  • 当有两个plist文件的时候,我们可以对其进行合并操作
# 将A.plist 合并到 B.plist中
/usr/libexec/PlistBuddy -c 'Merge A.plist'  B.plist

终端中会提示B.plist中有重复的键值,所以默认跳过该键值的合并

合并前

原文链接: http://www.jianshu.com/p/2167f755c47e

参考链接: http://shaojunxiao.com/2017/03/23/使用PlistBuddy修改info-plist文件/?utm_source=tuicool&utm_medium=referral

      https://segmentfault.com/a/1190000002423661

原文地址:https://www.cnblogs.com/lurenq/p/6920506.html