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):

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

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:

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.