The example AppleScript code, shown further below, will toggle the state of the Accessibility Keyboard and was tested under macOS Catalina and macOS Big Sur.
It worked for me as coded, however, it uses a third-party utility to avoid closing the Accessibility Keyboard via unchecking the Enable Accessibility Keyboard checkbox in System Preferences in order to not have to see the UI Scripting of System Preferences opening and the OK button being clicked, followed by System Preferences closing, as occurs with the code in the other answer to your question.
Having to use UI Scripting is bad enough, but having the visual distraction of having to see System Preferences open and the OK button being clicked, followed by System Preferences closing is just not necessary with my method.
The third-party utility is cliclick and I placed it in /usr/local/bin/
and it's used to click the Close button on the UI of the Accessibility Keyboard. (I did try closing it using additional other methods but this was the least problematic, and why I needed up with it.)
To use the example AppleScript code I saved it as Toggle Accessibility Keyboard.scpt in ~/Library/Scripts/ and trigger it with a keyboard shortcut of ⌃⌥⌘K using FastScripts, however, an Automator Service/Quick Action can be created using a Run AppleScript action and the code, along with a keyboard shortcut assigned in: System Preferences > Keyboard > Shortcuts > Services
What the example AppleScript code does:
Checks to see if the Accessibility Keyboard is showing by reading com.apple.HIToolbox plist file to see if com.apple.inputmethod.AssistiveControl exists, as it does when it's showing, and not when it's not showing.
If it's showing, it calls the closeVirtualKeyboard
handler which programmatically gets the position and size of the Close button of the Accessibility Keyboard in order to click it, using cliclick
, where ever it is on the display.
If it's not showing, it calls the openVirtualKeyboard
handler to show it.
Example AppleScript code:
set shellCMD to {¬
"defaults read com.apple.HIToolbox", ¬
" | ", ¬
"grep -o 'com.apple.inputmethod.AssistiveControl'", ¬
"; exit 0"} as string
set foo to (do shell script shellCMD)
if not foo is equal to "" then
closeVirtualKeyboard()
else
my openVirtualKeyboard()
end if
on closeVirtualKeyboard()
tell application "System Events"
set ps to {position, size} of ¬
button "Close" of window "Panel" of ¬
application process "Assistive Control"
set x to (item 1 of item 1 of ps) + ¬
(item 1 of item 2 of ps) / 2 as integer
set y to (item 2 of item 1 of ps) + ¬
(item 2 of item 2 of ps) / 2 as integer
set shellCMD to ¬
{"/usr/local/bin/cliclick -r c:", x, ",", y} ¬
as string
tell me to do shell script shellCMD
end tell
end closeVirtualKeyboard
on openVirtualKeyboard()
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
delay 0.1
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences" to ¬
reveal anchor "Virtual_Keyboard" in ¬
pane id "com.apple.preference.universalaccess"
tell application "System Events"
repeat until (exists checkbox "Enable Accessibility Keyboard" of ¬
tab group 1 of group 1 of window "Accessibility" of ¬
application process "System Preferences")
delay 0.01
end repeat
click checkbox "Enable Accessibility Keyboard" of ¬
tab group 1 of group 1 of window "Accessibility" of ¬
application process "System Preferences"
end tell
tell application "System Preferences" to quit
end openVirtualKeyboard