Search code examples
applescript

Apple Script to click Show Keyboard Viewer


I am new to Apple Script. After many trials, I got a working script. But I have some questions.

  1. I am not quite sure what does below code mean. What does menu bar 2 referring to?
tell menu bar item 1 of menu bar 2
  1. I am not sure if I can believe this script is going to work in the previous MacOS versions. I don't care about very old versions but I do for recent versions. e.g. Big Sur or Ventura This question might sounds bit odd, but do you think I can say this script is going to work across multiple versions of macOS? If it was swift programming, I can say it's going to work for a while even if apple deprecated an API. But I am not sure if I can say that for apple script.

Below is a full working script.

tell application "System Events"
    tell process "TextInputMenuAgent"
        tell menu bar item 1 of menu bar 2
            click
            tell menu 1
                if exists menu item "Show Keyboard Viewer" then
                    click menu item "Show Keyboard Viewer"
                else
                    click menu item "Hide Keyboard Viewer"
                end if
            end tell
        end tell
    end tell
end tell

enter image description here


Solution

  • tell menu bar item 1 of menu bar 2
    

    The number here is something we should infer using a tool. For example, Xcode > Open Developer Tool > Accessibility Inspector.