summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar/plugins/get_input_source.sh
blob: ccddfc16ed433ffdb406f89a9a667a2aca05a4a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh

PLIST="$HOME/Library/Preferences/com.apple.HIToolbox.plist"

get_source_id() {
  /usr/bin/defaults read "$PLIST" AppleCurrentKeyboardLayoutInputSourceID 2>/dev/null && return 0
  /usr/libexec/PlistBuddy -c 'Print :AppleSelectedInputSources' "$PLIST" 2>/dev/null |
    awk '
      BEGIN { RS=""; FS="\n" }
      {
        # split by entries, pick InputSourceID preceded by InputSourceKind
        n=split($0, lines, "\n")
        kind=""; id=""
        for (i=1;i<=n;i++) {
          if (lines[i] ~ /InputSourceKind/) {
            if (lines[i] ~ /Keyboard Layout/) kind="kbd"; else kind="ime"
          } else if (lines[i] ~ /InputSourceID/) {
            sub(/.*= /,"",lines[i]); sub(/;$/,"",lines[i]); id=lines[i]
          }
        }
        if (id != "") {
          if (kind=="kbd") { print id; exit } # prefer kbd
          last=id
        }
      }
      END { if (last!="") print last }'
}

SOURCE_ID="$(get_source_id | tr -d '"')"

if printf %s "$SOURCE_ID" | grep -q 'com\.apple\.keylayout\.2SetHangul'; then
  ICON="􀂩"
else
  ICON="􀂕"
fi

sketchybar --set input_source icon="$ICON"