Skip to content

Instantly share code, notes, and snippets.

@ianjamieson
Last active April 23, 2025 23:41
Show Gist options
  • Save ianjamieson/6e0e983839227fe57ac86ce8909d7cb9 to your computer and use it in GitHub Desktop.
Save ianjamieson/6e0e983839227fe57ac86ce8909d7cb9 to your computer and use it in GitHub Desktop.
Highlight macOS active windows using Hammerspoon init.lua
local highlight = nil
local function highlightWindow()
-- Clear any existing highlight
if highlight then
highlight:delete()
highlight = nil
end
-- Get the currently focused window
local win = hs.window.focusedWindow()
if not win then
return
end
-- Get the frame of the focused window
local frame = win:frame()
-- Create a new rectangle that slightly expands beyond the window's bounds
local borderWidth = 5 -- Width of the border
local highlightFrame = hs.geometry.rect(
frame.x - borderWidth,
frame.y - borderWidth,
frame.w + (2 * borderWidth),
frame.h + (2 * borderWidth)
)
-- Create the highlight rectangle
highlight = hs.drawing.rectangle(highlightFrame)
highlight:setStrokeColor({["red"] = 1, ["green"] = 0, ["blue"] = 0, ["alpha"] = 0.8})
highlight:setStrokeWidth(borderWidth)
highlight:setFill(false)
highlight:setRoundedRectRadii(10, 10) -- Optional: rounded corners
highlight:bringToFront(true) -- Ensure it's visible on top of the window
highlight:show()
end
-- Bind the function to a hotkey (e.g., Ctrl + Alt + H)
-- hs.hotkey.bind({"ctrl", "alt"}, "H", highlightWindow)
-- Automatically remove the highlight when the focus changes
hs.window.filter.default:subscribe(hs.window.filter.windowFocused, function()
highlightWindow()
end)
hs.window.filter.default:subscribe(hs.window.filter.windowUnfocused, function()
if highlight then
highlight:delete()
highlight = nil
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment