Hammerspoon on OSX, you are missing out! It has some great features! I will write some more articles on using Hammerspoon to control your Apple trackpad and also tricks with "hyper" keys to control your desktop.
Back on track. These are all the keys on the number pad that Hammerspoon will treat as "unique":
pad., pad*, pad+, pad/, pad-, pad=,
pad0, pad1, pad2,
pad3, pad4, pad5,
pad6, pad7, pad8, pad9,
padclear, padenter
This means you can bind functionality to those keys without them also applying to the number row. Cool, right?
The following code is used to focus Chrome and then switch to the hangouts tab when pad0
is pressed:
local shortcutsLogger = hs.logger.new('shortcuts/shortcuts','debug')
-- "pad0" -- focus the Chrome, switch to the hangouts tab
hs.hotkey.bind({}, "pad0", "Focus Hangouts", function ()
shortcutsLogger:d("[pad0] pressed")
end, function ()
shortcutsLogger:d("[pad0] released")
local win = hs.appfinder.appFromName("Google Chrome")
win:activate()
-- @see https://www.hammerspoon.org/docs/hs.eventtap.html#keyStroke
hs.eventtap.keyStroke({"cmd","shift"}, "A", 300, win)
-- needed in order to use the tab search
hs.timer.doAfter(0.3, function()
hs.eventtap.keyStrokes("meet", win)
hs.timer.doAfter(0.2, function()
hs.eventtap.keyStroke({}, "Return", 100, win)
end)
end)
end, function ()
shortcutsLogger:d("[pad0] repeated")
end)
You can see that we do need to "wait" a bit between key presses in order to make sure we are triggering things properly.
One of the great things about using cmd+shift+A
is that it will find tabs across windows. This means you can have multiple windows open or even multiple desktops and it should still work!
I have another shortcut setup on pad.
that will switch to the hangouts tab and toggle "mute":
-- "pad." -- focus the Chrome, switch to the hangouts tab, toggle mute
hs.hotkey.bind({}, "pad.", "Toggle Hangouts Mute", function ()
shortcutsLogger:d("[pad.] pressed")
end, function ()
shortcutsLogger:d("[pad.] released")
local win = hs.appfinder.appFromName("Google Chrome")
win:activate()
-- @see https://www.hammerspoon.org/docs/hs.eventtap.html#keyStroke
hs.eventtap.keyStroke({"cmd","shift"}, "A", 300, win)
-- needed in order to use the tab search
hs.timer.doAfter(0.3, function()
hs.eventtap.keyStrokes("meet", win)
hs.timer.doAfter(0.2, function()
hs.eventtap.keyStroke({}, "Return", 100, win)
-- presses this combo after the tab is in focus
hs.timer.doAfter(0.3, function()
hs.eventtap.keyStroke({"cmd"}, "D", 100, win)
end)
end)
end)
end, function ()
shortcutsLogger:d("[pad.] repeated")
end)
Those added lines trigger the keyboard shortcut that will toggle mute in hangouts.
If you want to modify the code above to work in other browsers, just change the line for hs.appfinder.appFromName("Google Chrome")
to your browser. You will also need to figure out what the key combo is for finding tabs in that browser.
In Firefox, for example, you can search the open tabs using %
in the search bar. So you would need to do something like the following:
1. cmd+L (activate the search bar)
2. % (start the tab search)
3. "meet" (type meet into the bar)
4. Return (press enter to go to the tab)
This is just a list of possible steps. Try it yourself and report back to me!
I'm a full-stack developer, co-organizer of PHP Vancouver meetup, and winner of a Canadian Developer 30 under 30 award. I'm a huge Open Source advocate and contributor to a lot of projects in my community. When I am not sitting at a computer, I'm trying to perfect some other skill.