tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
delay 0.3
tell application "System Events"
tell process "System Preferences"
click radio button 3 of tab group 1 of window 1
click checkbox 1 of tab group 1 of window 1
end tell
end tell
end tell
tell application "System Preferences" to quit
The System Preferencepane objects contain a set of anchors, which includes any tabs that sit at the top of the preference pane. In this case, there are three of them. Instead of asking System Events to click radio button 3 of tab group 1 of window 1, you can get System Preferences to
reveal the anchor named "displaysNightShiftTab" of the current pane
The best thing about System Preferences is that it doesn't need to be active or visible in order to have its UI scripted through System Events. Currently, telling it to activate brings it into focus, where the user can see the window change pane, then change tab, then the checkbox ticked or unticked. For a more seamless, background experience, remove that command.
Here's what the resulting final script might look like:
tell application "System Preferences" to reveal the ¬
anchor named "displaysNightShiftTab" of ¬
pane id "com.apple.preference.displays"
tell application "System Events" to tell ¬
process "System Preferences" to tell ¬
window 1 to tell ¬
tab group 1 to tell ¬
checkbox "Turn On Until Later Today" to ¬
perform action "AXPress"
quit application "System Preferences"
In this version, the user never sees a single glimpse of the System Preferences window, but only the desired effect of initiating or terminating Night Shift.
Note:checkbox 1 is probably preferable for portability, as non-English systems won't understand checkbox "Turn On Until Later Today".
tell application "System Preferences"
set paneList to id of every pane
set anchorList to name of anchors of pane id "com.apple.preference.displays"
end tell
This no longer appears to work (or at least isn't working for me) in Mojave (10.14.3).
if I remove the activate line and SysPrefs is not running, I get this error:
/var/folders/b2/cfyrg7n10s383v_djx2n4g7h0000gn/T/Keyboard-Maestro-Script-F6E4C1C0-A6D0-4A40-9CE7-C89A514BBBDA:224:267: execution error: System Events got an error: Can’t get checkbox 1 of tab group 1 of window 1 of process "System Preferences". Invalid index. (-1719)
Here's the AppleScript I've been using (without activate):
tell application "System Preferences"
reveal anchor "displaysNightShiftTab" of pane id "com.apple.preference.displays"
delay 1
end tell
tell application "System Events"
tell process "System Preferences"
click checkbox 1 of tab group 1 of window 1
end tell
end tell
tell application "System Preferences"
quit
end tell
Trying the script from @CJK post also results in the same error, even trying "Turn On Until Sunrise" and "Turn On Until Tomorrow" don't work.
EDIT: the issue appears to be that I have multiple displays. Even though "window 1" is the primary window for System Preferences, I have to call it by name if I have my external monitor plugged in.