PlistBuddy操作plist文件

背景:xcode打包时,需要更改一些plist文件,以满足不同的包要求
1、应用的Info.plist,更改包的版本号
2、导出包exportArchive的时候需要指定exportOptionsPlist的plist文件
 
当然我们可以通过python或shell等脚本去处理,但是比较麻烦,mac提供了PlistBuddy工具更编辑的操作plist文件
 

一、常见的plist操作

 
plist执行命令(具体支持的所有命令详见第2部分):
/usr/libexec/PlistBuddy -c '命令 :命令的具体内容' xxx.plist
 
例如:/usr/libexec/PlistBuddy -c 'Add :provisioningProfiles:test1key string  test1value' optionList.plist
 
例子初始化文件如下:
 

1、添加一个节点(使用Add命令)

a)普通类型
/usr/libexec/PlistBuddy -c 'Add :test_string_type string  test_string_value' optionList.plist
 
说明:标红部分string为值的类型,还可以添加bool等类型
 
b)字典类型
/usr/libexec/PlistBuddy -c 'Add :test_dict_type:key1 string  value1' optionList.plist
/usr/libexec/PlistBuddy -c 'Add :test_dict_type:key2 string  value2' optionList.plist
 
说明:标红部分由冒号分隔为两部分,左侧是字典的key,右侧为字典的子项的key,最右侧是对应子项的值
 
c)数组类型
/usr/libexec/PlistBuddy -c 'Add :test_list_type array' optionList.plist
/usr/libexec/PlistBuddy -c 'Add :test_list_type: string  value1' optionList.plist
/usr/libexec/PlistBuddy -c 'Add :test_list_type: string  value2' optionList.plist
 
说明:
1、数据类型需要先增加一个array类型的key,参见第一步
2、如果没先建array的key,直接使用第2步添加,实际创建的是一个dict类型的key
 

2、修改节点内容(使用Set命令)

a)普通类型
/usr/libexec/PlistBuddy -c 'Set :test_string_type change_test_string_value' optionList.plist
 
说明:命令直接表示好key和value即可
 
b)更改字典的值
/usr/libexec/PlistBuddy -c 'Set :test_dict_type:key1 change_value1' optionList.plist
 
c)更改array数组的值
/usr/libexec/PlistBuddy -c 'Set :test_list_type:1 change_value2' optionList.plist
 
说明:key后数字表示要修改哪个子项(从0开始计算个数)
 
3、打印值(使用Print命令)
a)普通类型
/usr/libexec/PlistBuddy -c 'Print test_string_type' optionList.plist
 
b)数组类型
/usr/libexec/PlistBuddy -c 'Print test_list_type' optionList.plist
/usr/libexec/PlistBuddy -c 'Print test_list_type:1' optionList.plist
 
说明:array的key后不加索引数字,打印所有列表值;加索引,打印对应位置的值(索引从0开始)
 
c)字典类型
/usr/libexec/PlistBuddy -c 'Print test_dict_type:key1' optionList.plist
/usr/libexec/PlistBuddy -c 'Print test_dict_type' optionList.plist
 
说明:字典的key后面加上子项的key,会打印对应key的具体值。不指明打印的key值,直接显示该字典所有值
 
4、删除值(使用Delete命令)
a)普通类型
/usr/libexec/PlistBuddy -c 'Delete test_string_type ' optionList.plist
 
b)array类型
/usr/libexec/PlistBuddy -c 'Delete test_list_type:1 ' optionList.plist
说明:删除整个array,参考删除普通类型方法,直接删除key
删除array下的单个项,在arry的key后面加上【:索引值】
 
c)dict类型
/usr/libexec/PlistBuddy -c 'Delete test_dict_type:key1' optionList.plist
/usr/libexec/PlistBuddy -c 'Delete test_dict_type' optionList.plist
说明:删除整个array,参考删除普通类型方法,直接删除key
删除字典下的某个key值,在字典key后面加上【:子项的key】
 
PlistBuddy还支持其他操作,比如复制、合并等操作,具体内容详见下面第2部分的说明文档
 
备注:如果要传递变量,需将变量用单引号括起来,例子如下
/usr/libexec/PlistBuddy -c 'Set :teamID '${teamId}'' optionList.plist

二、查看PlistBuddy支持的命令

/usr/libexec/PlistBuddy -help
 
支持命令如下:
Command Format:
    Help - Prints this information
    Exit - Exits the program, changes are not saved to the file
    Save - Saves the current changes to the file
    Revert - Reloads the last saved version of the file
    Clear [<Type>] - Clears out all existing entries, and creates root of Type
    Print [<Entry>] - Prints value of Entry.  Otherwise, prints file
    Set <Entry> <Value> - Sets the value at Entry to Value
    Add <Entry> <Type> [<Value>] - Adds Entry to the plist, with value Value
    Copy <EntrySrc> <EntryDst> - Copies the EntrySrc property to EntryDst
    Delete <Entry> - Deletes Entry from the plist
    Merge <file.plist> [<Entry>] - Adds the contents of file.plist to Entry
    Import <Entry> <file> - Creates or sets Entry the contents of file
 
Entry Format:
    Entries consist of property key names delimited by colons.  Array items
    are specified by a zero-based integer index.  Examples:
        :CFBundleShortVersionString
        :CFBundleDocumentTypes:2:CFBundleTypeExtensions
 
Types:
    string
    array
    dict
    bool
    real
    integer
    date
    data
 
Examples:
    Set :CFBundleIdentifier com.apple.plistbuddy
        Sets the CFBundleIdentifier property to com.apple.plistbuddy
    Add :CFBundleGetInfoString string "App version 1.0.1"
        Adds the CFBundleGetInfoString property to the plist
    Add :CFBundleDocumentTypes: dict
        Adds a new item of type dict to the CFBundleDocumentTypes array
    Add :CFBundleDocumentTypes:0 dict
        Adds the new item to the beginning of the array
    Delete :CFBundleDocumentTypes:0 dict
        Deletes the FIRST item in the array
    Delete :CFBundleDocumentTypes
        Deletes the ENTIRE CFBundleDocumentTypes array
原文地址:https://www.cnblogs.com/meitian/p/12465665.html