MacOS – Sierra Automator KeyboardViewer App Shortcut

applescriptautomatorkeyboardmacosshortcut

KeyboardViewer app is saved from this AppleScript.

on run {input, parameters}
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    else 
        activate application "KeyboardViewer"
    end if
    return input
end run

Dock toggle save. Adding shortcut to above, needs some work…

Syntax error Can’t set application "KeyboardViewer" to some object. Access not allowed.

tell application "System Events"
    set application "KeyboardViewer" to (keystroke "K" using control down)
end tell

Best Answer

Thanks all! process whose frontmost is true and visible is true froze Keyboard Viewer behind a Finder window that also froze - 2 freezes in 2 days. Following seems to work quite well, in macOS 10.13.2. Simply add the delay 1.

    on run {input, parameters}
        if application "KeyboardViewer" is running then
            quit application "KeyboardViewer"
            delay 1
        else if application "KeyboardViewer" is not running then
            activate application "KeyboardViewer"
        end if
        return input
    end run