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

后端 未结 6 1389
感情败类
感情败类 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 15:53

    I am not sure of this answer, but it may be worth checking out. If you look in file:

    /Library/Preferences/com.apple.HIToolbox.plist
    

    there is a variable called

    AppleCurrentKeyboardLayoutSourceID
    

    and mine is set to "British" and I am in Britain...

    You can read the file in a script with:

    defaults read /Library/Preferences/com.apple.HIToolbox.plist  AppleEnabledInputSources
    

    sample output below:

    (
        {
        InputSourceKind = "Keyboard Layout";
        "KeyboardLayout ID" = 2;
        "KeyboardLayout Name" = British;
    }
    )
    

    So, I guess your question can be simply answered using this:

    #!/bin/bash
    defaults read /Library/Preferences/com.apple.HIToolbox.plist  AppleEnabledInputSources | grep -sq Swedish
    [[ $? -eq 0 ]] && echo Swedish
    

提交回复
热议问题