AI Summary 11.20550.1 Mac PlistBuddy Plist摘要 2026-10-09
plist是Mac 普遍的文件格式,类似xml 键值对的方式配置 PlistBuddy 是Mac自带的解析plist的小工具 Buddy为好朋友,伙伴的意思

Mac PlistBuddy Plist

Mac PlistBuddy Plist
/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


新建一个plist文件
/usr/libexec/PlistBuddy Info.plist
$ Command: save
$ Command: exit

用户文件夹下找到创建的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值
$ /usr/libexec/PlistBuddy -c ‘Add :Application: string app1’ Info.plist
$ /usr/libexec/PlistBuddy -c ‘Add :Application: string app2’ Info.plist
$ /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 :Person:Age string secret’ Info.plist
$ /usr/libexec/PlistBuddy -c ‘Add :Person:Name string Xuan’ Info.plist
$ /usr/libexec/PlistBuddy -c ‘Add :Person:sex string boy’ Info.plist
$ /usr/libexec/PlistBuddy -c ‘Add :Person:weight string 65’ Info.plist



添加字典字段

查看plist内容
/usr/libexec/PlistBuddy -c “print” Info.plist


查看字段内容
/usr/libexec/PlistBuddy -c ‘Print :Person’ Info.plist

查看特定下标的值
/usr/libexec/PlistBuddy -c ‘Print :Application:2’ Info.plist

删除特定字段值
/usr/libexec/PlistBuddy -c ‘Delete :Version’ Info.plist


修改特定字段值
/usr/libexec/PlistBuddy -c ‘Set :Application:1 “this is app1”’ Info.plist


# 将A.plist 合并到 B.plist中
/usr/libexec/PlistBuddy -c ‘Merge A.plist’ B.plist


若两个plist文件中与相同的键值,终端中会提示B.plist中有重复的键值,默认跳过该键值的合并