Perl Plist
Perl Plist Script and Examples
This script contains perl code that uses the PerlObjCBridge to read and modify Mac OS X plist format files. Plist files are ubiquitous on Mac OS X. I originally wrote them for personal projects (article 1 and article 2) but they are so useful at work (Mac OS X system administration) that I keep improving and working on these scripts. Here is the latest version and some script examples and utilities.
- perlplist.pl - The shared library of functions. Save it to "/usr/local/lib". (Version 8.3.11 - Added 2011-03-22)
- superDefaults.pl - Very similar to the built in "defaults" command, but super.
- plcat - This script will print a plist file as xml no matter what the original format is (binary bleck). If you specify 2 plist files it will combine them. Values that differ will not be combined but show an error. Values can only be combined that are missing (dictionary keys or the end of an array). (Added 2011-03-22)
- pldiff - This script will compare 2 plist files and print out differences similarly to the diff command. (Added 2011-03-22)
- set_ip.pl - A script that parses OS X's network pref file and set's the IP of the current location. You can set it to DHCP or manual. You can set router and subnet mask as well. You can also specify an interface (Ethernet (default), Airport, or FireWire). It can also point to any preferences.plist file. It does not activate the settings, it just changes the file. Use ncutil to actually activate the settings.
- get_ip.pl - A script that parses OS X's network pref file and get's the IP of whatever is set to Manual (presumably Ethernet--yeah, this script is fairly basic).
- FoundationExamples - Here is another script containing all functions, but in object form (sent to me by HB Desiato). This does not have any of the new additions I've created,
Watch a presentation on this.
If you have questions, don't hesitate to contact me!
Features of the perlplist.pl script.
- Read and write plist files (all objects are original Cocoa objects referenced by a Perl pointer).
- Dig into a loaded plist object and read or write Cocoa values (without converting it to Perl).
- Convert Cocoa objects into Perl versions (NSDictionary to HASH, NSArray to ARRAY, all other values into SCALARS -- NSData is not dealt with).
- Convert Perl objects into Cocoa versions (HASH to NSDictionary, ARRAY to NSArray, SCALARS into NSStrings -- NSData is not dealt with).
- Create Cocoa objects NSDate and NSNumber (bool, int, float) from Perl SCALARS -- NSData is not dealt with.
- Convert a string representation of xml plist to an NSDictionary (which can be saved as plist).
Features of the script examples.
- Compare 2 plist files (using pldiff script).
- Print binary plist files as xml (using plcat script).
- Combine 2 plist files (using plcat script).
- Set the IP (router and subnet mask) of the active Ethernet, Airport, or FireWire interfaces of any preferences.plist file.
Examples
james% sudo superDefaults.pl /private/var/db/launchd.db/com.apple.launchd/overrides.plist --add -d com.openssh.sshd -d Disabled -b 0
james% sudo superDefaults.pl ~/Library/Preferences/com.apple.Safari.plist --add -d WebKitJavaScriptCanOpenWindowsAutomatically -b 0
james% sudo superDefaults.pl /Library/Preferences/com.apple.AppleFileServer.plist --add -d guestAccess -b 0
james% sudo superDefaults.pl /Library/Preferences/com.apple.Bluetooth.plist --add -d BluetoothAutoSeekHIDDevices -b 0
james% sudo superDefaults.pl /Library/Preferences/DirectoryService/DSLDAPv3PlugInConfig.plist --add -d "LDAP Server Configs" -a 0 -d "Record Type Map" -a 0 -d "Attribute Type Map" -a 4 -d "Native Map" -a 0 -s "#/Users/kiosk"
james% sudo superDefaults.pl /etc/authorization --add -d rights -d system.privilege.taskport.debug -d class -s allow
james% sudo superDefaults.pl /Library/Preferences/.GlobalPreferences.plist --add -d "com.apple.ColorSync.Devices" -d "Device.mntr.756E6B6E-0000-0717-0000-00005B81C5C0" -d "CustomProfiles" -d "1" -s "/Library/ColorSync/Profiles/Projector Profile.icc"
james% plcat test1.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>3</key>
<array>
<string>1</string>
</array>
<key>1</key>
<string>A</string>
<key>2</key>
<dict>
<key>2</key>
<string>B</string>
</dict>
</dict>
</plist>
james% plcat test2.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>3</key>
<array>
<string>1</string>
<string>2</string>
</array>
<key>2</key>
<dict>
<key>1</key>
<string>A</string>
</dict>
</dict>
</plist>
james% pldiff test1.plist test2.plist
> /{3}/[1]/2
< /{1}/A
< /{2}/{2}/B
> /{2}/{1}/A
james% plcat test1.plist test2.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>3</key>
<array>
<string>1</string>
<string>2</string>
</array>
<key>1</key>
<string>A</string>
<key>2</key>
<dict>
<key>1</key>
<string>A</string>
<key>2</key>
<string>B</string>
</dict>
</dict>
</plist>
Python
Philip Rinehart at Yale loves Python. He sent me this Python version of get_ip (it doesn't read binary plists afaik, but you can install and include pyobc to get around this, or you can run plutil -convert xml1 on the file first...). This is a lot cleaner than the perlplist examples.
- get_ip.py - A script that parses OS X's network pref file and get's the IP of the current location.