Created
December 8, 2025 20:09
-
-
Save amolk/a25c3c79872a725820feaac1d30fa216 to your computer and use it in GitHub Desktop.
hammerspoon script for Focus Follows Mouse behavior on macos (install hammerspoon, Open Config from menu)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Focus follows mouse | |
| local lastWinElement = nil | |
| local function focusCheck() | |
| local ok, err = pcall(function() | |
| local mousePos = hs.mouse.absolutePosition() | |
| local systemWide = hs.axuielement.systemWideElement() | |
| local elementAtPoint = systemWide:elementAtPosition(mousePos) | |
| if elementAtPoint then | |
| local element = elementAtPoint | |
| local windowElement = nil | |
| while element do | |
| local role = element:attributeValue("AXRole") | |
| if role == "AXWindow" then | |
| windowElement = element | |
| break | |
| end | |
| element = element:attributeValue("AXParent") | |
| end | |
| if windowElement then | |
| local winTitle = windowElement:attributeValue("AXTitle") or "" | |
| local pid = windowElement:pid() | |
| local winKey = tostring(pid) .. ":" .. winTitle | |
| if winKey ~= lastWinElement then | |
| lastWinElement = winKey | |
| local app = hs.application.applicationForPID(pid) | |
| if app then | |
| windowElement:performAction("AXRaise") | |
| app:activate(false) | |
| -- print("Focused: " .. app:name() .. " - " .. winTitle) | |
| end | |
| end | |
| end | |
| end | |
| end) | |
| -- if not ok then | |
| -- print("Error: " .. tostring(err)) | |
| -- end | |
| -- Always schedule next check | |
| hs.timer.doAfter(0.1, focusCheck) | |
| end | |
| -- Start the chain | |
| focusCheck() | |
| hs.alert.show("Focus follows mouse enabled") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment