Skip to content

Instantly share code, notes, and snippets.

@Gronis
Last active October 23, 2024 17:40
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
# .config/ghostty/config
theme = shades-of-purple
background = #1f1f1f
foreground = #ffffff
cursor-color = #ffffff
selection-foreground = #000000
selection-background = #eeeeee
bold-is-bright = true
keybind = alt+right=esc:f
keybind = alt+left=esc:b
keybind = super+right=text:\x05
keybind = super+left=text:\x01
keybind = alt+backspace=text:\x1b\x7f
keybind = alt+delete=esc:d
keybind = super+backspace=text:\x15
keybind = super+delete=text:\x0b
keybind = ctrl+alt+left=goto_split:left
keybind = ctrl+alt+right=goto_split:right
keybind = super+alt+left=next_tab
keybind = super+alt+right=previous_tab
macos-titlebar-style = transparent
focus-follows-mouse = true
-- 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("Ghostty") 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