Write a Hammerspoon Lua script to reposition and resize the frontmost window on macOS

domain: hammerspoon.org · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Install Hammerspoon and create/edit ~/.hammerspoon/init.lua, the script Hammerspoon loads on launch and reload.
  2. Get the frontmost window object with hs.window.focusedWindow().
  3. Move and resize it with :moveToUnit() using a unit rect table ({x, y, w, h} as fractions from 0.0-1.0 of the screen), e.g. hs.layout.left50 for the left half, so positioning works across different display resolutions.
  4. Bind the action to a hotkey with hs.hotkey.bind(mods, key, fn) so the window layout script can be triggered on demand.
  5. Reload the Hammerspoon config after edits (via the menu bar item or hs.reload()) to pick up changes without restarting the app.
  6. For multi-Space window movement, note that hs.spaces relies on private APIs and Accessibility-based workarounds and is explicitly documented as experimental, so test it carefully before depending on it.

Known gotchas

Related routes

Filter and lay out application windows automatically on macOS using Hammerspoon's window filter and grid modules
hammerspoon.org · 5 steps · unrated
Trigger a Hammerspoon Lua function from an external script or another app via the hs CLI and URL scheme
hammerspoon.org · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans