Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created July 14, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/251618b459d3af3133d22057ded0acce to your computer and use it in GitHub Desktop.
Save james2doyle/251618b459d3af3133d22057ded0acce to your computer and use it in GitHub Desktop.
Hammerspoon Keyboard Shortcuts (Toggle Apps/Shortcuts With The Keyboard)
--[[
Name: Shortcuts (Toggle Apps/Shortcuts With The Keyboard)
Author: James Doyle <james2doyle@gmail.com>
Description: Run the shortcut that toggles apps and functions
Demo: <None>
Installation: Just require this file in your Hammerspoon init.lua file
Usage:
Press the keybinding
The shortcut/app will run or focus and there will be a notification
]]--
-- "cmd", "command" or "⌘"
-- "ctrl", "control" or "⌃"
-- "alt", "option" or "⌥"
-- "shift" or "⇧"
-- Hyper (Option+Shift+Ctrl+Cmd)
-- Meh (Alt+Shift+Ctrl)
local shortcutsLogger = hs.logger.new('shortcuts/shortcuts','debug')
local hyper_key_shortcuts = {}
hyper_key_shortcuts["S"] = "Sublime Text"
hyper_key_shortcuts["C"] = "Google Chrome"
hyper_key_shortcuts["T"] = "WezTerm"
hyper_key_shortcuts["E"] = "Canary Mail"
hyper_key_shortcuts["P"] = "Postman"
hyper_key_shortcuts["W"] = "WhatsApp"
local automation_key_shortcuts = {}
automation_key_shortcuts["B"] = "Toggle Bluetooth Headphones"
for key,title in hs.fnutils.sortByKeys(hyper_key_shortcuts) do
hs.hotkey.bind({"cmd","ctrl","option","shift"}, key, title, function ()
shortcutsLogger:d("[hyper "..key.."] pressed")
end, function ()
shortcutsLogger:d("[hyper "..key.."] released")
-- @see https://www.hammerspoon.org/docs/hs.application.html#open
hs.application.open(title)
end, function ()
shortcutsLogger:d("[hyper "..key.."] repeated")
end)
end
for key,title in hs.fnutils.sortByKeys(automation_key_shortcuts) do
hs.hotkey.bind({"option"}, key, title.."...", function ()
shortcutsLogger:d("[option + "..key.."] pressed")
end, function ()
shortcutsLogger:d("[option + "..key.."] released")
-- @see https://www.hammerspoon.org/docs/hs.shortcuts.html#run
hs.shortcuts.run(title)
end, function ()
shortcutsLogger:d("[option + "..key.."] repeated")
end)
end
-- cmd+ctrl+option+shift X
hs.hotkey.bind({"cmd","ctrl","option","shift"}, "X", "Slack", function ()
shortcutsLogger:d("[hyper X] pressed")
end, function ()
shortcutsLogger:d("[hyper X] released")
local win = hs.appfinder.appFromName("Google Chrome")
win:activate()
-- @see https://www.hammerspoon.org/docs/hs.eventtap.html#keyStroke
hs.eventtap.keyStroke({"cmd"}, "1", 300, win)
end, function ()
shortcutsLogger:d("[hyper X] repeated")
end)
-- cmd+ctrl+option+shift Y
hs.hotkey.bind({"cmd","ctrl","option","shift"}, "Y", "Opening Hammerspoon Config...", function ()
shortcutsLogger:d("[hyper Y] pressed")
end, function ()
shortcutsLogger:d("[hyper Y] released")
-- @see https://www.hammerspoon.org/docs/hs.html#execute
hs.execute('open https://www.hammerspoon.org/docs/index.html')
hs.execute('open $HOME/Dropbox/Apps/env/hammerspoon/init.lua', true)
end, function ()
shortcutsLogger:d("[hyper Y] repeated")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment