Determine OS X keyboard layout (“input source”) in the terminal/a script?

后端 未结 6 1427
感情败类
感情败类 2020-12-23 15:25

I would like to determine the OS X keyboard layout (or \"input source\" as OS X calls it) from the terminal so that I can show it in places like the tmux status bar.

6条回答
  •  星月不相逢
    2020-12-23 16:17

    Figured out how to do it with AppleScript, assuming you have the menu bar input menu.

    Run this in a terminal:

    osascript -e 'tell application "System Events" to tell process "SystemUIServer" to get the value of the first menu bar item of menu bar 1 whose description is "text input"'
    

    Works fine even if you only show the input menu as flag icons, without the input source name.

    Mavericks will probably prompt you to allow access, the first time. In earlier versions of OS X I suspect you'll need to turn on support for assistive devices in your accessibility preferences.

提交回复
热议问题