So I’ve been meaning to start using PlistBuddy for a while, and this morning I ran into a real need for it.
Unbeknownst to me, we had a problem with our imaging system where a machine would be freshly imaged, and it would retain the LocalHostName and ComputerName of the imaged machine until the next reboot.
(We normally set these values to “hostname -s” as part of a StartupItem)
Anyway, I wanted to set these values when machines are booted from our custom NetInstall image, and quite frankly, using “defaults” with nested dictionaries can sometimes either be an absolute pain in the ass, or impossible.
Obviously someone at Apple realised this a while ago, which is why they invented PlistBuddy. If you do “locate PlistBuddy” from a shell, you’re likely to see it in all sorts of Apple packages, and I feel perfectly comfortable simply stealing one of them…
PlistBuddy lets you easily write nested values and merge dictionaries into each other.
Here’s the two liner that lets you set the relevant values (as root/sudo etc)
$ PlistBuddy -c "Set :System:Network:HostNames:LocalHostName $(hostname -s)" \ /Library/Preferences/SystemConfiguration/preferences.plist $ /PlistBuddy -c "Set :System:System:ComputerName $(hostname -s)" \ /Library/Preferences/SystemConfiguration/preferences.plist
And for us we just point it to /Volumes/System/Library/… etc as that’s where the SOE partition is while NetBooting.
Much nicer than using defaults…
hmm.
Here’s another example. Manage the IR receiver:
PlistBuddy -c ‘Set :DeviceEnabled True’ /Library/Preferences/com.apple.driver.AppleIRController.plist
A nice Applescript example of Plistbuddy usage
http://bbs.applescript.net/viewtopic.php?id=18380
It looks like Plistbuddy in Leopard is now in a pretty short, easy, and consistent location: /usr/libexec/PlistBuddy
My friend Dave and I found another nice use for PlistBuddy, for tracking Xcode build numbers in a property list.
http://davedelong.com/blog/2009/04/15/incrementing-build-numbers-xcode
Thanks for blogging about PlistBuddy — we were looking for a good way to edit plist files from the command line!
Isn’t systemsetup -setcomputername and -setlocalsubnetname
now the best way to set hostname from the command line? scutil is what a search of dev.apple.com comes up with, but didn’t seem to work in a quick test. I threw this PlistBuddy way of doing it into a script called by launchd during first boot of my InstaDMG’d image and it worked here in 10.5.7, but just wondering.
May I additionally ask what you mean by SOE, and how does the pointing work/allow you to get the applicable computer/local name values?
It is totally at your discretion to shed light on your(probably quite out of date at this point) setup, but any response would of course be greatly appreciated. Thanks!
So SOE seems to be an Australian-only term… Standard Operating Environment.
The point of this example was to show how you can manipulate a rather nested key when not actually booted from that volume. scutil and systemsetup (which I think wasn’t available when I wrote this post) both operate on the currently booted volume.
Twiddling the file directly allows you to manipulate the setting on a non-boot volume.
This was for a post-action for our imaging system.