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

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

问题描述

我目前通过阿尔弗雷德运行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.

我所遇到的<一个href=\"http://stackoverflow.com/questions/21597804/determine-os-x-keyboard-layout-input-source-in-the-terminal-a-script\">Determine 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.

(我不知道有映射快捷方式在系统preference可用输入源,但是我preFER映射关键字与阿尔弗雷德)

(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.

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

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