macOS lets you edit property list files in Terminal.
Email Facebook x.com Reddit
Property lists are essentially XML files with the .plist extension. Here's how to edit them using the command line in the macOS Terminal app.
XML is an open data format that gained popularity in the mid-1990s when the Internet first became commercialized. It is a text-based format that uses key/value pairs to store data. Keys provide labels for the data, and values store the data associated with each key.
Data types in XML include Booleans (true/false), numbers, dates, strings (text), arrays, dictionaries, and plain data. A dictionary is simply a linked table of values, also having its own set of keys – one for each piece of data.
By combining and embedding these data types in an XML file, you can store a variety of nested data for anything. Although XML is typically stored as plain text, it has also become one of the standards for exchanging data over the Internet – although today it has been largely replaced by JSON (Javascript Object Notation), which is somewhat similar.
XML on Apple platforms
When Mac OS X was first released in 2000, Apple made it clear that it would adopt XML as the file format for much of the Mac operating system. Apple uses a proprietary file format called Property Lists (.plist), which is simple XML with some custom Apple XML header information at the top of each file.
You may have seen .plist files in the macOS preferences folders in /Library/Preferences or ~/Library/Preferences. These are simple XML files that contain lists of XML data that can be read by applications or by macOS itself to store preferences.
For example, the macOS Finder preferences file is located in the Preferences folder and is called com.apple.Finder.plist. Most .plist preferences files use this form of reverse DNS notation: the second component in the filename identifies the company that makes the software, then the name of the application, then the .plist extension.
You can open a .plist file in Apple's TextEdit app to read it as raw text, or you can open it in Apple's developer IDE, Xcode – or most other plain-text editing apps.
Xcode has a special formatting feature that displays a .plist file as a table editor, with each data type on a row containing the data type and its key. By clicking the pop-up menu next to each item in the table, you can change its type to any known formatted kind.
.plist data of common, unknown, or opaque types is treated as blob data, or in the case of Apple's platform programming, as the Data data type (in Swift) or NSData in Objective-C.
Apps also bundle .plist files internally on macOS, iOS, iPadOS, and watchOS for both app descriptions and content storage. For example, the Info.plist file describes each app and its capabilities to the operating system.
Apple operating systems have system APIs for converting and serializing XML and .plist to and from other data formats.
Editing Property Lists
As mentioned, you can edit .plist files directly by opening them in either a text editor or in Xcode.
If you open a .plist file in TextEdit, for example, you'll see unformatted XML with tags. To edit the data in a .plist file in a text editor, you'll need to understand XML tags and how they work. XML tags are very similar to HTML tags.
In Xcode, you can simply open the .plist file, or you can add it to the Xcode project window and then click it once in the Project Navigator on the left. This will display the contents of the .plist in the pane on the right:
Editing a .plist file in Apple's Xcode.
The window above shows the version.plist file for Apple's Chess app: each row is a single data item, the keys for each item are listed in the left column, each data type is shown in the center column, and the value of each key is shown in the column on the right.
To edit a .plist file's data in Xcode, either single-click on the data or key of a single row and type in new information, or click the small pop-up menu in the center column of a row to change its type. The pop-up menu only lists known, valid .plist data types.
Once you've made all the changes you want, simply choose File -> Save .plist File (or press Command-S on your keyboard).
One of the great benefits of XML is that you can edit files on any platform, save them, and then copy them between computers without having to convert them. Software localization is often done this way, with strings of text stored in .strings files for translation between languages. Strings files also contain standard XML using key/value pairs.
InfoPlist.strings files packaged inside apps contain localized versions of the strings found in the descriptive information used to identify the app. This is the text that appears, for example, when you do File -> Get Info about an app in the Finder.
More recently, the version.plist file packaged inside each app might contain the app's version information, stored in XML format using Apple keys such as CFBundleVersion and CFBundleShortVersionString.
Apple has a section in their developer documentation that discusses info property lists.
There's also a Info Property List Key Reference – though it's decades old and the property list keys mentioned are incomplete.
The “CF” prefix in Apple's .plist keys represents Core Foundation, a C-based API used to manage basic data types and .plists on Apple platforms.
Strings Files in Apple Platform Development
In Apple development, strings are often stored in a .strings file or string table for localization. Developers may outsource their strings files to translation companies so that they don't have to do the translation themselves.
If you look inside an app's package (folder), you may see multiple .strings files stored in language folders, with each folder ending in “.lproj” and having a two-character ISO prefix for the country name. By duplicating and changing the contents of each version of the strings files, developers can add new languages to app interfaces without knowing those languages.
macOS and iOS are smart enough to load the correct localized strings file or table for the current language used on the Apple device.
By using separate strings and .plist files, an app's UI can be changed at runtime or later – without having to compile the text into the app's binary. This is called dynamic loading.
This is why you can change the system language in Settings on an Apple device and have the same app update its UI text for the new language you select. In older operating systems, vendors had to ship separate language versions of an app for each country or language.
Dynamic loading also reduces the memory footprint of an app at runtime, since strings are not loaded into memory until they are actually used.
You can learn more about .plist formats and CF types in Terminal by typing “man plist”.
Editing property lists in Terminal
macOS includes the (UNIX) Terminal (shell) application, which lets you type commands at the command line to perform actions. One of the most powerful uses of Terminal is batch processing and scripting to automate processes.
Many Terminal commands include the -r (recursive) flag, which tells the command to continue processing all files it finds in a given folder, no matter how deeply those files are nested.
You can use Terminal to both manually and automatically process .plist and .strings files for faster editing.
This can save time, for example, if you have a batch of files for several languages and want to replace all their values with new localized text from language tables or other input. Or you may want to change all the keys for a particular item in a batch of files at once, without editing each file manually.
We won't go into shell script automation in this article, but there are many good books and online tutorials on writing shell scripts for batch processing.
OS X used to come in a box
Editing a property list in Terminal manually
To edit a .plist file in Terminal, use the built-in defaults command. defaults allows you to edit and view .plist files, as well as set system-wide preferences for the operating system's known .plist files using names.
To fully utilize the defaults commands in Terminal, type:
man defaults and press Return on your keyboard.
To exit man in Terminal, press Control-Z.
The primary parameter for defaults to change a value in an existing .plist file is the write parameter followed by the new data to write.
As the man page mentions, the new data must be in a specific format (usually another .plist or dictionary) and must contain the keys and values to write. The format of this data must be precise, otherwise the command may fail or the file data may be corrupted.
For example, the man page shows changing an array in a .plist named “Default Color” to a new value of (255,0,0). To do this, you would use the command:
defaults write com.companyname.appname “Default Color” '(255, 0, 0)'
Where “appname” is the name of the app for the company “companyname”.
You can also overwrite existing values in a .plist with another .plist. For example:
defaults write com.companyname.appname '{ “Default Color” = (255, 0, 0); “Default Font” = Helvetica; }';
In this example, the new .plist data is enclosed in curly braces and contains two key/value pairs: “Default Color” and “Default Font”.
Be careful when writing to existing preferences files, because if you corrupt the data in the .plist file used by macOS, your Mac may stop working properly.
There are also delete options for the default values, but keep in mind that delete is even more dangerous than the write option. Some delete options destroy data and can even delete all data in a domain with a single command.
You can print a list of all the domains on your Mac using the domains option:
Default domains
You can learn more about .plist formats in Terminal by typing “man plist”.
Editing a property list file outside a settings domain
To edit any arbitrary .plist file located in any writable location on the file system, use the filepath option. This lets you edit the .plist just as you did above for preferences files, but for any .plist file at any specified path. filepath also works with the read command.
For example:
defaults read ~/Library/Containers/com.apple.TextEdit/Data/Library/Preferences/com.apple.TextEdit.plist
reads the TextEdit application preferences file located in the user's Library folder.
But the filepath option works for any .plist file, not just preferences files or files belonging to apps.
By writing and combining the defaults commands into an automated script file, you can see how easy it would be to batch process .plist files without much effort.
In fact, most third-party Apple software is built this way: typically, a build engineer writes automation scripts to fetch the codebase from a server, batch process the localized .plist files and strings, and then run builds on all the pieces of software. The builds are then typically post-processed using another set of script files to assemble the final software for release.
The defaults command also has options to add or replace certain types of data in the .plist file.
The defaults system has been around in macOS, Mac OS X, and iOS for decades, and was actually one of the original technologies used in NeXTStep, the predecessor to most modern Apple operating systems.
Once you get the hang of the defaults system, you'll find yourself using it a lot to inspect and change macOS settings and to edit .plist files. Just make sure you feel comfortable and confident with it before using it.
Apple has a short section in the Terminal user guide that describes how to use the default system to edit .plist files and preferences.
If you're using a Windows machine, check out the really cool XML editing tools made by Altova.
Follow AppleInsider on Google News