Created
December 10, 2022 07:25
-
-
Save zhang0098/765415b394585f88748328e7bab2756b to your computer and use it in GitHub Desktop.
Hammerspoon input method auto switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function Chinese() | |
-- hs.keycodes.currentSourceID("com.sogou.inputmethod.sogou.pinyin") | |
hs.keycodes.currentSourceID("com.apple.inputmethod.SCIM.ITABC") | |
end | |
local function English() | |
hs.keycodes.currentSourceID("com.apple.keylayout.ABC") | |
end | |
local targetApps = { | |
'微信', | |
'Notion', | |
} | |
function updateFocusAppInputMethod() | |
local focusApp = hs.application.frontmostApplication():name() | |
for i, app in ipairs(targetApps) do | |
if focusApp == app then | |
Chinese() | |
return | |
end | |
end | |
English() | |
end | |
hs.hotkey.bind({'ctrl', 'alt','cmd'}, ".", function() | |
hs.alert.show("App path: " | |
..hs.application.frontmostApplication():path() | |
.."\n" | |
.."App name: " | |
..hs.application.frontmostApplication():name() | |
.."\n" | |
.."IM source id: " | |
..hs.keycodes.currentSourceID()) | |
end) | |
function applicationWatcher(appName, eventType, appObject) | |
if (eventType == hs.application.watcher.activated) then | |
updateFocusAppInputMethod() | |
end | |
end | |
appWatcher = hs.application.watcher.new(applicationWatcher) | |
appWatcher:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment