/
set-ime.py
executable file
路47 lines (42 loc) 路 1.07 KB
/
set-ime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
import sys
import objc
import Foundation
#
# Read ARGV[1]
#
if len(sys.argv) != 2:
print 'usage: set-ime <name>'
sys.exit(1)
desired_ime_name = sys.argv[1]
#
# Load carbon framework
#
carbon = {}
bundle = Foundation.NSBundle.bundleWithIdentifier_('com.apple.HIToolbox')
objc.loadBundleFunctions(bundle, carbon, [
('TISCreateInputSourceList','@@B'),
('TISGetInputSourceProperty', '@@@'),
('TISSelectInputSource', 'I@'),
])
objc.loadBundleVariables(bundle, carbon, [
('kTISPropertyLocalizedName', '@'),
])
#
# Lookup all available IMEs and find desired IME
#
for ime in carbon['TISCreateInputSourceList'](None, False):
ime_name = carbon['TISGetInputSourceProperty'](ime, carbon['kTISPropertyLocalizedName'])
if desired_ime_name == ime_name:
chosen_ime = ime
break
else:
print 'Input source "{}" is not available'.format(desired_ime_name)
sys.exit(1)
#
# Change IME
#
err = carbon['TISSelectInputSource'](chosen_ime)
if err != 0:
print 'Could not change input language (OSStatus = {})'.format(err)
sys.exit(1)