Restore Mac windows after wake with a Hammerspoon script
1 December 2026 · Martín Acuña Lledó · Applies to macOS 13 Ventura through macOS 27
If you are comfortable with a bit of code, Hammerspoon is the free way to put windows back after sleep. It is a macOS automation tool scripted in Lua, and in Hacker News threads about windows moving after sleep, people mention writing their own Hammerspoon scripts for it. This page shows the pieces Hammerspoon gives you, a short script to start from, and the limits that its own documentation describes. I make a paid app that does this job, so I also say where a script is enough and where it is not.
The pieces you need
From Hammerspoon's documentation (the latest release on its GitHub is 1.1.1, from February 2026):
- hs.caffeinate.watcher watches "display and system sleep/wake/power events". Its events include
screensDidLock,screensDidSleep,systemWillSleep,screensDidUnlock,screensDidWakeandsystemDidWake. - hs.screen.watcher calls a function when the screen layout changes, for example when a monitor is attached or removed or a resolution changes.
- hs.window gives each window's
frame(),screen()andid(), andsetFrame(rect)sets a window's frame "in absolute coordinates". - hs.screen lists screens with
allScreens(), andgetUUID()gives each one an identifier. - hs.timer.doAfter runs a function after a delay, which you need because monitors come back a few seconds apart.
Hammerspoon needs the Accessibility permission to move other apps' windows, like any window tool.
A script to start from
The one rule that matters: save before the monitors disconnect, and restore after they are back. Locking the screen and system sleep both happen before the monitors turn off, so save there. Restore a few seconds after the unlock or wake. This goes in ~/.hammerspoon/init.lua:
local saved = {}
local function save()
saved = {}
for _, win in ipairs(hs.window.allWindows()) do
local id, scr = win:id(), win:screen()
if id and scr then
saved[id] = { frame = win:frame(), screen = scr:getUUID() }
end
end
end
local function restore()
local present = {}
for _, scr in ipairs(hs.screen.allScreens()) do
present[scr:getUUID()] = true
end
for _, win in ipairs(hs.window.allWindows()) do
local s = saved[win:id()]
if s and present[s.screen] then
win:setFrame(s.frame)
end
end
end
local w = hs.caffeinate.watcher
sleepWatcher = w.new(function(event)
if event == w.screensDidLock or event == w.systemWillSleep then
save()
elseif event == w.screensDidUnlock or event == w.systemDidWake then
hs.timer.doAfter(10, restore)
end
end)
sleepWatcher:start()
Treat it as a sketch: I wrote it from Hammerspoon's documentation and have not run this exact script on every setup. The watcher is kept in a global variable so Lua does not clean it up. Ten seconds is a starting point, not a measured value: raise it if a monitor wakes late. Reload Hammerspoon's config after editing.
Where it stops
- Only the current Space. Hammerspoon's documentation says
hs.window.allWindows()"can only return windows in the current Mission Control Space", and points tohs.window.filterfor other Spaces. Windows on other desktops are not saved by this script. - Absolute coordinates.
setFrameuses absolute coordinates. If macOS swapped the arrangement of your monitors, the saved frames point at the wrong place. Fix the arrangement first, for example with displayplacer: see fix swapped monitors with displayplacer. - Identical monitors. The script matches screens by
getUUID(). If macOS mixes up two identical monitors, the UUIDs can follow the wrong one. See two identical monitors swap places after sleep. - Displays that sleep without a lock. If the monitors turn off on their own timer while you are logged in and the screen is not locked, this script has saved nothing. Add your own save on another event if that is your case, and test it.
- Events that do not arrive. The hs.screen.watcher documentation warns that screen events during sleep may not be reported in some cases, for example with FileVault when the Mac resumes from hibernation.
- Saving at the wrong moment. If you add a timer that saves every minute, you will sooner or later save the pile macOS made. That trap is explained in why some window restore apps save the mess.
- Upkeep. It is your code. New windows, closed windows, apps that restart and get new window IDs, and different desks with different monitors are all yours to handle.
More scripted options, including Keyboard Maestro, are in every fix for windows moving after sleep.
How Many Displays handles it
Many Displays is the same idea, with the edge cases done. It saves as you work, within about 1.5 seconds of a window moving, and freezes saving the moment the Mac locks, sleeps, the displays sleep or a display reconfigures. It keeps one layout per combination of displays, puts back the arrangement, main display, resolution, rotation and HDR before the windows, and waits until the displays have been stable for a few seconds instead of a fixed delay. Windows on other desktops are remembered and restored without switching to them. After an app or the Mac restarts, it re-links windows by title.
Where a script may suit you better:
- It is free and yours. If you already use Hammerspoon and have one desk with different monitors, the script above may be all you need.
- You want custom rules. A script can do anything you can code, such as a different layout per time of day. Many Displays has no named layouts or rules.
And what neither can do: since macOS 14.5 no app or script can move another app's window to a different Space without disabling System Integrity Protection.
Download the free trial How it works
Questions
Can Hammerspoon restore my windows after the Mac wakes?
It can, with a script. hs.caffeinate.watcher tells you when the screens lock, sleep, unlock or wake, hs.window gives each window's frame and screen, and hs.window:setFrame moves it back. You write and maintain the script yourself.
When should the script save the layout?
Before the monitors disconnect, so on the screen lock or system sleep events, not on a timer and not after the wake. If you save after macOS has moved the windows, you save the mess.
Does it work with windows on other desktops?
Not with hs.window.allWindows. Its documentation says it only returns windows in the current Mission Control Space and points to hs.window.filter for other Spaces.
What version of Hammerspoon is current?
The latest release on Hammerspoon's GitHub is 1.1.1, from February 2026.