AppleScript to turn on the Accessibility Keyboard in High Sierra 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.
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 &quot;System Preferences&quot; then
    quit application &quot;System Preferences&quot;
    delay 1
end if
tell application &quot;System Preferences&quot;
    reveal pane id &quot;com.apple.preference.universalaccess&quot;
    delay 1
    tell application &quot;System Events&quot;
        tell window 1 of application process &quot;System Preferences&quot;
            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 &quot;Accessibility Keyboard&quot;
                delay 0.1
                click checkbox &quot;Enable Accessibility Keyboard&quot;
            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 &gt; Security &amp; Privacy &gt; Privacy &gt; 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 &quot;System Preferences&quot; then
    quit application &quot;System Preferences&quot;
    delay 1
end if
tell application &quot;System Preferences&quot;
    reveal pane id &quot;com.apple.preference.universalaccess&quot;
    delay 1
    tell application &quot;System Events&quot;
        tell window 1 of application process &quot;System Preferences&quot;
            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 &quot;Accessibility Keyboard&quot;
                delay 0.1
                if value of checkbox &quot;Enable Accessibility Keyboard&quot; is 0 then
                    click checkbox &quot;Enable Accessibility Keyboard&quot;
                else
                    click checkbox &quot;Enable Accessibility Keyboard&quot;
                    delay 1
                    my clickOK()
                end if
            end tell
        end tell
    end tell
    quit
end tell

on clickOK()
    tell application &quot;System Events&quot; to click button &quot;OK&quot; of window 1 of application process &quot;System Preferences&quot;
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 &gt; Security &amp; Privacy &gt; Privacy &gt; 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.