Haupz Blog

... still a totally disordered mix

Hammerspoon

2022-03-19 — Michael Haupt

By chance, I came across Hammerspoon a while ago. This is a pretty amazing tool for Mac automation. It hooks right into a (still growing) number of the macOS APIs and allows for fine-grained control. It also offers numerous event listeners that allow for reacting to things happening, e.g., signing on to a certain WiFi network, connecting a specific external monitor, battery charge dropping below a certain threshold, and so forth. It's also possible to define hotkeys for just about everything. Use Cmd-Option-Ctrl-B to open a URL copied to the clipboard in the tracking-quenching Brave Browser? You got it:

hs.hotkey.bind({"cmd", "alt", "ctrl"}, "B", function()
  local url = hs.pasteboard.readString()
  hs.applescript('tell Application "Brave Browser" to open location "' .. url .. '"')
end)

The goodness is brought about by the Lua scripting language. Lua is very lightweight and yet powerful, and easy to learn. Oh, and Hammerspoon has some very good API documentation, too.

Tags: hacking