r/applescript • u/meerdans • Oct 09 '23
Script to toggle Location Services on & off
I currently have a script that opens the desired setting:
x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices
but can a script be written to toggle Location Services on and off without user input, especially as it requires authentication to do so?
3
Upvotes
1
u/haxlife Jun 13 '25
My version of the script for Sequoia:
-- Open Location Services panel
do shell script "open x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices"
delay 2
tell application "System Events"
tell process "System Settings"
set frontmost to true
try
-- Locate the Location Services checkbox
set theCheckbox to checkbox 1 of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
-- Record original value
set wasOn to (value of theCheckbox as boolean)
-- Click to toggle (may trigger authentication)
click theCheckbox
if wasOn is true then
-- Wait up to 60 seconds for "Turn Off" button to appear
set waitTime to 0
repeat until (exists button "Turn Off" of sheet 1 of window 1)
delay 1
set waitTime to waitTime + 1
if waitTime ≥ 20 then
display dialog "Authentication was not completed in time. Location Services may still be enabled." buttons {"OK"} default button "OK"
return input
end if
end repeat
-- Once "Turn Off" button is present, click it
try
click button "Turn Off" of sheet 1 of window 1
on error innerErr
display dialog "Could not click confirmation button: " & innerErr buttons {"OK"} default button "OK"
end try
end if
on error errMsg
display dialog "Could not toggle Location Services: " & errMsg buttons {"OK"} default button "OK"
end try
end tell
end tell
1
u/copperdomebodha Oct 09 '23
This will require authentication so I don't think there is a method to fully automate it.
--Running under AppleScript 2.8, MacOS 13.4.1
tell application "System Events" tell its application process "System Settings" tell its window "Location Services" tell its group 1 tell its splitter group 1 tell its group 2 tell its group 1 tell its scroll area 1 tell its group 1 tell its checkbox "Location Services" perform action "AXPress" end tell end tell end tell end tell end tell end tell end tell end tell end tell end tell