1

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 "Can't get application KeyboardViewer"
If i press the shortcut i assigned, it opens a window("Choose an application") with a list of apps

The scripts I have tried:

if application "KeyboardViewer" is running then
    quit application "KeyboardViewer"
end if

activate application "KeyboardViewer"

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

and

on run
    tell application "System Events"
        set activeApp to name of first application process whose frontmost is true and visible is true
    end tell
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    else
        activate application "KeyboardViewer"
    end if
    tell application activeApp to activate
end run
4
  • Does this answer help?
    – nohillside
    Dec 26, 2020 at 10:26
  • nope, for that i need to press accessibility shortcut+choose keyboard with mouse. I want to create a shortcut that directly toggles keyboard viewer
    – Rasul
    Dec 26, 2020 at 11:07
  • i don't get why people downvote the question, what is wrong with it lol?
    – Rasul
    Dec 31, 2020 at 13:00
  • Downvotes often are a mystery, best not to loose to much sleep over it.
    – nohillside
    Dec 31, 2020 at 13:07

2 Answers 2

0

This works for me on macOS Big Sur.

if application "System Preferences" is running then do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
    delay 0.1
end repeat

tell application "System Preferences"
    reveal anchor "Virtual_Keyboard" of pane "com.apple.preference.universalaccess"
end tell

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

do shell script "killall 'System Preferences'"

SIDENOTE: My reason for using the the do shell script "killall 'System Preferences'" command rather than tell application "System Preferences" 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 "System Preferences" to reveal anchor "SpeakableItems" of pane id "com.apple.preference.universalaccess" That command completes with no obvious errors but when you switch over to System Preferences, your "revealed anchor" is not revealed. Ok, no big deal, easy fix. I'll just insert a tell application "System Preferences" 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 "killall 'System Preferences'" 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 "System Events"
    click menu bar item "Accessibility Shortcuts" of menu bar 1 ¬
        of application process "ControlCenter"
    repeat until checkbox "Accessibility Keyboard" of group 1 of ¬
        window "Control Center" of application process "ControlCenter" exists
        delay 0.1
    end repeat
    click checkbox "Accessibility Keyboard" of group 1 of ¬
        window "Control Center" of application process "ControlCenter"
    delay 0.1
    key code 53 -- escape key
end tell
6
  • 1
    do shell script "killall 'System Preferences'" Whhhyyyyyy⁈
    – CJK
    Dec 27, 2020 at 0:16
  • do shell script "killall 'System Preferences'" is really only needed if there is a modal sheet that needs responding to that would stop other System Preferences related commands to fail, otherwise tell application "System Preferences" to quit or quit application "System Preferences" is fine. That said, since when unchecking "Enable Accessibility Keyboard" checkbox forces the UI of System Preferences to show and bring up a modal sheet I took a different approach in my answer. Dec 27, 2020 at 0:33
  • The first do shell script "killall 'System Preferences'" is fine although I personally choose to close it gracefully and only use the do shell script command if a graceful quit fails. That said, why the ending one, as there shouldn't be a need to force it closed at the very end of the script. Dec 27, 2020 at 0:38
  • @user3439894 I chose the ending one in the event that clicking the “OK” action in my code fails for whatever reason it's window remains open… a simple quit command sent to Sys Prefs wont work.. Is just a failsafe and frankly it makes no difference to me whether it's gracefully done or not
    – wch1zpink
    Dec 27, 2020 at 0:43
  • If the sheet with the OK button isn't dismissed then the button wasn't clicked and the Accessibility Keyboard will still be showing because of System Preferences being killed by the do shell script command. Dec 27, 2020 at 0:50
0

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

You must log in to answer this question.

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