PlistBuddy 对plist文件的操作

it2024-08-18  42

plist(property list)是一种文件组织形式,通常用于储存用户设置,或者用于存储捆绑的信息,在iOS开发中是一种很常用的数据存储格式。除了可以手动操作之外,mac还提供了使用指令操作plist文件的工具---PlistBuddy,在使用脚本修改plist文件时非常有用.

PlistBuddy是Mac OS操作系统自带的操作工具,不过Mac OS并没有在环境变量中默认配置该工具,所以不能在全局使用,只能通过路径引用.

/usr/libexec/PlistBuddy

当然如果你对该工具的使用特别频繁,你可以将其拷贝到

/usr/local/bin

目录下,就可以在全局使用.使用PlistBuddy -h查看一下支持的基本操作:

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

初始化

Plist中并没有提供专门的初始化命令,不过可以借助shell自带的命令来完成初始化创建.

#1.初始化一个test.plist文件

touch test.plist

#2.指定文件格式和编码格式

echo "<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict></dict></plist>" > test.plist

添加

添加一个非集合的key

# -c 表示直接执行后边的指令,否则会进入人机交互模式

# Add 指令后有一个空格,然后跟上需要添加的key

# string 表示要添加的value的数据类型,然后跟上value的值

PlistBuddy -c "Add :CFBundleIdentifier string Ericydong.WeChatCodesign" test.plist

添加一个集合

#添加集合需要两步走

#1. 添加集合

#2. 添加元素

 

#数组

PlistBuddy -c "Add :UISupportedInterfaceOrientations array" test.plist

PlistBuddy -c "Add : UISupportedInterfaceOrientations:0 string UISupportedInterfaceOrientations" tst.plist

 

#字典

PlistBuddy -c "Add :UIApplicationSceneManifest dict" test.plist

PlistBuddy -c "Add :UIApplicationSceneManifest:UIApplicationSupportsMultipleScenes bool false" test.plist

删除

#要删除某个key对应的全部内容

PlistBuddy -c "Delete :CFBundleIdentifier" test.plist

 

#删除数组中的指定下标元素

PlistBuddy -c "Delete :UISupportedInterfaceOrientations:0" test.plist

 

 

#删除字典中的指定的key

PlistBuddy -c "Delete :UIApplicationSceneManifest:UIApplicationSupportsMultipleScenes" test.plist

 

 

修改

#要修改的value在最外层

PlistBuddy -c "Set :CFBundleIdentifier Ericydong.WeChatCodesignNew" test.plist

 

#要修改的value在字典中

PlistBuddy -c "Set :UIApplicationSceneManifest:UIApplicationSupportsMultipleScenes true" test.plist

 

#要修改的value在数组中

PlistBuddy -c "Set :UISupportedInterfaceOrientations:0 UIInterfaceOrientationLandscapeLeft" test.plist

 

 

 

查询

#查询整个plist

PlistBuddy -c "Print" test.plist

 

#查询最外层的key

PlistBuddy -c "Print :CFBundleIdentifier" test.plist

 

#查询数组中的元素

PlistBuddy -c "Print :UISupportedInterfaceOrientations:0" test.plist

 

#查询字典中的元素

PlistBuddy -c "Print :UIApplicationSceneManifest:UIApplicationSupportsMultipleScenes" test.plist

合并

#重新创建一个test2.plist,然后将test.plist合并到test2.plist中

PlistBuddy -c "Merge test.plist" test2.plist

清空

#清空test.plist中的所有value,保留根字典

PlistBuddy -c "Clear" test.plist

 

 

#清空以后是这个样子

Dict {

}

复制

#使用Copy命令

#注意:Copy命令只能新增属性,不会覆盖已有属性.例如以下指令如果CFTargetName已经存在,则会抛出异常并不会覆盖原来的值(Copy: ":CFBundleIdentifiernew" Entry Already Exists)

PlistBuddy -c "Copy :CFBundleIdentifier : CFTargetName" test.plist

最新回复(0)