Enabling Location Services Programmatically

Location Services is an important part of OS X, but it is not obvious on how to enable it without going through the System Preferences GUI.  The preference that controls it is not in /Library/Preferences, and due to cfprefsd it is hard to find.

Thanks to Tim Sutton for help in finding where this preference lives.

It is a ByHost preference that exists in locationd’s container, at /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid.

To enable it, use this script:

#!/bin/sh
launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid=`/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57`
defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled -int 1
chown -R _locationd:_locationd /var/db/locationd
launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist
 
exit 0