Skip to content

Instantly share code, notes, and snippets.

@Gronis
Last active January 15, 2024 09:53
Show Gist options
  • Save Gronis/5e0924a14d9915ffeffb59970188e980 to your computer and use it in GitHub Desktop.
Save Gronis/5e0924a14d9915ffeffb59970188e980 to your computer and use it in GitHub Desktop.
hammerspoon application toggler
-- This hammerspoon config enables the possibility of using both normal keys and system keys to toggle applications
function create_hotkeys_service()
local service = { event_handlers = {} }
function service.bind_hotkey(modifiers, key, action)
if hs.keycodes.map[key] then
hs.hotkey.bind(modifiers, key, action)
return
end
local down = true
local flags = {}
for i,mod in pairs(modifiers) do
flags[mod] = true
end
if(service.event_handlers[key] == nil) then
service.event_handlers[key] = {}
end
table.insert(service.event_handlers[key], {down, modifiers, action})
end
function service.start()
service.proc = hs.eventtap.new({hs.eventtap.event.types.systemDefined}, function(event)
local system_key = event:systemKey()
local event_modifiers = event:getFlags()
local handlers = service.event_handlers[system_key.key]
if handlers then
for i,handler in pairs(handlers) do
local handler_down = handler[1]
local handler_mods = handler[2]
local handler_action = handler[3]
if system_key.down == handler_down and event_modifiers:containExactly(handler_mods) then
handler_action()
end
end
end
end)
service.proc:start()
end
return service
end
function create_application_service()
local application = {}
function application.toggle_app(app)
local app = hs.application.get(app)
if app then
if not app:mainWindow() then
app:selectMenuItem({app, "New OS window"})
elseif app:isFrontmost() then
app:hide()
else
app:activate()
end
else
hs.application.launchOrFocus(app)
end
end
return application
end
mac = {
hotkeys = create_hotkeys_service(),
application = create_application_service(),
}
mac.hotkeys.bind_hotkey({"cmd"}, "PLAY", function() mac.application.toggle_app('Spotify') end)
mac.hotkeys.bind_hotkey({}, "§", function() mac.application.toggle_app("kitty") end)
mac.hotkeys.bind_hotkey({"cmd"}, "§", function() mac.application.toggle_app("Google Chrome") end)
mac.hotkeys.bind_hotkey({"cmd"}, "M", function() mac.application.toggle_app("Mail") end)
mac.hotkeys.bind_hotkey({"cmd"}, ".", function() mac.application.toggle_app("VSCodium") end)
mac.hotkeys.start()
[
{
"key": "alt+cmd+-",
"command": "editor.action.jumpToBracket",
"when": "editorTextFocus"
},
{
"key": "shift+alt+cmd+-",
"command": "editor.action.selectToBracket"
}
]
font_size 13.0
font_family JetBrainsMonoNL Nerd Font Mono Regular
bold_font JetBrainsMonoNL Nerd Font Mono ExtraBold
italic_font JetBrainsMonoNL Nerd Font Mono Regular Italic
bold_italic_fon JetBrainsMonoNL Nerd Font Mono ExtraBold Italic
window_padding_width 2
draw_minimal_borders yes
hide_window_decorations titlebar-only
map cmd+c copy_or_interrupt
map ctrl+shift+- no_op
map ctrl+shift++ no_op
background #1f1f1f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment