Managing Focus With Hammerspoon

Hammerspoon is a fantastic tool for automating common tasks such as window management, hotkeys, and much more. It uses Lua to allow you to interface with many low-level macOS APIs. Recently I improved my config to manage Focus (do not disturb).

A recent use case I had for Hammerspoon was to manage Focus when starting and ending Zoom meetings where I want it to automatically start when the meeting starts, then automatically turn off when the meeting ends. With Hammerspoon’s capabilities to subscribe to window changes, it only requires a few lines of Lua code in your Hammerspoon config to make this happen.

local function focus(state)
	return function()
        hs.execute("shortcuts run 'Focus' <<<'" .. state .. "'")
	end
end

hs.window.filter
	.new(function(window)
		return window ~= nil and window:title() == "Zoom Meeting"
	end)
	:subscribe(hs.window.filter.windowCreated, focus("on"))
	:subscribe(hs.window.filter.windowTitleChanged, focus("on"))
	:subscribe(hs.window.filter.windowDestroyed, focus("off"))

The only unfortunate part of this, is that you do have to create a shortcut in the Shortcuts app that will actually enable/disable focus mode, since Apple removed the APIs that once made that possible in Swift or via the terminal. If you are interested in using this technique yourself, feel free to install the shortcut I built for this purpose.