4

I have a script I run periodically to toggle grayscale on/off with Applescript. It runs fine on High Sierra but throw an exception when I use it was Mojave.

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

tell application "System Events" to tell process "System Preferences"
    delay 0.5 # Needed or else script fails
    set theCheckbox to checkbox "Use grayscale" of window "Accessibility"
    tell theCheckbox
        # If the checkbox is not checked, check it to turn grayscale on
        if not (its value as boolean) then
            set checked to true
            click theCheckbox
        else # else turn grayscale off
            set checked to false
            click theCheckbox
        end if
    end tell
end tell

tell application "System Preferences"
    quit
end tell

The exception is:

System Events got an error: Can’t get checkbox "Use grayscale" of window "Accessibility" of process "System Preferences". (-1728)

Does Mojave still support Applescript/Would anyone know how to fix this?

ClickThisNick
  • 5,110
  • 9
  • 44
  • 69
  • The checkbox is now in `group 1 of window "Accessibility"` but it seems that the access is broken. Feel free to file a bug. – vadian Oct 01 '18 at 04:58
  • it's not a bug i think it's Apple's System integrity protection. Either deactivate it (there are lots of tutorials out there) or wrap your script in an Xcode application using appropriate plist modifications – Pat_Morita Oct 01 '18 at 20:59
  • I can manually edit the checkbox without having to disable the System integrity protection, just cannot with Applescript anymore. I can try the Xcode application – ClickThisNick Oct 02 '18 at 01:02
  • If you want something that is more "click and go", you can use the app I built. Makes Grayscale mode toggleable from the status bar. https://shubhamjain.co/quick-grayscale/ – Shubham Nov 25 '18 at 14:11

2 Answers2

8

As of Catalina, updated to:

# quit system preferences if running
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

# wait for system preferences to quit
with timeout of 10 seconds
    repeat while running of application "System Preferences" is true
        delay 0.01
    end repeat
end timeout

# switch to correct anchor
tell application "System Preferences" to reveal anchor "Seeing_ColorFilters" of pane "Accessibility"
#tell application "System Preferences" to activate
#get the name of every anchor of pane "Accessibility" of application "System Preferences"

tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
    #return UI elements of tab group 1 of group 1
    with timeout of 10 seconds
        repeat until exists checkbox "Enable Color Filters" of tab group 1 of group 1
            delay 0.01
        end repeat
    end timeout
    click the checkbox "Enable Color Filters" of tab group 1 of group 1
end tell

tell application "System Preferences" to quit
Tyler
  • 170
  • 1
  • 3
  • 2
    Dude you are a hero. Thank you for this! – thenomadicmann Jul 28 '20 at 06:03
  • 1
    Fantastic! All I had to change was to update Color to 'Colour' for my British English OS. – ppt Dec 17 '22 at 09:36
  • This breaks in Ventura. I opened a new question for this at https://stackoverflow.com/questions/75152094/applescript-ventura-toggle-accessibility-grayscale-on-off – ppt Jan 17 '23 at 20:53
2

This works for me Using OS Mojave

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

tell application "System Events" to tell process "System Preferences"
    repeat while not (exists of checkbox "Use grayscale" of group 1 of window "Accessibility")
        delay 0.1
    end repeat
    set theCheckbox to checkbox "Use grayscale" of group 1 of window "Accessibility"
    tell theCheckbox
        # If the checkbox is not checked, check it to turn grayscale on
        if not (its value as boolean) then
            set checked to true
            click theCheckbox
        else # else turn grayscale off
            set checked to false
            click theCheckbox
        end if
    end tell
end tell

tell application "System Preferences"
    quit
end tell
wch1zpink
  • 3,026
  • 1
  • 8
  • 19