Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
applescript for toggling grayscale on in OSX Catalina, Sierra, and Mojave.
# gotcha: depending on whether you use american or british english, you may need to change "color" -> "colour".
# thanks goes to @kevin-deyoungster
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.universalaccess"
delay 1 #needs time to open universal access
tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
select row 5 of table 1 of scroll area 1 #open display preferences
click radio button "Color Filters" of tab group 1 of group 1
click checkbox "Enable Color Filters" of tab group 1 of group 1
end tell
end tell
tell application "System Preferences" to quit
# for osx mojave
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.universalaccess"
delay 1 # needs time to open universal access
tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
tell scroll area 1 to tell table 1 to tell row 6 #open display preferences
select
end tell
tell group 1
click checkbox "Use grayscale"
end tell
end tell
end tell
tell application "System Preferences" to quit
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.universalaccess"
delay 1 # needs time to open universal access
tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
tell scroll area 2 to tell table 1 to tell row 6 #open display preferences
select
end tell
click checkbox "Use grayscale"
end tell
end tell
tell application "System Preferences" to quit
@agsadasfs

This comment has been minimized.

Copy link

@agsadasfs agsadasfs commented Sep 19, 2018

I was getting an error for High Sierra 10.13.1 - I fixed it by adding an extra line. Take a look.

tell application "System Preferences"
	activate
	reveal (pane id "com.apple.preference.universalaccess")
end tell

tell application "System Events"
	tell process "System Preferences"
		tell window "Accessibility"
			tell table 1 of scroll area 1
				-- Added "delay 1" to work in 10.11 - El Capitan
				delay 1
				select (row 2)
			end tell
			tell scroll area 2 to tell table 1 to tell row 6 #open display preferences
				select
			end tell
			click checkbox "Use grayscale"
		end tell
	end tell
end tell
tell application "System Preferences" to quit
@wbw1006

This comment has been minimized.

Copy link

@wbw1006 wbw1006 commented Nov 10, 2018

can get window "Accessibility" of process "System Preferences

@1404423

This comment has been minimized.

Copy link

@1404423 1404423 commented May 22, 2019

Now I can't use this code.
If possible I'd like to know the script which turns off and on 3 finger drag.

@silesky

This comment has been minimized.

Copy link
Owner Author

@silesky silesky commented Jun 5, 2019

Updated this for OSX Mojave.
@1404423, that should be possible by tweaking this script.

@KDALEX

This comment has been minimized.

Copy link

@KDALEX KDALEX commented Sep 14, 2019

For Mac OS 10.14.4 (18E227) I had to change line 11 of the first script to "click checkbox 2".

@Giulianini

This comment has been minimized.

Copy link

@Giulianini Giulianini commented Apr 28, 2020

Can you make it for Catalina? it would be very useful

@kevin-deyoungster

This comment has been minimized.

Copy link

@kevin-deyoungster kevin-deyoungster commented May 1, 2020

Thanks @silesky for pointing in the right direction. Here's what I came up with for Catalina (@Giulianini)

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.universalaccess"
	delay 1 #needs time to open universal access
	tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
		select row 5 of table 1 of scroll area 1 #open display preferences
		click radio button "Colour Filters" of tab group 1 of group 1
		click checkbox "Enable Colour Filters" of tab group 1 of group 1
	end tell
end tell
tell application "System Preferences" to quit
@Giulianini

This comment has been minimized.

Copy link

@Giulianini Giulianini commented May 1, 2020

Schermata 2020-05-01 alle 21 51 07

Thanks @kevin-deyoungster but i ran into this error "Impossible to obtain window "Accessibility" of process "System Preferences"". It happened to me a lot of times and I can't face the problem. In my opinion there is a problem in Catalina

@kevin-deyoungster

This comment has been minimized.

Copy link

@kevin-deyoungster kevin-deyoungster commented May 1, 2020

Oh, I see the issue, your language setting is different.

You'll need to change the strings Accessibility, Colour Filters and Enable Colour Filters to the right translations, the ones your system uses.

I assume you're using Italian so change:
Accessibility -> Accessibilità
Colour Filters -> Filtri colore
Enable Colour Filters -> Abilita filtri colore

Hope it helps, let know how it goes!

Schermata 2020-05-01 alle 21 51 07

Thanks @kevin-deyoungster but i ran into this error "Impossible to obtain window "Accessibility" of process "System Preferences"". It happened to me a lot of times and I can't face the problem. In my opinion there is a problem in Catalina

@Giulianini

This comment has been minimized.

Copy link

@Giulianini Giulianini commented May 1, 2020

@kevin-deyoungster you save my day. I have done a very similar setting yesterday but i kept Enable (C)olor (F)ilters capitalized but in Italian it's lowercase. I must definitely change language settings to English. Thanks again for the help!

@kevin-deyoungster

This comment has been minimized.

Copy link

@kevin-deyoungster kevin-deyoungster commented May 1, 2020

Glad that helped. You're welcome!

@LuisMcLovin

This comment has been minimized.

Copy link

@LuisMcLovin LuisMcLovin commented May 28, 2020

omg thank you. ily. im no coder (learning python) and your catalina scripted helped me a lot. thank you. Im assigning a hotkey to it now!

@LuisMcLovin

This comment has been minimized.

Copy link

@LuisMcLovin LuisMcLovin commented May 28, 2020

ah. gosh. it worked in automator. now when running it with hotkey i get denied permissions and am unable to find the means to grant it

@LuisMcLovin

This comment has been minimized.

Copy link

@LuisMcLovin LuisMcLovin commented May 28, 2020

got it! THANK YOU

@silesky

This comment has been minimized.

Copy link
Owner Author

@silesky silesky commented May 29, 2020

@kevin-deyoungster, thanks for updating the script. I added it to the gist.

@brettferdosi

This comment has been minimized.

Copy link

@brettferdosi brettferdosi commented Jun 15, 2020

Thanks for maintaining this resource, I found it helpful while looking for a way to bind grayscale toggling to a keyboard shortcut. I ended up building a menu bar app that toggles grayscale directly. Here's a link in case anyone is interested: https://github.com/brettferdosi/grayscale.

@ChristoferK

This comment has been minimized.

Copy link

@ChristoferK ChristoferK commented Jun 17, 2020

You shouldn't put a tell application "System Events" block inside a tell application "System Preferences" block. At a minimum, it can throw silent errors in the background that slow down a script. At the worst, it can create terminology confusion that ends up sending a command intended for one application to another that happens to use the same terminology to perform a very different operation, and in one case involving Finder and System Events, to pretty disastrous results.

@AdamSHurwitz

This comment has been minimized.

Copy link

@AdamSHurwitz AdamSHurwitz commented Jul 6, 2020

Expect

Run the AppleScript shared by @kevin_deyoungster above from a shortcut when in the Finder application.

Observe

Error

The action “Run AppleScript” encountered an error: “System Events got an error: com.automator.runner.xpc is not allowed assistive access.”

Script

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.universalaccess"
	delay 1 #needs time to open universal access
	tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
		select row 5 of table 1 of scroll area 1 #open display preferences
		click radio button "Color Filters" of tab group 1 of group 1
		click checkbox "Enable Color Filters" of tab group 1 of group 1
	end tell
end tell
tell application "System Preferences" to quit

Solve

I solved this error for launching the Grayscale shortcut from Finder by giving Finder access to control the computer. The shortcut does not work when launched with Google Chrome open. However, I do not know if it is a good idea to give Chrome this level of deep access. Therefore, I will launch this shortcut from Finder which is fairly convenient considering it can quickly be shifted into focus via cmd + tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.