Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Trying to write script to toggle grayscale (Big Sur)

I'm trying to write a script to toggle grayscale on my Mac running Big Sur. This is my first time trying to write something, so please excuse my ignorance. I've found some help on this for other OS versions, but something isn't working here. Can anyone tell from the screen shot of the script, System Preferences Window, and error what I've got wrong here? Thanks so much!


Posted on May 13, 2021 2:26 PM

Reply
Question marked as Best reply

Posted on May 15, 2021 7:07 AM

From Indie Stack, a very short, and very proprietary means to toggle macOS screen grayscale on and off. Copy/paste the following into Script Editor, compile, and run:


use scripting additions

-- reference: https://indiestack.com/2019/04/toggle-system-grayscale-mode/

-- Line up a Python script for dynamically loading 
-- the private framework  and invoking the required
-- private methods to get current grayscale mode
-- and set it to the opposite value.
set toggleGrayScript to "python -c 'from ctypes import cdll
lib = cdll.LoadLibrary(\"/System/Library/PrivateFrameworks/UniversalAccess.framework/UniversalAccess\")
lib.UAGrayscaleSetEnabled(lib.UAGrayscaleIsEnabled() == 0)
'"
do shell script toggleGrayScript
return


Any future release of macOS could break this solution, but until it does, it is a low code effort that works.


Similar questions

7 replies
Question marked as Best reply

May 15, 2021 7:07 AM in response to caitlinshall

From Indie Stack, a very short, and very proprietary means to toggle macOS screen grayscale on and off. Copy/paste the following into Script Editor, compile, and run:


use scripting additions

-- reference: https://indiestack.com/2019/04/toggle-system-grayscale-mode/

-- Line up a Python script for dynamically loading 
-- the private framework  and invoking the required
-- private methods to get current grayscale mode
-- and set it to the opposite value.
set toggleGrayScript to "python -c 'from ctypes import cdll
lib = cdll.LoadLibrary(\"/System/Library/PrivateFrameworks/UniversalAccess.framework/UniversalAccess\")
lib.UAGrayscaleSetEnabled(lib.UAGrayscaleIsEnabled() == 0)
'"
do shell script toggleGrayScript
return


Any future release of macOS could break this solution, but until it does, it is a low code effort that works.


May 13, 2021 4:45 PM in response to caitlinshall

The follow kludge (no more time to give it) does toggle from Color Tint to Grayscale and back each time it is run on macOS 11.3.1. Ideally instead of key code sequences, one should be able to directly address "Grayscale" or "Color Tint" settings.


-- set_grayscale.applescript
-- alternates between grayscale and normal display with each run.
-- Tested on High Sierra (10.13.6), Mojave (10.14.6), and Catalina 10.15.1
-- Revision: 11/19/2019, Upgrade script for Catalina
-- Revision 2021-05-13 for Big Sur 11.3.1
-- VikingOSX, 12/30/2018, Apple Support Communities, No warranty at all

set os_ver to do shell script "sw_vers -productVersion | cut -d '.' -f1-2"

tell application "System Preferences"
	launch
	if os_ver < "10.15" then
		reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess"
	else
		reveal anchor "Seeing_ColorFilters" of pane "Accessibility"
	end if
end tell

tell application "System Events"
	tell application process "System Preferences"
		set frontmost to true
		delay 1
		if os_ver ≥ "10.15" then
			tell window "Accessibility"
				repeat until exists checkbox "Enable Color Filters" of tab group 1 of group 1
					delay 0.01
				end repeat
				tell pop up button 1 of tab group 1 of group 1
					click
					if not its value is "Grayscale" then
						# send four up-arrow key codes fromn Color Tint to select "Grayscale"
						key code {126, 126, 126, 126}
						# and a return
						key code 36
					else
						# send four down-arrow sequences from GrayScale to get Color Tint
						key code {125, 125, 125, 125}
						key code 36
						click
					end if
				end tell
			end tell
		else if os_ver = "10.14" then
			tell group 1 of window "Accessibility"
				click checkbox "Use grayscale"
			end tell
		else if os_ver < "10.14" then
			tell window "Accessibility"
				click checkbox "Use grayscale"
			end tell
		end if
	end tell
end tell
tell application "System Preferences" to if it is running then quit


Trying to write script to toggle grayscale (Big Sur)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.