Skip to content

Instantly share code, notes, and snippets.

@ianjamieson
Last active February 11, 2026 09:32
Show Gist options
  • Select an option

  • Save ianjamieson/6e0e983839227fe57ac86ce8909d7cb9 to your computer and use it in GitHub Desktop.

Select an option

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)
-- Automatically remove the highlight, and recreate it when the window is moved
hs.window.filter.default:subscribe(hs.window.filter.windowMoved, function()
if highlight then
highlight:delete()
highlight = nil
end
highlightWindow()
end)
@thezetrax
Copy link

Just added this to redraw the highlight when the window is moved

-- Automatically remove the highlight, and recreate it when the window is moved
hs.window.filter.default:subscribe(hs.window.filter.windowMoved, function()
    if highlight then
        highlight:delete()
        highlight = nil
    end
    highlightWindow()
end)

@ianjamieson
Copy link
Author

Great thank you, I actually have that on my local config it's just not been updated here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment