Skip to content

Instantly share code, notes, and snippets.

@zhang0098
Created December 10, 2022 07:25
Show Gist options
  • Save zhang0098/765415b394585f88748328e7bab2756b to your computer and use it in GitHub Desktop.
Save zhang0098/765415b394585f88748328e7bab2756b to your computer and use it in GitHub Desktop.
Hammerspoon input method auto switch
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