2

Been trying to get an AppleScript to work that will switch the display mode to greyscale.

Based this off another script that was a few years old. Apple changed the configuration of the System Preference panel since then which broke the script.

Can't figure out how to get it to navigate into the "Display" menu item inside the Accessibility Panel.

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
                delay 1
                select (row 4)
            end tell
            click checkbox "Use grayscale"
        end tell
    end tell
end tell
tell application "System Preferences" to quit

Any help very much appreciated!

3 Answers3

4

I'm going to chime in here with a third contribution, since I admittedly dislike both scripts posted so far, although I do fully agree with @vadian's advice about checking for existence.

My first qualm is that there's no need to activate System Preferences. It works quite happily in the background and completely out-of-sight.

My second is that there's no need at all to muddle around with which particular row one must select and how to go about identifying it: System Preferences has, along with panes, a series of anchors, one of which takes your directly to the "Display" section.

The only piece of UI scripting, which is a regrettable necessity in this case, is in accessing the checkbox and clicking it.

You should find the result of the script below a bit more pleasing by how unapparent System Preferences's role appears to be at execution time, when it seemingly fails to make an appearance.

use prefs : application "System Preferences"
use sys : application "System Events"

property process : a reference to application process "System Preferences"
property window : a reference to window 1 of my process
property pane : a reference to pane id "com.apple.preference.universalaccess"
property anchor : a reference to anchor "Seeing_Display" of my pane
property checkbox : a reference to checkbox "Use grayscale" of my window

contents of my anchor = (reveal my anchor)
if the result = false then return

with timeout of 60 seconds
    repeat until my checkbox exists
        delay 0.5
    end repeat
end timeout

click my checkbox

quit prefs

System info: AppleScript version: "2.7", system version: "10.13.6"

CJK
  • 5,732
  • 1
  • 8
  • 26
  • 1
    This is lovely and elegant – nice to have it run in the background without the preferences window showing up. Also taught me a lot of new AppleScript functionality. Thanks for adding it on! – Maggie Appleton Oct 05 '18 at 10:55
  • 1
    Interesting style! Changes necessary for Catalina: https://stackoverflow.com/a/58469753/463645 – Tyler Oct 20 '19 at 03:17
1

For macOS High Sierra, the answer was kindly posted in the comments of this article, so all credit the original author.

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

That is working for me in macOS 10.13.5

Raquel Moss
  • 91
  • 1
  • 5
1

If you are going to script the UI make sure that particular UI elements exist before using them. The most efficient way is a repeat loop.

And instead of selecting the row number the script selects the row named Display.

If the script will be used in a localized environment use the localized strings for Accessibility, Display and Use grayscale

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

tell application "System Events"
    tell process "System Preferences"
        repeat until exists window "Accessibility"
            delay 0.1
        end repeat
        tell window "Accessibility"
            try
                select (first row of table 1 of scroll area 2 whose name of UI element 1 is "Display")
                click checkbox "Use grayscale"
            end try
        end tell
    end tell
end tell
quit application "System Preferences"
vadian
  • 274,689
  • 30
  • 353
  • 361