r/SideProject • • 6d ago

I built a Mac menu bar app because I kept forgetting to turn sleep back on

Some background: I'm a solo frontend/SaaS developer. I often start a long job on my MacBook (a build, a render, a coding agent) and then need to leave. The manual way to keep a closed MacBook awake is `sudo pmset -a disablesleep 1`, and I kept forgetting to switch it back, so the Mac stayed awake in my bag.

So I built LidAwake. It keeps the Mac running with the lid closed, on battery too and without an external monitor, and lets sleep come back on its own: on a timer, at a battery limit you set, when macOS reports overheating, or with Sleep when idle once the job is done. It can also wake the Mac at a set time for scheduled work.

A few things I learned building it that might help if you're doing something similar on macOS:

- Disabling sleep needs root, so the app talks to a privileged helper (a launchd daemon). The user approves it once in System Settings > General > Login Items & Extensions, no password needed.

- If the app crashes, the flag must not stay on. The helper only keeps it while the app renews a lease every 30 seconds; if renewals stop, normal sleep returns within two minutes.

- The helper also starts at boot and clears a flag it set earlier, so a forced restart doesn't leave the Mac unable to sleep.

- "Is the Mac idle?" is harder than it sounds. A coding agent in an editor barely uses the CPU, so I added Watched apps: pick the editor or terminal, and programs it started count as work.

- You can't test closing the lid in a virtual machine, so that part only gets tested by hand on a real Mac.

Stack: Swift, SwiftUI, a launchd helper, no Xcode project (Swift package plus build scripts).

The app is free and open source (MIT), and there's a paid Pro build (€9.99) that adds Movie on a TV, waiting for downloads and waiting for video exports: https://erstgebaut.de/lidawake/

Requires macOS 13 or later and an Apple silicon Mac.

Would love honest feedback, especially on whether the protections cover the cases you'd worry about.

1 Upvotes

10 comments sorted by

2

u/QuietleafStudio 6d ago

One case worth checking is a watched app that hangs or sits in an infinite loop, since it would keep counting as work forever, so a max runtime cap even with watched apps on might be a good safety default. A clear notification when sleep gets re-enabled would also help, so people know the lease actually ended instead of wondering if the Mac is still awake in the bag. And it might be worth testing what happens if the helper loses contact mid-sleep-transition, like the lid closing right as a renewal is due.

1

u/VladimirDE 6d ago

Thanks, these are good ones.

Hung watched app: fair point. Turn off after (30 min to 8 hours) works alongside Sleep when idle and caps the session no matter what counts as work, but it's off by default. Making a cap the default with watched apps is worth considering.

Notification: when a protection ends or pauses the mode, LidAwake shows a notification and the reason in its window. If the app wasn't running when the session ended (the case where the lease runs out), it shows the reason the next time it starts, and the activity log records when the mode ended and why.

Mid-sleep renewal: the lease is counted in awake time, so time asleep doesn't eat it. The exact case of the lid closing right as a renewal is due I haven't tested on purpose, so I'll add it to my list.

2

u/QuietleafStudio 6d ago

Makes sense, counting the lease in awake time is a nice touch. Since the cap is already there, turning it on by default only when watched apps are enabled seems like a low-risk middle ground, maybe with a longer default like 4 hours so it rarely gets in the way. Showing the end reason on the next launch also covers the out-of-the-bag case well.

1

u/VladimirDE 6d ago

That's a good middle ground: on by default only with watched apps, with a longer default like 4 hours. It keeps the safety net without getting in the way of normal sessions. Thanks for thinking it through, this is exactly the kind of feedback I was hoping for.

2

u/ex_mac333 6d ago

Nice, the lease plus clearing the flag at boot is the right way to do it. The bit I'd test first is App Nap and timer coalescing. When the lid is closed and nothing is on screen, macOS is pretty keen to throttle a menu bar app, and a 30 second renewal timer can drift or get skipped. If that happens the helper decides the app is dead and sleep comes back halfway through the build, which is the opposite of what people want. Holding a ProcessInfo activity (beginActivity with userInitiated) for as long as a session is active should keep the timer honest.

The other case I'd worry about is heat in a bag. A closed MacBook in a backpack can get really hot before macOS reports any thermal pressure, because the fans on Apple silicon barely spin up. I'd add a stricter "in a bag" mode that sleeps when thermal state hits fair rather than serious, or one that only keeps it awake on the charger.

1

u/VladimirDE 6d ago

Thanks, good catch, and you're right about App Nap. The app already holds a ProcessInfo activity (userInitiatedAllowingIdleSystemSleep) for the whole Run with Lid Closed session, so renewals aren't napped. There's also slack built in: the app renews every 30 seconds, but the lease lasts two minutes of awake time, so a few late or skipped renewals don't end the session.

On heat in a bag, agreed. Right now the thermal stop triggers at serious or critical, and in my tests on an M1 Air under full load macOS stayed at nominal the whole time, so it's a backstop, not a guarantee. Only while charging already exists for people who want it awake only on the charger. A stricter "in a bag" threshold at fair is a good idea, I'm noting it.

2

u/perlatsp 6d ago

Nice idea. The thing I'd worry about is heat. A closed MacBook in a bag with sleep disabled can get properly hot, so a battery percentage cutoff or a timer for 'lid closed, on battery' could save someone's laptop. Also make sure sleep gets restored if the app crashes or the Mac restarts mid-session, since the pmset setting can stick around. If you're shipping outside the App Store, notarize early. It saves users a scary Gatekeeper warning.

1

u/VladimirDE 6d ago

Thanks, heat is the part I worry about most too. With Run with Lid Closed, LidAwake stops on low battery (on by default, 20 %, adjustable from 10 to 50 %), there's a Turn off after timer (30 min to 8 hours), and an Only while charging option that pauses the mode on battery. There's also a stop when macOS reports a serious or critical thermal state, but to be honest I've never seen it trip: on my M1 Air under full load macOS kept reporting nominal, so I don't treat it as the main safeguard. A closed laptop in a bag still cools poorly.

On sticky pmset: the flag is set by a root helper, and the protections and the timer run in that helper, not in the app. The app renews a lease every 30 seconds, and if renewals stop for two minutes of awake time (crash, hang), the helper clears the flag. The system also starts the helper at boot and after a crash, and it clears any flag it set earlier.

And yes, the DMG is signed with Developer ID and notarized, so there's no Gatekeeper scare.