AppleScript: Toggle Grayscale On/Off with a Keyboard Shortcut

So OS X 10.11 – El Capitan broke an AppleScript that I had been using to toggle grayscale mode on/off, resulting in a script error:

AppleScript Error

All it took was adding a delay to fix the script (notated as a comment in the script below).

If you are running into errors with a previously used “Toggle Grayscale” AppleScript (or perhaps any AppleScript) in El Capitan, you may want to try adding “delay 1” in the line before the error occurs.

Okay, enough digression.

Fix or Create Script
    1. Open Script Editor.app, located in Applications>Utilities.
    2. Copy and paste the following:
      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
      			click checkbox "Use grayscale"
      		end tell
      	end tell
      end tell
      tell application "System Preferences" to quit
      
    3. Click Compile, then Run to verify it works. Preview:
    4. Save the script file (.scpt).

Assign Script to a Keyboard Shortcut
    1. To map the script to a keyboard shortcut, such as a F key or a multiple key combination, download, install, and run BetterTouchTool (BTT). There’s a free trial version but it’s worth the few bucks since you can do so many other things with it.

      Note: You can also use OS X’s bulit-in method described here or any other keyboard mapping tools. BTT is the simplest though, in my opinion.
    2. Open BTT’s preferences by clicking the BTT Menu Bar Icon icon, located in the menu bar. Select Global on the left side (to ensure the shortcut will work from any app), then click + Add New Shortcut.Add Shortcut in BTT
    3. Under Keyboard Shortcut, type the key or shortcut combo you wish to use (F15 in this example) and then click on the Trigger Predefined Action drop-down box.Select Shortcut in BTT
    4. In the search box that pops-up, type:
      apple script

      and then select Open Application / File / Apple Script …BTT Search Box with "Apple Script"

    5. Navigate to the saved .scpt file from earlier and select it. Click Open.Select AppleScript in BTT
    6. You can close the BTT window, as it will stay running in the background and accessible via the menu bar.

There you go!

10 Comments

  1. Any update to this for Sierra? It’s running fine for me in compile in Automator or Apple Script Editor (after a minor tweak to select “Scroll area 2” and “row 6”), but as soon as I save it as a service or app I get an error. It doesn’t seem to select the Display option, after it opens the Accessibility pane. Love to hear if you have it working!

    1. Same, sierra broke my AppleScript.
      I’m having trouble customizing it to access the Display anchor menu.
      `reveal anchor “Display” of pane id “com.apple.preference.universalaccess”`
      spits out an error.

    2. Hey, I got it working:

      tell application “System Preferences”
      activate
      set the current pane to pane id “com.apple.preference.universalaccess”

      tell application “System Events” to tell process “System Preferences”
      tell window “Accessibility”
      tell scroll area 1
      tell table 1
      select row 6

      end tell

      end tell
      click checkbox “Use grayscale”
      end tell

      end tell
      end tell
      #tell application “System Preferences” to quit

  2. ^ I made a mistake in the last script (feel free to delete it)

    Here’s is my new one, working for OSX Sierra.
    —————
    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

  3. Hi there. Running Mac OS High Sierra, and curious about a script to do this for the newest operating system. Tried the solution offered above (Seth’s corrected post), and got a “Syntax Error” dialog.

    1. @Joshua: Sorry, there was an error in the post. Changing all “ to ” in the script should make it run correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *