Skip to main content

Stop Your Mac Going to Sleep at the Wrong Moment

A how-to by Phil Hardings Shovel


Right. So you've got a Mac. You step away for ten minutes — maybe you're on a call, maybe you're making a brew, maybe you're just staring out the window having a think — and you come back to find the screen has locked, your music has stopped, or something you were waiting on has paused because the machine decided you weren't there anymore.

This is fixable. And you can fix it properly, without buying anything, without installing some sketchy app from a website that looks like it was designed in 2003, and without leaving your machine permanently awake like a paranoid insomniac.

What we're going to do is use Hammerspoon — a free, open-source Mac automation tool — to build a mouse jiggler that moves the cursor by a few pixels every minute or so, tricks macOS into thinking you're still there, then moves it back. You won't notice it. The machine won't lock. Job done.

What You'll Need

  • A Mac running macOS (Apple Silicon or Intel — doesn't matter)
  • Hammerspoon installed — it's free, open source, and has been around for years
  • About fifteen minutes

Why Hammerspoon and Not Just a Paid App?

There are paid apps that do this. Some of them are fine. But Hammerspoon is a proper automation tool that lets you write small Lua scripts to control your Mac — window management, keyboard shortcuts, scheduled tasks, all of it. The jiggler is just one small thing you can do with it. Once it's installed you'll find other uses for it.

Also it's free and the source code is on GitHub. You can read exactly what it does. That matters.

Guide Structure

SectionWhat it covers
InstallationInstalling Hammerspoon and getting it running
The JigglerWriting the mouse jiggler script
The Menubar WidgetAdding a menubar icon to control it
How It All WorksWhat the code is actually doing, if you're curious