0

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
1

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
0

If you're wanting to assign a keyboard shortcut to an AppleScript application, you'll need to do it as an Automator Service. However, one of the issues you'll run into, when Keyboard Viewer has focus is, it will eat the keyboard shortcut and not close Keyboard Viewer. You'd have to set focus away from Keyboard Viewer in order for the Automator Service to close Keyboard Viewer.

The following AppleScript code does that, providing you have not set focus to it after it opens or reset it elsewhere if you did.

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

In Automator create a new Service workflow with the following setting:

  • Service receives no input in any application
  • Add a Run AppleScript action.
    • Replace the default code with the code above.
    • Click the Compile button (Hammer icon).
  • Save it as e.g.: Toggle Keyboard Viewer
  • In System Preferences > Keyboard > Services > Shortcuts > Services
    • Find e.g. Toggle Keyboard Viewer and give it a shortcut.
    • I gave it: ⌥⌘K

Note: Finding a keyboard shortcut that isn't already taken by another app can be a challenge. In this use case, any application that has focus when e.g. ⌥⌘K is pressed, it will go to the app's assigned command before it goes to the e.g.: Toggle Keyboard Viewer service. You might need to go to a four key keyboard shortcut, e.g.: ^⌥⌘K

Automator Service

System Preferences > Keyboard > Services > Shortcuts** > Services

  • I did try Services approach, but shortcut was ignored. Then tried same application-and-services approaches, using launchd keyboardservicesd, same result. Perhaps an if-else, but then if-else cannot handle keystroke assignment (syntax good but system cannot parse). Nulaana Shortcuts FAQ nulana.com/support - tell application "System Events" delay 0.5 keystroke "n" using {command down, control down} end tell – mark stewart Oct 25 '17 at 12:50
  • Nulana's Delay 0.5 works, keystroke seated in Dock's toggle icon application but... Now we have problem KeyboardViewer is open, and app just highlights shortcut keys, intercepting and cutting off any application-services shortcut settings. I think this effort needs to inject something more into launchd. Btw, instructions above very well done. – mark stewart Oct 25 '17 at 13:02
  • @mark stewart, What I posted works, I would not have posted it otherwise! Saying "I did try Services approach, but shortcut was ignored." is on no value without knowing what keyboard shortcut you assigned. Did you read my Note: in my answer? If the keyboard shortcut you assigned was done by the code you show in the OP, i.e. tell application "System Events" set application "KeyboardViewer" to (keystroke "K" using control down) You cannot assign a keyboard shortcut in that manned, it's improper coding regardless of the fact that it compiles. Read the first part of my answer up to the code. – user3439894 Oct 25 '17 at 13:14
  • Sorry, copy and paste what works for you, not working here, just an error beep. Put this to Nulana support, as sending new mail with other apps open blocks Nulana too. Nulana opens my old Lion Gem, but sometimes close just displays keypress. Launchd (500), for Sierra 10.12.6. Wonder what happens to keyboardservicesd in High Sierra? Flakey applet. Maybe certain Accessibility features make KeyboardViewer.app more stable? Feedback to Apple, requesting more robust app... Got to be careful here, new mp6,1 may be replaced soon. – mark stewart Oct 25 '17 at 16:31
  • user3439894 wrote: "is on no value without knowing what keyboard shortcut you assigned" – mark stewart Nov 1 '17 at 2:20

You must log in to answer this question.

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