-
Notifications
You must be signed in to change notification settings - Fork 0
/
im-auto.lua
54 lines (39 loc) · 1.01 KB
/
im-auto.lua
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
48
49
50
51
52
53
local log = hs.logger.new('im-auto', 'info')
local M = {}
-- Input Sources
M.S = {
['abc'] = 'com.apple.keylayout.ABC',
['pinyin'] = 'com.apple.inputmethod.SCIM.ITABC'
}
M.CMD = '/usr/local/bin/im-select'
-- Default opptions
M.D = {}
M.D.default_source_name = M.S.abc
M.D.restore_default_source_on_leave = true
M.set = function(source)
local curr_source = hs.keycodes.currentSourceID()
log.i('current', curr_source)
if curr_source == source then return end
log.i('set', source)
hs.execute(string.format('%s %s', M.CMD, source))
end
local app = {
['WeChat'] = M.S.pinyin
}
local function onApplicationChange(name)
local target_source = app[name]
if target_source then
M.set(target_source)
return
end
if M.D.restore_default_source_on_leave then
M.set(M.D.default_source_name)
end
end
local watcher_fun = function(name, type)
if (type == hs.application.watcher.activated) then
onApplicationChange(name)
end
end
WATCHER = hs.application.watcher.new(watcher_fun)
WATCHER:start()