osx - Determine OS X keyboard layout ("input source") in the terminal/a script? -
i determine os x keyboard layout (or "input source" os x calls it) terminal can show in places tmux status bar.
so want know if current layout "u.s." or "swedish - pro" example.
googling turns nothing me. possible?
note: @marksetchell deserves credit coming fundamental approach - [start to] , tools use. after further investigation , , forth in comments thought i'd summarize solution (as of os x 10.9.1):
do shell script "defaults read ~/library/preferences/com.apple.hitoolbox.plist \\ appleselectedinputsources | \\ egrep -w 'keyboardlayout name' | sed -e 's/^.+ = \"?([^\"]+)\"?;$/\\1/'"
- the selected keyboard layout stored in user-level file
~/library/preferences/com.apple.hitoolbox.plist
, top-level keyappleselectedinputsources
, subkeykeyboardlayout name
. defaults read
ensures current settings read (sadly, of osx 10.9, otherwise superior/usr/libexec/plistbuddy
sees cached version, may out of sync).- since
defaults read
cannot return individual key's value, value of interest must extracted viaegrep
,sed
- 1 caveat theredefaults read
conditionally uses double quotes around key names , string values, depending on whether single word (without punctuation) or not.
update:
turns out applescript can parse property lists, it's bit pulling teeth. also, incredibly, potentially-not-fully-current-values problem affects applescript's parsing.
below applescript handler gets current keyboard layout; uses do shell script
-based workaround ensure plist file current, otherwise uses applescript's property-list features, via property list suite
of application system events
.
note: obviously, above shell-based approach shorter in case, code below demonstrates general techniques working property lists.
# example call. set activekbdlayout getactivekeyboardlayout() # ->, e.g., "u.s." on getactivekeyboardlayout() # surprisingly, using posix-style paths (even '~') works # `property list file` type. set plistpath "~/library/preferences/com.apple.hitoolbox.plist" # !! first, ensure plist cache flushed , # !! *.plist file contains current value; executing # !! `default read` against file - dummy # !! key - that. try shell script "defaults read " & plistpath & " dummy" end try tell application "system events" repeat pli in property list items of ¬ property list item "appleselectedinputsources" of ¬ property list file plistpath # (first) entry key "keyboardlayout name" , return # value. # note: not entries may have 'keyboardlayout name' key, # must ignore errors. try return value of property list item "keyboardlayout name" of pli end try end repeat end tell end getactivekeyboardlayout
Comments
Post a Comment