Use Grayscale (Keyboard Shortcut)- Updated

This post seems to be outdated and the code no longer works for MacOS Monterey or higher. The functionality is exactly what I'm looking for. Would anyone be able to assist with providing an updated Apple Script for this post?


Use Grayscale (Keyboard Shortcut) - Apple Community


MacBook Pro

Posted on Feb 3, 2023 12:41 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 3, 2023 4:03 AM

That GUI scripting solution link was done five years ago and that is the least favorable AppleScript solution owing to Apple's continuous changes to the operating system that break that type of code. I will not waste any more time on tinkering with GUI AppleScript grayscale tinkering, especially with Ventura where the Settings are completely scrambled.


If you have Python3 installed, either from Apple's CommandLine Tools for Xcode, or from Python.org, then the following script will toggle grayscale on Monterey 12.6.3 (tested) or Ventura 13.2 (tested). Tested with Python 3.9.6 and 3.11.1 on both operating systems.


Since this script is loading a private Apple framework and methods — it may also break eventually.


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.

if exists "/usr/local/bin/python3" then
	-- have installation from Python.org
	set PYTHON3 to "/usr/local/bin/python3"
else if exists "/usr/bin/python3" then
	-- have installation from Apple's CommandLine Tools
	set PYTHON3 to "/usr/bin/python3"
else
	display dialog "Missing Python3… script ending." giving up after 10
	return
end if

set toggleGrayScript to PYTHON3 & " -c 'from ctypes import cdll
lib = cdll.LoadLibrary(\"/System/Library/PrivateFrameworks/UniversalAccess.framework/UniversalAccess\")
lib.UAGrayscaleSetEnabled(lib.UAGrayscaleIsEnabled() == 0)
'"
do shell script toggleGrayScript
return




Similar questions

1 reply
Question marked as Top-ranking reply

Feb 3, 2023 4:03 AM in response to iJohno101

That GUI scripting solution link was done five years ago and that is the least favorable AppleScript solution owing to Apple's continuous changes to the operating system that break that type of code. I will not waste any more time on tinkering with GUI AppleScript grayscale tinkering, especially with Ventura where the Settings are completely scrambled.


If you have Python3 installed, either from Apple's CommandLine Tools for Xcode, or from Python.org, then the following script will toggle grayscale on Monterey 12.6.3 (tested) or Ventura 13.2 (tested). Tested with Python 3.9.6 and 3.11.1 on both operating systems.


Since this script is loading a private Apple framework and methods — it may also break eventually.


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.

if exists "/usr/local/bin/python3" then
	-- have installation from Python.org
	set PYTHON3 to "/usr/local/bin/python3"
else if exists "/usr/bin/python3" then
	-- have installation from Apple's CommandLine Tools
	set PYTHON3 to "/usr/bin/python3"
else
	display dialog "Missing Python3… script ending." giving up after 10
	return
end if

set toggleGrayScript to PYTHON3 & " -c 'from ctypes import cdll
lib = cdll.LoadLibrary(\"/System/Library/PrivateFrameworks/UniversalAccess.framework/UniversalAccess\")
lib.UAGrayscaleSetEnabled(lib.UAGrayscaleIsEnabled() == 0)
'"
do shell script toggleGrayScript
return




This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Use Grayscale (Keyboard Shortcut)- Updated

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