Toggle Night Shift

Toggle Night Shift


Toggle Night Shift.kmmacros (2.9 KB)

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
2 Likes

Thanks for sharing.
I hadn't tried NS on the Mac until now. Not sure if I like it or not. :wink:
Running macOS 10.12.6.

It's no True Tone but it's all right :slightly_smiling_face:

Two quick notes on this AppleScript:

  1. The System Preference pane 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
    
  2. 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".
4 Likes

@CJK, thanks, that's a cool tip.

Taking that to the next level, the obvious question is "how do we know the names of the panes and anchors"?

Well, this short script run in Script Debugger 7 can give us the answer:

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
2 Likes

Thank you both @CJK @JMichaelTX I'll try to modify my touchbar script later on.

1 Like

@CJK thank you! It works as expected once I changed this line to ""Turn On Until Tomorrow" (10.13.6)

Continuing the discussion from Toggle Night Shift:

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.