Skip to main content
Solved

Enabling Location Services Programmatically via Casper


Forum|alt.badge.img+3

I am trying to enable Location Services for 10.10 systems via Casper but I cannot find any option with configuration profiles to enable it and the few scripts I have found online do not seem to enable location services.

Specifically I am trying to enable location services to allow for OS X to update the timezone based on the location of the system.

Below is one of the failed scripts I had attempted. https://www.afp548.com/2013/07/30/enabling-location-services-programmatically/

Best answer by jhbush

@bthomas this has been working for me. I'm not sure where I picked it up from.

#!/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
View original
Did this topic help you find an answer to your question?

Forum|alt.badge.img+16
  • Valued Contributor
  • March 13, 2015

There's this discussion from Oct '11 on disabling. I'd think it'd be a simple change to Enable. But I can't get it to work (change Disabled to false to enable is how I understand it).


RobertHammen
Forum|alt.badge.img+28
  • Esteemed Contributor
  • March 13, 2015

Here's what I do, which seemed to work. Stole this from someone.

launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid=tick/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57tick
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

replace tick with the tick mark from the tilde key (upper left corner on a US keyboard, JAMF Nation interprets this somehow, @jake can this behavior be fixed or changed?


Forum|alt.badge.img+18
  • Valued Contributor
  • March 13, 2015

@RobertHammen I would think if you put it in the quoted text and escape the tick ` it might have worked

uuid=`system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57`

or apparently you have to escape the ticks to get them to show up in both just `


jhbush
Forum|alt.badge.img+26
  • Esteemed Contributor
  • March 13, 2015

@bthomas this has been working for me. I'm not sure where I picked it up from.

#!/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

Forum|alt.badge.img+3
  • New Contributor
  • March 14, 2015

Thank you all for the quick responses however I cannot get the script to enable location services via a policy which calls the the script. I manually ran the on a test system and I believe the problem comes in with the uuid= line because this is the output of grep and cut. I believe the last line is the intended output but I am not that great with scripting to know how to clean up the output.

2015-03-13 19:36:58.810 system_profiler[511:11331] platformPluginDictionary: Can't get X86PlatformPlugin, return value 0 2015-03-13 19:36:58.813 system_profiler[511:11331] platformPluginDictionary: Can't get X86PlatformPlugin, return value 0 564DFCEF-B4E6-7B6A-BE36-42ACC85B9ED0

jhbush
Forum|alt.badge.img+26
  • Esteemed Contributor
  • March 14, 2015

@bthomas I just tested this out again and location services activated as expected. By chance are you running an older version of 10.9?


Forum|alt.badge.img+3
  • New Contributor
  • March 14, 2015

@jhbush1973 I am running 10.10.2 on my test systems. Production systems are the same OS version. The OS is built using AutoDMG from the 10.10.2 installer downloaded from the Mac App Store.


Forum|alt.badge.img+13

Try this script with an additional line inserted;

#!/bin/sh

# enable location services
/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd."$uuid" LocationServicesEnabled -int 1
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.notbackedup."$uuid" LocationServicesEnabled -int 1
/usr/sbin/chown -R _locationd:_locationd /var/db/locationd
/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

exit 0

Forum|alt.badge.img+3
  • New Contributor
  • March 16, 2015

@jhbush1973 Your script did work for me. I found that it didn't work on my VM due to some weirdness with the hardware ID. I can now enable location services on 10.10.2. Thanks for everyones help!


Forum|alt.badge.img+7
  • New Contributor
  • January 11, 2016

This does not turn location services on a 10.11 machine. Anyone try the above command on Elcapitan?


bpavlov
Forum|alt.badge.img+18
  • Esteemed Contributor
  • January 11, 2016

Works for me. This is the script I use which is meant to use Casper variables which you need to populate.

#!/bin/sh

#Use "/usr/sbin/systemsetup -listtimezones" to see a list of available list time zones.
#This script is meant to be used with Casper.
# $4 variable should be filled with the TimeZone information (ie: America/Chicago, America/New_York )
# $5 variable should be filled with the TimeServer information (ie: time.apple.com )
TimeZone="$4"
TimeServer="$5"

############# Pause for network services #############
/bin/sleep 10
######################################################

#Disable network time
/usr/sbin/systemsetup -setusingnetworktime off

#Set an initial time zone
/usr/sbin/systemsetup -settimezone "$TimeZone"

#Set specific time server
/usr/sbin/systemsetup -setnetworktimeserver "$TimeServer"

# enable location services
/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid=`ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}'`

/usr/bin/defaults write /private/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd."$uuid" LocationServicesEnabled -int 1
/usr/bin/defaults write /private/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.notbackedup."$uuid" LocationServicesEnabled -int 1
/usr/sbin/chown -R _locationd:_locationd /var/db/locationd
/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

# set time zone automatically using current location
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

#Re-enable network time
/usr/sbin/systemsetup -setusingnetworktime on

#Detect the timezone
/usr/sbin/systemsetup -gettimezone

#Get the time from time server
/usr/sbin/systemsetup -getnetworktimeserver

exit 0

dvasquez
Forum|alt.badge.img+16
  • Valued Contributor
  • August 18, 2016

@bpavlov your script works well.

Thank you for sharing. I was concerned about the enable locations services portion and "ioreg" command to get the system uuid but I get why your doing it, very cool.

I can say this works in 10.11.6

Thank you


russeller
Forum|alt.badge.img+15
  • Valued Contributor
  • November 20, 2016

This seemed like the right place to put this. Been testing my 10.12 first boot script and found that defaults will put the UUID of a ByHost preference in the plist name itself now in 10.12. The original script will actually write the UUID to the plist twice (once by defaults and once by the uuid string) when ran. I removed the uuid stuff from the above script and it works for me. Here is an updated version for 10.12:

#!/bin/bash

## Unload locationd
launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist

## Write enabled value to locationd plist
defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1

## Fix Permissions for the locationd folder
chown -R _locationd:_locationd /var/db/locationd

## Reload locationd
launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

exit 0

Hope this helps and please let me know if there is improvement needed.


Forum|alt.badge.img+11
  • Contributor
  • November 29, 2016

@ssrussell thanks for this.. I will give it a twirl..


Forum|alt.badge.img+9
  • Contributor
  • December 2, 2016

@ssrussell I've combined the lengthier script above with your script and I'm not having much luck. I'm in Seattle but as a test I'm setting the initial time zone as "America/New_York" and setting the timer server as "time.apple.com," however even after everything gets reloaded it won't update the time zone to be Seattle or Cupertino or any city in the Pacific time zone.

For clarity's sake, here's the script that I'm trying:

#!/bin/sh

#Use "/usr/sbin/systemsetup -listtimezones" to see a list of available list time zones.
#This script is meant to be used with Casper.
# $4 variable should be filled with the TimeZone information (ie: America/Chicago, America/New_York )
# $5 variable should be filled with the TimeServer information (ie: time.apple.com )
TimeZone="America/New_York"
TimeServer="time.apple.com"

############# Pause for network services #############
/bin/sleep 10
######################################################

#Disable network time
/usr/sbin/systemsetup -setusingnetworktime off

#Set an initial time zone
/usr/sbin/systemsetup -settimezone "$TimeZone"

#Set specific time server
/usr/sbin/systemsetup -setnetworktimeserver "$TimeServer"

## Unload locationd
launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist

## Write enabled value to locationd plist
defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1

## Fix Permissions for the locationd folder
chown -R _locationd:_locationd /var/db/locationd

## Reload locationd
launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

# set time zone automatically using current location
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

#Re-enable network time
/usr/sbin/systemsetup -setusingnetworktime on

#Detect the timezone
/usr/sbin/systemsetup -gettimezone

#Get the time from time server
/usr/sbin/systemsetup -getnetworktimeserver

The output I'm getting is:

setUsingNetworkTime: Off Set TimeZone: America/New_York setNetworkTimeServer: time.apple.com setUsingNetworkTime: On Time Zone: America/New_York Network Time Server: time.apple.com

Any suggestions?


russeller
Forum|alt.badge.img+15
  • Valued Contributor
  • December 2, 2016

@cgiordano When you mentioned setting the Time Zone to Seattle or Cupertino are you talking about setting the TimeZone variable to something like: America/Seattle if so, I found that from this command sudo systemsetup -listtimezones which this link has the list, it doesn't have Seattle or Cupertino as options. Your only option is America/Los_Angeles. Using New York and LA worked for me when I used your script and switched them back and forth. I hope I understood your question correctly, but as far as I can tell your script works perfectly.

EDIT: words are hard


Forum|alt.badge.img+9
  • Contributor
  • December 2, 2016

@ssrussell Maybe I'm misunderstanding the purpose of the script but at the end of the script should it be automatically setting the location? If so, it's not doing that for me. It's leaving the TimeZone set to NY. I thought it was supposed to set the time zone automatically...


russeller
Forum|alt.badge.img+15
  • Valued Contributor
  • December 2, 2016

@cgiordano I'm sorry, the purpose was basically to click the "Enable Location Services" checkbox under "Security & Privacy" PrefPane. This makes it to where when the user launches an app like Maps.app it'll prompt them to allow Maps to use Location Services (which doesn't require admin rights). If that checkbox isn't checked then it requires admin rights to check it from that "Security & Privacy" PrefPane. This could lead to frustrated non-admin users.

I'm not sure how to check this box, this is what you want:


Forum|alt.badge.img+9
  • Contributor
  • December 2, 2016

Yup...I got it now. I guess I was getting confused by the other scripts in this post. I'm not sure the point setting a time zone if we're automatically going to set it via enabling "Set time zone automatically using current location" with the command:

/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

Thanks for the assistance and the clarity.


Forum|alt.badge.img+7
  • Contributor
  • December 16, 2016

@ssrussell thank you for sharing! Works great in Sierra.


Forum|alt.badge.img+4
  • New Contributor
  • January 9, 2017

@ssrussell Thanks! That script worked for me as well! 10.12


Forum|alt.badge.img+10
  • Contributor
  • March 2, 2017

@ssrussell Thanks for the script that is perfect. Do you know if there is a way to enable all items in the Location Services to be turned on?


Forum|alt.badge.img
  • New Contributor
  • March 15, 2017

Wondering the same thing that ssrussell is. Is this possible, because I am not seeing that the apps can be enabled within location services with a non-admin.


russeller
Forum|alt.badge.img+15
  • Valued Contributor
  • March 15, 2017

@ammonsc @btrabue I'm not sure about how to enable all the items in Location Services programmatically. I'll continue to test and let you know what I find.


Forum|alt.badge.img+8
  • Valued Contributor
  • May 10, 2017

@ssrussell Thanks for the Sierra update of this script.

Nevertheless, when I try to run it directly into the Terminal I get an error saying something like "operation not permitted while system integrity protection is engaged". Do you know if this is something I might be doing wrong or a configuration that I need to change in order to integrate this into a larger script?

Thanks!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings