确定终端/脚本中的 OS X 键盘布局(“输入源")? [英] Determine OS X keyboard layout ("input source") in the terminal/a script?

查看:20
本文介绍了确定终端/脚本中的 OS X 键盘布局(“输入源")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从终端确定 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/'"

注意如何将 \ 转义为 \\ 以保证 AppleScript 的好处,这确保了 \ 到达 shell.如果您希望直接从 shell 执行相同的命令(作为一行),它将是:
默认读取 ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources |egrep -w '键盘布局名称' |sed -E 's/^.+ = \"?([^\"]+)\"?;$/\1/'

Note how \ is escaped as \\ for the benefit of AppleScript, which ensures that just \ reaches the shell. If you want to execute the same command directly from the shell (as one line), it would be:
defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | egrep -w 'KeyboardLayout Name' |sed -E 's/^.+ = \"?([^\"]+)\"?;$/\1/'

  • 当前选择的键盘布局存储在用户级文件~/Library/Preferences/com.apple.HIToolbox.plist,顶级键AppleSelectedInputSources,子键 KeyboardLayout Name.
  • defaults read 确保读取当前 设置(遗憾的是,从 OSX 10.9 开始,其他方面优于 /usr/libexec/PlistBuddy只能看到一个缓存版本,可能不同步).
  • 由于 defaults read 不能返回单个键的值,必须通过 egrepsed 提取感兴趣的值 - 有一个警告defaults read 有条件地在键名和字符串值周围使用双引号,这取决于它们是否是单个单词(没有标点符号).
  • The currently selected keyboard layout is stored in the user-level file ~/Library/Preferences/com.apple.HIToolbox.plist, top-level key AppleSelectedInputSources, subkey KeyboardLayout 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 via egrep and sed - one caveat there is that defaults 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 处理程序;它使用基于 do shell script 的解决方法来确保 plist 文件是最新的,否则通过应用程序 Property List Suite 使用 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.

注意:显然,上面的在这种情况下基于shell的方法要短得多,但下面的代码演示了处理属性列表的一般技术.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆