MacOS – How to enable/disable grayscale mode in Accessibility via Terminal/App

accessibilityapplescriptmacosterminal

I'm wondering what would be the process of enabling/disabling the grayscale mode in the Accessibility panel in System Preferences?

I would like to run a simple script, either by clicking on an app in the Dock or by a Terminal command, and have it switch from grayscale to regular mode.

Any ideas?

Best Answer

Note: The example AppleScript code was updated to handle the current latest macOS (Mojave) and add additional code improvements. On earlier versions of OS X/macOS you may have to remove of group 1 from the two lines of example AppleScript code that have it, for the code to work.


If you want to create an AppleScript application to put in the Dock you can use the following code in OS X Yosemite (and latter, I believe).

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
end if

repeat while running of application "System Preferences" is true
    delay 0.01
end repeat

tell application "System Preferences" to reveal anchor "Seeing_Display" of ¬
    pane id "com.apple.preference.universalaccess"

tell application "System Events" to tell process "System Preferences"
    repeat until exists checkbox "Use grayscale" of group 1 of ¬
        window "Accessibility"
        delay 0.01
    end repeat
    click the checkbox "Use grayscale" of group 1 of window "Accessibility"
end tell

tell application "System Preferences" to quit

In Script Editor, save it as Toggle Grayscale changing the File Format: to: Application

You can give the App a different icon via copy and paste into the icon of the App's Get Info sheet and then drag and drop the App bundle onto the Dock.

You'll have to give permission under Accessibility on the Privacy tab of Security & Privacy in System Preferences in order to run it successfully.


If you'd like to use a bash script using the code provided by IconDaemon, the following code will toggle between using color and grayscale based on how it's currently set.

#!/bin/bash

setGrayscale () {

    defaults write com.apple.universalaccess grayscale -bool $1
    defaults write com.apple.CoreGraphics DisplayUseForcedGray -bool $1
    launchctl unload /System/Library/LaunchAgents/com.apple.universalaccessd.plist
    launchctl load /System/Library/LaunchAgents/com.apple.universalaccessd.plist

    case "$1" in
        "NO")
            echo "  Changing Display to use color. This will take a moment..."
        ;;
        "YES")
            echo "  Changing Display to use grayscale. This will take a moment..."
        ;;
    esac

}

_bool="$(defaults read com.apple.universalaccess grayscale 2>/dev/null)"

case "$_bool" in
    "0")
        setGrayscale "YES"
    ;;
    "1")
        setGrayscale "NO"
    ;;
    *)
        setGrayscale "YES"
    ;;
esac

enter image description here