Saturday, March 11, 2023


Tags

Sleep Aid Icon

Extending Sleep Aid's functionality using Scripts

Take advantage of the macOS to gain back more control of your Mac's Sleeping habits.

Saturday, March 11, 2023 - Sam Rowlands



In this screenshot, you see my Mac spent 1/4th of my long weekend checking mail for me. I don't want my Mac to wasting electricity checking for mail. However I do like the idea that my backups are upto date, so I don't disable "Wake for Maintenance".

The solution is to quit, or "suspend" Apple Mail. Thanks to the shell script feature of Sleep Aid 1.2, I can easily add that.

If you don't know how to create shell scripts, Apple have a Introduction to shell scripts https://support.apple.com/guide/terminal/intro-to-shell-scripts-apd53500956-7c5b-496b-a362-2845f2aab4bc/mac.

I created two scripts, one to run when the Mac goes to sleep and one to run when it wakes up.

Sleep Script

#!/bin/bash kill -STOP $(pgrep -f 'Mail')

Save this snippet as a shell script (make it executable, as indicated in the above tutorial), then set the saved file as the "Sleep Script" in the "Sleep Settings" window of Sleep Aid.

The command line function "kill" with the "-STOP" parameter, will suspend "Mail". It's still loaded, but can't do anything. Kill requires a PID (Process Identifier), so we use the command "pggrep" to find Mail's PID.

WARNING: Any process you "-STOP" must always have a matching "-CONT" on wake, otherwise you're going to have unresponsive apps. Do NOT use this on Sleep Aid, otherwise it will NOT be able to run the Wake Script.

Wake Script

#!/bin/bash kill -CONT $(pgrep -f 'Mail')

This is the script that Sleep Aid will run when the Mac wakes up. Again, we save this as a shell script (make it executable) and add the file to the "Wake Script" setting of the "Sleep Settings" window in Sleep Aid.

Like before, this script uses the "kill" function, but instead of suspending Apple Mail, we're now resuming it.

Does it work?

If you look at the first screenshot, you can see my 4 day weekend has lots of red in it. After setting these scripts, the following 3 days look much better, until I disabled the scripts yesterday afternoon and the red came back.

What about the other blocks of red on Wednesday and Thursday morning?

Those are "Adaptive Standby Wakes", where the Mac automatically wakes up. Supposedly in preparation for me to use it. Apple says there is no way to disable this, but I think there is, I just haven't found it yet :).