1

How can I write a script to turn on the accessibility keyboard present in macOS High Sierra?

It's of course possible to do this by going to System Preferences > Accessibility > Keyboard > Accessibility Keyboard > "Enable Accessibility Keyboard," but this takes a number of clicks. I would like a script that can be launched from the Dock, since I frequently use the Accessibility Keyboard.

One idea is to use AppleScript; another idea is to find the Accessibility Keyboard binary and use a shell script to open it.

Here is my attempt at an AppleScript:

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

tell application "System Events" to tell application process "System Preferences"
    tell window "Accessibility"
        click UI element "Keyboard" of row 16 of table 1 of scroll area 2
        click button "Accessibility Keyboard"
        click checkbox "Enable Accessibility Keyboard"
    end tell
end tell

This script fails with the error: "System Events got an error: Can’t get button 'Accessibility Keyboard' of window 'Accessibility' of application process 'System Preferences'."

If I remove the last two click commands, the script does not produce an error, but also does not appear to click the "Keyboard" UI element.

What am I doing wrong? Apologies if this is a stupid question; this is my first AppleScript.

Or if someone knows the location of the Accessibility Keyboard binary, that would be helpful, too.

1

Update Note: This answer was originally written prior to the release of macOS Mojave and as such to use it in macOS Mojave you'll need to change three lines of code.

Change:

  • select table 1 of scroll area 2
  • select row 16 of table 1 of scroll area 2
  • tell tab group 1

To:

  • select table 1 of scroll area 1
  • select row 16 of table 1 of scroll area 1
  • tell tab group 1 of group 1

The following example AppleScript code works for me under macOS High Sierra:

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.universalaccess"
    delay 1
    tell application "System Events"
        tell window 1 of application process "System Preferences"
            select table 1 of scroll area 2
            delay 0.1
            select row 16 of table 1 of scroll area 2
            delay 0.1
            tell tab group 1
                click radio button "Accessibility Keyboard"
                delay 0.1
                click checkbox "Enable Accessibility Keyboard"
            end tell
        end tell
    end tell
    quit
end tell

In Script Editor, I saved example AppleScript code as an application, named Accessibility Keyboard.app, in the Applications folder.

Next, I added the Accessibility Keyboard.app to: System Preferences > Security & Privacy > Privacy > Accessibility

Now I can bring up the Accessibility Keyboard easily via Spotlight, or the Accessibility Keyboard.app can be dragged and dropped on the Dock so as to be launched for there.


Update:

My original example AppleScript code was geared at opening the Accessibility Keyboard, not closing it. As such, if it's open and the original script is run again it errors out. The following version of the example AppleScript code accounts for whether or not the Accessibility Keyboard it already shown and if so, closes it.

Note thought that while this does close the Accessibility Keyboard if it's showing, there is an added bounce to Dock Tile of System Preferences and is not and issue other then a secondary visual distraction.

Example AppleScript code:

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.universalaccess"
    delay 1
    tell application "System Events"
        tell window 1 of application process "System Preferences"
            select table 1 of scroll area 2
            delay 0.1
            select row 16 of table 1 of scroll area 2
            delay 0.1
            tell tab group 1
                click radio button "Accessibility Keyboard"
                delay 0.1
                if value of checkbox "Enable Accessibility Keyboard" is 0 then
                    click checkbox "Enable Accessibility Keyboard"
                else
                    click checkbox "Enable Accessibility Keyboard"
                    delay 1
                    my clickOK()
                end if
            end tell
        end tell
    end tell
    quit
end tell

on clickOK()
    tell application "System Events" to click button "OK" of window 1 of application process "System Preferences"
end clickOK

Note that System Preferences does not need to be visible for this to work and why there is no activate command in the example AppleScript code. Additionally if System Preferences is already open, it is first closed before the rest of the code is processed. This is done for a couple of reasons, the first of which was already stated and secondly seeing the UI Events processed is a visual distraction and can be annoying.

Also note that the value of the delay commands may need to be adjusted for your system, and or additional delay commands may or may not be needed. Adjust values of and or add/remove the delay commands as appropriate.

Keep in mind that once the app is added to System Preferences > Security & Privacy > Privacy > Accessibility and modified thereafter, you'll need to uncheck it and recheck it for the new changes to be allowed to be processed.


Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

  • Thanks so much! This works great! So that I understand better going forward, was my mistake in using click instead of select and in not using delays? How do click and select differ? – Henry DeYoung Feb 27 '18 at 17:01
  • @Henry DeYoung, to answer your question in the comment... Generally speaking but not alway, with UI Scripting in order for something to be clicked, it first must have focus. When System Preferences is first opened to Accessibility usually the Search text box has focus. So trying to click on something in the table in the scroll area that's not in focus fails and why you got the error you did. So by first selecting the table in the scroll area then Keyboard (select row 16 of table 1 of scroll area 2) can be selected. Continued in next comment... – user3439894 Feb 27 '18 at 17:42
  • @Henry DeYoung, Now the Accessibility Keyboard button can be clicked and the Enable Accessibility Keyboard can be checked. So, select sets focus to an object while click actions the object. The bottom line is, the differences as applicable and or necessary in any particular use case scenario become more apparent with experience using AppleScript UI Scripting. – user3439894 Feb 27 '18 at 17:42
  • @Henry DeYoung, As to the use of the delay command, this is necessary in some use cases and not others, obliviously if something is erring out and it's a timing issue then a delay is necessary. Often with UI Scripting delays are necessary, but not always. – user3439894 Feb 27 '18 at 17:57
  • Thanks so much again for your help! Your description of focus and select vs. click makes sense. So that I don't always need to rely on the generosity of people like you, is there a good tutorial/documentation for AppleScript UI scripting that you would recommend? – Henry DeYoung Feb 27 '18 at 19:16
1

Based on your excellent prior work, here's an

Updated 2018 version

Features

  • Works without any arbitrary delays
  • Works as a toggle (shows/hides depending on the keyboard state)
  • Preserves the state of the System Preferences app
  • Uses Apples Enhanced Application Object Model

Sourcecode

(*
 * toggle-macos-accessibility-keyboard
 * applescript
 *
 * description:
 * macOS automation script to toggle the macOS Accessibility Keyboard.
 * Shows and hides the keyboard depending on its current state.
 *
 * author: sidneys
 * homepage: http://sidneys.github.io
 * version: 2.0.1
 * license: MIT
 *)

-- Init
use AppleScript version "2.7"
use scripting additions

-- Persist start-up state of the "System Preferences" app
set didRunSystemPreferences to (get running of application "System Preferences")

-- Initialize storage for the checkbox value
set initialCheckboxValue to -1
set currentCheckboxValue to -1

-- Show "Keyboard" pane within the Accessibility preferences
tell application "System Preferences"
    reveal anchor 2 of pane id "com.apple.preference.universalaccess"
end tell

-- Start automated interaction
tell application "System Events"

    -- Wait for: System Preferences Window
    repeat until tab group 1 of window 1 of process "System Preferences" exists
    end repeat

    -- Wait for: Settings Pane
    repeat until (name of second radio button of tab group 1 of window 1 of process "System Preferences") is "Accessibility Keyboard"
    end repeat

    -- Select the "Accessibility Keyboard" Segmented Control
    click radio button 2 of tab group 1 of window 1 of process "System Preferences"

    -- Tick the "Enable Accessibility Keyboard" checkbox, remembering its before/after value
    set initialCheckboxValue to value of checkbox 1 of tab group 1 of window 1 of process "System Preferences"
    click checkbox 1 of tab group 1 of window 1 of process "System Preferences"
    set currentCheckboxValue to value of checkbox 1 of tab group 1 of window 1 of process "System Preferences"

    -- Did the checkbox value change?
    if initialCheckboxValue is currentCheckboxValue then
        -- No - Wait for: confirmation dialog
        repeat until sheet 1 of window 1 of process "System Preferences" exists
        end repeat
        -- Dismiss dialog
        click button 1 of sheet 1 of window 1 of process "System Preferences"
    end if

end tell

-- Did the "System Preferences" app run on start-up?
if not didRunSystemPreferences then
    -- No - Quit
    quit application "System Preferences"
else
    -- Yes - Return to the overview screen
    tell application "System Events"
        click menu item 3 of menu 1 of menu bar item 4 of menu bar 1 of process "System Preferences"
    end tell
end if

Repository

Fork the Gist at GitHub: toggle-accessibility-keyboard-macos

Looking forward to your feedback, Cheers, S

  • This is awesome, thanks! Do you happen to know if there is any way for your script to recall the keyboard from hiding in the hot corners? I do like the hot corner feature, but I often return to my desk and activate the script only to realize that the keyboard had been previously hidden away due to a hot corner event. It would be nice if your script would show the hidden keyboard rather than closing it. In any case, thanks so much for your help! – Henry DeYoung May 30 '18 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .