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.