通过终端或 AppleScript 以编程方式更改 OSX 键盘布局(“输入源")? [英] Change OSX keyboard layout("input source") programmatically via terminal or AppleScript?

查看:31
本文介绍了通过终端或 AppleScript 以编程方式更改 OSX 键盘布局(“输入源")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过 Alfred 运行 GUI AppleScript 来切换输入源,有时 GUI 脚本可能需要 1 秒才能完成更改.有时会很烦人.

I am currently switching input sources by running a GUI AppleScript through Alfred, and the GUI script can sometime take up to 1s to complete the change. It gets quite annoying at times.

我遇到了确定终端/脚本中的 OS X 键盘布局(输入源").我想知道,既然我们可以找出当前的输入源,是否可以以编程方式更改输入源?我试过覆盖 com.apple.HIToolbox.plist 但它不会改变输入.

I have come across Determine OS X keyboard layout ("input source") in the terminal/a script. And I want to know since we can find out the current input source if there's a way to change input source programatically? I'd tried overwriting the com.apple.HIToolbox.plist but it does not change the input.

(我确实意识到在系统首选项中有可用的输入源的映射快捷方式,但是我更喜欢使用 Alfred 映射关键字)

(I do realise there's mapping shortcut to input sources available in the system preference, however I prefer mapping keywords with Alfred)

推荐答案

您可以使用文本输入服务 API:

You can do it using the Text Input Services API:

NSArray* sources = CFBridgingRelease(TISCreateInputSourceList((__bridge CFDictionaryRef)@{ (__bridge NSString*)kTISPropertyInputSourceID : @"com.apple.keylayout.French" }, FALSE));
TISInputSourceRef source = (__bridge TISInputSourceRef)sources[0];
OSStatus status = TISSelectInputSource(source);
if (status != noErr)
    /* handle error */;

第一行的字典可以使用其他属性作为选择输入源的其他标准.

The dictionary in the first line can use other properties for other criteria for picking an input source.

还有 NSTextInputContext.它有一个 selectedKeyboardInputSource ,它可以设置为输入源 ID 以选择不同的输入源.问题在于您需要一个 NSTextInputContext 的实例来使用,并且只有当您有一个带有文本视图的关键窗口作为其第一响应者时,其中一个才存在.

There's also NSTextInputContext. It has a selectedKeyboardInputSource which can be set to an input source ID to select a different input source. The issue there is that you need an instance of NSTextInputContext to work with and one of those exists only when you have a key window with a text view as its first responder.

这篇关于通过终端或 AppleScript 以编程方式更改 OSX 键盘布局(“输入源")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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