0

I have the AppleScript below that worked great in Big Sur. But after upgrading to Monterey, it stopped working. Can anyone help me fix it?

Thanks!

changeKeyboardLayout("Squirrel")

on changeKeyboardLayout(layoutName)
    tell application "System Events"
        tell process "TextInputMenuAgent"
            click menu item layoutName of menu 1 of menu bar item 1 of menu bar 2
            click menu bar item 1 of menu bar 2
        end tell
    end tell
end changeKeyboardLayout

If I change layoutName to a number, such as 1

click menu item 1 of menu 1 of menu bar item 1 of menu bar 2

ScriptDebugger shows

menu item "U.S." of menu 1 of menu bar item 1 of menu bar 2 of application process "TextInputMenuAgent"

enter image description here

So it is able to get U.S., but the click simulation does not have any effects, i.e., it does not switch the keyboard layouts to U.S. (from another keyboard).

Martin
  • 155
  • 10

1 Answers1

1

The example AppleScript code, shown below, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Example AppleScript code:

my changeKeyboardLayoutTo("U.S.")

to changeKeyboardLayoutTo(layoutName)
    ignoring application responses
        tell application "System Events" to ¬
            click menu bar item 1 of menu bar 2 of ¬
                application process "TextInputMenuAgent"
    end ignoring
    
    delay 0.1
    do shell script "killall 'System Events'"
    delay 0.2
    
    tell application "System Events"
        launch
        click menu item layoutName of menu 1 of ¬
            menu bar item 1 of menu bar 2 of ¬
            application process "TextInputMenuAgent"
    end tell
end changeKeyboardLayoutTo

Notes:

The example AppleScript code assumes one has checked Show Input menu in menu bar in: System Preferences > Keyboard Input Sources

The example AppleScript code is also coded to workaround the ~5 second delay that is a know issue between actuating the primary menu and the the target menu item on the menu.

See my answer A: AppleScript - Can't get rid of delay after click for the reference to mentioned ~5 second delay, which still persists in macOS Big Sur and macOS Monterey. It also includes other methods to click the target menu items that can be adapted as/if needed. Although I have not tested the alternate solutions in macOS Monterey yet.


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

user3439894
  • 7,266
  • 3
  • 17
  • 28
  • Hi @user3439894, thanks. But it doesn't work for me either. I have **Show Input menu in menu bar** checked. It doesn't do anything on my Mac. I was thinking that maybe something was broken during the upgrade (to Monterey). – Martin Oct 29 '21 at 20:17
  • @Martin, The _example_ **AppleScript** _code_ shown in my answer does indeed work in **macOS Monterey** as stated. I'm using a clean install of **macOS Monterey** not an upgrade. I'd go into **System Preferences** > **Security & Privacy** > **Privacy** > **Accessibility** and _toggle the bit_, so to speak, by unchecking/rechecking _applications_ as appropriate to see if that will resolve your issue. – user3439894 Oct 29 '21 at 20:27
  • I had a smc reset and the script is working now. Thanks. The script I posed in the OP used to work with Big Sur, and it did not show the keyboard menu when switching keyboards, nor does it need to `do shell script "killall 'System Events'"`. Do you know why it stopped working in Monterey? Thanks! – Martin Oct 29 '21 at 20:43
  • I do not want to see the click menu because it may cause some inconvenience. For example, when I have Alfred activated and switch keyboard layouts, it will cause Alfred to be deactivated. – Martin Oct 29 '21 at 20:58
  • @Martin, RE: "Do you know why it stopped working in Monterey? " -- No, sorry. -- RE: "when I have Alfred activated and switch keyboard layouts, it will cause Alfred to be deactivated" -- Can you not just add either e.g. `tell application "Alfred" to activate` or `tell application "System Events" to set frontmost of process "Alfred" to true` – user3439894 Oct 29 '21 at 21:13
  • But I don't always switch keyboard layout when Alfred is activated. I could switch keyboard anytime. Alfred is just one example to show why your solution could interfere with what I might be doing. – Martin Oct 29 '21 at 21:22
  • @Martin, Sorry I was assuming you trigger the _keyboard shortcut_ to toggle the **Input menu** with **Alfred**. BTW I think there may be a _command line utility_ that can change the **Input** _source_. Let me check into it. – user3439894 Oct 29 '21 at 21:34
  • @Martin, Have a look at: https://stackoverflow.com/questions/23729704/change-osx-keyboard-layoutinput-source-programmatically-via-terminal-or-appl/63232278#63232278 -- I just compiled the _code_ to an executable and tested it in **macOS Monterey** and it works quite nice and it changed it instantly. – user3439894 Oct 29 '21 at 21:53
  • I saw it. I actually tried it before and also today. It works for some keyboards, but not for all. For example, it does not work for [rime/squirrel](https://github.com/rime/squirrel), even I have put `im.rime.inputmethod.Squirrel` there. Also, some keyboards do not have `Bundle ID` in the `plist` file. – Martin Oct 29 '21 at 21:58