-
Notifications
You must be signed in to change notification settings - Fork 114
/
ime.lua
64 lines (57 loc) · 1.99 KB
/
ime.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
54
55
56
57
58
59
60
61
62
63
64
local function Chinese()
hs.keycodes.currentSourceID("com.sogou.inputmethod.sogou.pinyin")
end
local function English()
hs.keycodes.currentSourceID("com.apple.keylayout.ABC")
end
-- app to expected ime config
local app2Ime = {
{'/Applications/iTerm.app', 'English'},
{'/Applications/Xcode.app', 'English'},
{'/Applications/Google Chrome.app', 'Chinese'},
{'/System/Library/CoreServices/Finder.app', 'English'},
{'/Applications/DingTalk.app', 'Chinese'},
{'/Applications/Kindle.app', 'English'},
{'/Applications/NeteaseMusic.app', 'Chinese'},
{'/Applications/微信.app', 'Chinese'},
{'/Applications/System Preferences.app', 'English'},
{'/Applications/Dash.app', 'English'},
{'/Applications/MindNode.app', 'Chinese'},
{'/Applications/Preview.app', 'Chinese'},
{'/Applications/wechatwebdevtools.app', 'English'},
{'/Applications/Sketch.app', 'English'},
}
function updateFocusAppInputMethod()
local focusAppPath = hs.window.frontmostWindow():application():path()
for index, app in pairs(app2Ime) do
local appPath = app[1]
local expectedIme = app[2]
if focusAppPath == appPath then
if expectedIme == 'English' then
English()
else
Chinese()
end
break
end
end
end
-- helper hotkey to figure out the app path and name of current focused window
hs.hotkey.bind({'ctrl', 'cmd'}, ".", function()
hs.alert.show("App path: "
..hs.window.focusedWindow():application():path()
.."\n"
.."App name: "
..hs.window.focusedWindow():application():name()
.."\n"
.."IM source id: "
..hs.keycodes.currentSourceID())
end)
-- Handle cursor focus and application's screen manage.
function applicationWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
updateFocusAppInputMethod()
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()