PlistBuddy is mac build-in program which help the user to edit .plist
file.
When you are working with CFBundleXXXX
file, it is useful for editing. Different with other program, PlistBuddy
is not set as default path. If you need to run PlistBuddy
, you need to run usr/libexec/PlistBuddy
.
Check Help Menu
Type /usr/libexec/PlistBuddy --help
to display the PlistBuddy
help menu. The menu is not very long, you can have a look for that. (check the below image)
2 Different Edit Modes
There are 2 different edit modes when you use PlistBuddy
to edit the plist file. I use Edit Mode and Direct Mode to name them. And, we will use Edit Mode to explain the command first. After then, I will list the Direct Mode command.
- Edit Mode: enter to edit mode like vi edit, you have to save at the end if you want to apply the change. (more easy and least cmd to type)
- Direct Mode: type cmd in Terminal and it will apply the change to the file immediately.
Let’s open your Terminal and start type the command.
Edit Mode
1. Create .plist
/usr/libexec/PlistBuddy <your_file_location>/info.plist
2. Add key value pair
add :version string 1.0
3. Print plist content
print
4. Add array
add :student arr
add :student: string tom
add :student: string may
add :student: string john
5. Add dictionary
add :activity dict
add :activity:swim string 10-6
add :activity:running string 9-6
6. Save
save // save the file
7. Delete element, array or dictionary
delete :version
delete :student
delete :activity:swim
delete :activity
8. Revert
revert // revert back to last saved file
9. Exit
exit // exit Edit Mode
Direct Mode
Different with Edit Mode, Direct Mode will apply the change immediately, save and exit is useless in here.
The basic format of cmd is
/usr/libexec/PlistBuddy -c '<command> :<key> <type> <value>' <file_name>
1. Create .plist and add key-value pair
/usr/libexec/PlistBuddy -c 'Add :Version string 1.0' info.plist
This cmd will create a file called info.plist
in your current directory with the following content.
Open the info.plist
, you can see the following content.
2. Print plist content
/usr/libexec/PlistBuddy -c 'print' info.plist
The display content don’t show <xml>
and related tag, it only shows the tag inside <plist>
.
3. Add array
/usr/libexec/PlistBuddy -c 'Add :student array' info.plist
/usr/libexec/PlistBuddy -c 'Add :student: string tom' info.plist
/usr/libexec/PlistBuddy -c 'Add :student: string may' info.plist
/usr/libexec/PlistBuddy -c 'Add :student: string john' info.plist
4. Add Dictionary
Type the following cmd
/usr/libexec/PlistBuddy -c 'Add :activity dict' info.plist
/usr/libexec/PlistBuddy -c 'Add :activity:swim string 10-6' info.plist
/usr/libexec/PlistBuddy -c 'Add :activity:running: string 9-6' info.plist
5. Delete element
/usr/libexec/PlistBuddy -c 'Delete :version' info.plist
/usr/libexec/PlistBuddy -c 'Delete :student' info.plist
/usr/libexec/PlistBuddy -c 'Delete :activity:swim' info.plist
/usr/libexec/PlistBuddy -c 'Delete :activity' info.plist
Summary
PlistBuddy
is a useful tool to work with Mac and iOS development, and it is easy to learn and apply.
Please like or share this article if you enjoy 😄