Skip to content

Instantly share code, notes, and snippets.

@szhu
Last active January 19, 2026 20:39
Show Gist options
  • Select an option

  • Save szhu/254b634be6a57473e20f048b3849acf8 to your computer and use it in GitHub Desktop.

Select an option

Save szhu/254b634be6a57473e20f048b3849acf8 to your computer and use it in GitHub Desktop.
-- ~/.hammerspoon/init.lua
---@diagnostic disable-next-line: undefined-global
local hs = hs
hs.window.animationDuration = 0
-- https://github.com/Hammerspoon/hammerspoon/issues/3224#issuecomment-1294971600
local function axHotfix(win, fn)
local axApp = hs.axuielement.applicationElement(win:application())
local wasEnhanced = axApp.AXEnhancedUserInterface
if wasEnhanced then
axApp.AXEnhancedUserInterface = false
end
fn()
if wasEnhanced then
axApp.AXEnhancedUserInterface = true
end
end
-- CenterOnScreen: Center new standard windows on the screen.
-- https://chatgpt.com/share/679c6282-322c-8007-9421-d7a0513d1a5f
hs.window.filter.default:subscribe(hs.window.filter.windowCreated, function(win)
print("[CenterOnScreen] Window created. App: " .. (win:application():bundleID() or "unknown"))
-- if win:application():bundleID() == "com.microsoft.VSCode" then return end
if win:application():bundleID() == "org.hammerspoon.Hammerspoon" then return end
if not win:isStandard() then return end
if not win:isMaximizable() then return end
if win:tabCount() ~= nil and win:tabCount() > 1 then return end
if not win:application():isFrontmost() then return end
-- Skip centering if window is aligned to an edge.
local winFrame = win:frame() -- excludes menu bar and dock areas!
local screenFrame = win:screen():frame()
local aligned = {
left = (winFrame.x == screenFrame.x),
right = (winFrame.x + winFrame.w == screenFrame.x + screenFrame.w),
top = (winFrame.y == screenFrame.y),
bottom = (winFrame.y + winFrame.h == screenFrame.y + screenFrame.h)
}
if aligned.left or aligned.right or aligned.top or aligned.bottom then
print("[CenterOnScreen] Window is edge-aligned, skipping centering")
return
end
axHotfix(win, function()
win:centerOnScreen(nil, true)
end)
end)
-- QuitFinder: Quit Finder when it's backgrounded with no standard windows.
QuitFinderTimer = hs.timer.new(5, function()
local Finder = (hs.application.applicationsForBundleID("com.apple.finder"))[1]
if not Finder then return end
-- Count only standard windows, ignoring the Desktop window.
local windowCount = 0
for _, window in ipairs(Finder:allWindows()) do
if window:isStandard() then
windowCount = windowCount + 1
end
end
print("[QuitFinder] Finder has " .. windowCount .. " standard windows and is " ..
(Finder:isFrontmost() and "frontmost" or "backgrounded") .. ".")
if windowCount == 0 and not Finder:isFrontmost() then
print("[QuitFinder] Finder is backgrounded with no windows, quitting.")
Finder:kill()
end
end)
-- QuitFinderTimer:start() -- https://github.com/Hammerspoon/hammerspoon/issues/1942#issuecomment-430450705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment