确定OS X的键盘布局("输入源")在终端/脚本? [英] Determine OS X keyboard layout ("input source") in the terminal/a script?
问题描述
我想确定OS X键盘布局(或输入源为OS X调用它)从终端,这样我可以在像TMUX状态栏的地方表现出来。
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.
所以,我想知道,如果目前的布局是美国或瑞典 - 亲。例如:
So I want to know if the current layout is "U.S." or "Swedish - Pro" for example.
谷歌搜索变成了没有我的。这可能吗?
Googling turns up nothing for me. Is this possible?
推荐答案
请注意:@MarkSetchell值得称赞想出的根本途径 - 从哪里[开始]外观和使用什么样的工具。
后在评论进一步调查和来回我想我总结的解决方案(如OS X 10.9.1的):
Note: @MarkSetchell deserves credit for coming up with the fundamental approach - where to [start to] look and what tools to use. After further investigation and back and forth in the comments I thought I'd summarize the 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/'"
- 当前选择的键盘布局存储在用户级的文件
〜/库/ preferences / com.apple.HIToolbox.plist
,顶级键AppleSelectedInputSources
,子项KeyboardLayout名称
。 -
默认读
确保的读取电流的设置(可悲的是,作为OSX 10.9时,否则上级/ USR / libexec目录/ PlistBuddy
只能看到的缓存的版本,这可能是不同步的)。 - 由于
默认读
不能返回单个键的值,感兴趣的值必须通过egrep的
提取和SED
- 一个警告存在着默认读
的条件的使用左右键的名称和字符串双引号值,这取决于它们是否是一个字(不含标点)或不 - The currently selected keyboard layout is stored in the user-level file
~/Library/Preferences/com.apple.HIToolbox.plist
, top-level keyAppleSelectedInputSources
, subkeyKeyboardLayout Name
. defaults read
ensures that the current settings are read (sadly, as of OSX 10.9, the otherwise superior/usr/libexec/PlistBuddy
sees only a cached version, which may be out of sync).- Since
defaults read
cannot return an individual key's value, the value of interest must be extracted viaegrep
andsed
- one caveat there is thatdefaults read
conditionally uses double quotes around key names and string values, depending on whether they are a single word (without punctuation) or not.
更新
原来,AppleScript的本身可以解析属性列表,但它是一个有点像拔牙。
此外,令人难以置信的,潜在的,未充分电流值问题也影响的AppleScript的解析。
Turns out that AppleScript itself can parse property lists, but it's a bit like pulling teeth. Also, incredibly, the potentially-not-fully-current-values problem also affects AppleScript's parsing.
下面是一个AppleScript处理程序,获取当前的键盘布局;它采用了做shell脚本
为基础的解决办法,以确保plist文件是最新的,但以其他方式使用AppleScript的的属性列表功能,通过属性列表套房
应用系统事件的
。
Below is an AppleScript handler that gets the current keyboard layout; it uses a do shell script
-based workaround to ensure that the plist file is current, but otherwise uses AppleScript's property-list features, via the Property List Suite
of application System Events
.
注意:显然,上述基于壳的方法是在这种情况下要短得多,但低于code表明的一般技术与属性列表的工作
Note: Obviously, the above shell-based approach is much shorter in this case, but the code below demonstrates general techniques for working with property lists.
# Example call.
set activeKbdLayout to my getActiveKeyboardLayout() # ->, e.g., "U.S."
on getActiveKeyboardLayout()
# Surprisingly, using POSIX-style paths (even with '~') works with
# the `property list file` type.
set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"
# !! First, ensure that the plist cache is flushed and that the
# !! *.plist file contains the current value; simply executing
# !! `default read` against the file - even with a dummy
# !! key - does that.
try
do shell script "defaults read " & plistPath & " dummy"
end try
tell application "System Events"
repeat with pli in property list items of ¬
property list item "AppleSelectedInputSources" of ¬
property list file plistPath
# Look for (first) entry with key "KeyboardLayout Name" and return
# its value.
# Note: Not all entries may have a 'KeyboardLayout Name' key,
# so we must ignore errors.
try
return value of property list item "KeyboardLayout Name" of pli
end try
end repeat
end tell
end getActiveKeyboardLayout
这篇关于确定OS X的键盘布局("输入源")在终端/脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!