Shortcut to toggle KeyboardViewer with AppleScript on Big Sur I am trying to create a shortcut that toggles Keyboard Viewer with Automator. I have tried these both ways: this old one and and this one for Sierra 
For both ways, there is a syntax error &quot;Can't get application KeyboardViewer&quot; 
If i press the shortcut i assigned, it opens a window(&quot;Choose an application&quot;) with a list of apps 
The scripts I have tried:
if application &quot;KeyboardViewer&quot; is running then
    quit application &quot;KeyboardViewer&quot;
end if

activate application &quot;KeyboardViewer&quot;

-- wait until the window has been closed, then end the KeyboardViewer process
set numberOfWindows to 1
repeat until numberOfWindows = 0
    delay 5
    tell application &quot;System Events&quot;
        tell process &quot;KeyboardViewer&quot;
            set numberOfWindows to count windows
        end tell
    end tell
end repeat
quit application &quot;KeyboardViewer&quot;

and
on run
    tell application &quot;System Events&quot;
        set activeApp to name of first application process whose frontmost is true and visible is true
    end tell
    if application &quot;KeyboardViewer&quot; is running then
        quit application &quot;KeyboardViewer&quot;
    else
        activate application &quot;KeyboardViewer&quot;
    end if
    tell application activeApp to activate
end run
This works for me on macOS Big Sur.
if application &quot;System Preferences&quot; is running then do shell script &quot;killall 'System Preferences'&quot;
repeat until application &quot;System Preferences&quot; is not running
    delay 0.1
end repeat

tell application &quot;System Preferences&quot;
    reveal anchor &quot;Virtual_Keyboard&quot; of pane &quot;com.apple.preference.universalaccess&quot;
end tell

tell application &quot;System Events&quot; to tell application process &quot;System Preferences&quot;
    repeat until checkbox &quot;Enable Accessibility Keyboard&quot; of tab group 1 ¬
        of group 1 of window &quot;Accessibility&quot; exists
        delay 0.1
    end repeat
    set theValue to value of checkbox &quot;Enable Accessibility Keyboard&quot; of tab group ¬
        1 of group 1 of window &quot;Accessibility&quot;
    delay 0.1
    if theValue = 1 then
        set frontmost to true
        repeat until frontmost
            delay 0.1
        end repeat
        click checkbox &quot;Enable Accessibility Keyboard&quot; of tab group 1 of ¬
            group 1 of window &quot;Accessibility&quot;
        delay 0.1
        if UI element &quot;OK&quot; of sheet 1 of window &quot;Accessibility&quot; exists then
            click UI element &quot;OK&quot; of sheet 1 of window &quot;Accessibility&quot;
        end if
    else
        click checkbox &quot;Enable Accessibility Keyboard&quot; of tab group 1 of ¬
            group 1 of window &quot;Accessibility&quot;
    end if
end tell

do shell script &quot;killall 'System Preferences'&quot;

SIDENOTE: My reason for using the the do shell script &quot;killall 'System Preferences'&quot; command rather than tell application &quot;System Preferences&quot; to quit is... Let's say, for what ever reason, System Preferences.app is already activated (with, for example, window Sharing/ Remote Management/ Computer Settings open) but not visible or not front most or whatever. In Script Editor, you try to run tell application &quot;System Preferences&quot; to reveal anchor &quot;SpeakableItems&quot; of pane id &quot;com.apple.preference.universalaccess&quot; That command completes with no obvious errors but when you switch over to System Preferences, your &quot;revealed anchor&quot; is not revealed. Ok, no big deal, easy fix. I'll just insert a tell application &quot;System Preferences&quot; to quit command prior to the reveal anchorcommand. Running the updated code this time, you get a (userCanceledErr:-128) error. All of this hassle is because that secondary drop down window in System Preferences is open. In my opinion, the do shell script &quot;killall 'System Preferences'&quot; command seems to be the best solution.

Here is another option if you have the “Show Accessibility status in menu bar” checked in System Preferences/Accessibility. It's short, it's sweet, but it does use UI scripting.
tell application &quot;System Events&quot;
    click menu bar item &quot;Accessibility Shortcuts&quot; of menu bar 1 ¬
        of application process &quot;ControlCenter&quot;
    repeat until checkbox &quot;Accessibility Keyboard&quot; of group 1 of ¬
        window &quot;Control Center&quot; of application process &quot;ControlCenter&quot; exists
        delay 0.1
    end repeat
    click checkbox &quot;Accessibility Keyboard&quot; of group 1 of ¬
        window &quot;Control Center&quot; of application process &quot;ControlCenter&quot;
    delay 0.1
    key code 53 -- escape key
end tell