I am new to Apple Script. After many trials, I got a working script. But I have some questions.
menu bar 2
referring to?tell menu bar item 1 of menu bar 2
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
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.