Skip to content

Instantly share code, notes, and snippets.

@jaoye
Last active July 16, 2024 15:08
Show Gist options
  • Save jaoye/33a5363173184e92ff50d0a93eb4fd4e to your computer and use it in GitHub Desktop.
Save jaoye/33a5363173184e92ff50d0a93eb4fd4e to your computer and use it in GitHub Desktop.
[Reading and modifying plist file on the command line] #cli #macOS #plist
打印值
/usr/libexec/PlistBuddy -c Print:CFBundleExecutable  Info.plist
合并
/usr/libexec/PlistBuddy -c 'Merge framework/Info.plist'  Info.plist

/usr/libexec/PlistBuddy -c Set :CFBundleIdentifier Info.plist
        Sets the CFBundleIdentifier property to com.apple.plistbuddy
        
/usr/libexec/PlistBuddy -c Add :CFBundleGetInfoString string "App version 1.0.1" Info.plist
        Adds the CFBundleGetInfoString property to the plist
        
/usr/libexec/PlistBuddy -c Add :CFBundleDocumentTypes: dict
        Adds a new item of type dict to the CFBundleDocumentTypes array
        
/usr/libexec/PlistBuddy -c Add :CFBundleDocumentTypes:0 dict
        Adds the new item to the beginning of the array
        
/usr/libexec/PlistBuddy -c Delete :CFBundleDocumentTypes:0 dict
        Deletes the FIRST item in the array
        
/usr/libexec/PlistBuddy -c Delete :CFBundleDocumentTypes
        Deletes the ENTIRE CFBundleDocumentTypes array

help plutil -h

验证

plutil -lint example.plist

打印

plutil -p example.plist

转换格式

可转换以下 xml1 binary1 json 格式

plutil -convert json example.plist 
plutil -convert binary1 example.plist 
plutil -convert xml1 example.plist

插入 删除 替换

 -insert keypath -type value   insert a value into the property list before writing it out
                               keypath is a key-value coding key path, with one extension:
                               a numerical path component applied to an array will act on the object at that index in the array
                               or insert it into the array if the numerical path component is the last one in the key path
                               type is one of: bool, integer, float, date, string, data, xml, json
                               -bool: YES if passed "YES" or "true", otherwise NO
                               -integer: any valid 64 bit integer
                               -float: any valid 64 bit float
                               -string: UTF8 encoded string
                               -date: a date in XML property list format, not supported if outputting JSON
                               -data: a base-64 encoded string
                               -xml: an XML property list, useful for inserting compound values
                               -json: a JSON fragment, useful for inserting compound values
                               value YES, NO, a number, a date, or a base-64 encoded blob of data

type:bool, integer, float, date, string, data, xml, json

值操作

plutil -insert BOOL_Key -bool "YES" example.plist   
plutil -insert Count_Key -integer 10 example.plist
plutil -insert float_Key -float 3.1415926 example.plist
plutil -insert String_Key -string "String_Value" example.plist
plutil -remove String_Key example.plist

数组操作

plutil -insert listStyle -xml "<array/>" example.plist
plutil -insert listColor -json "[]" example.plist

plutil -insert listStyle.2 -string "Suite" example.plist 
plutil -replace listStyle.2 -string "Suite" example.plist 
plutil -remove listStyle.2 

字典操作

plutil -insert Employee -xml "<dict/>" example.plist

plutil -insert Employee.LastName -string "Liu" example.plist
plutil -replace Employee.LastName -string "Wang" example.plist
plutil -remove Employee.LastName 

嵌套操作

plutil -insert Date -xml "<date>2019-04-24 09:48:49</date>" example.plist
plutil -insert Date -json "{"S":"string","F":3.14,"B":true}" example.plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment