Advanced SleepWatcher for macOS: Go Beyond Just Muting
In a previous post, I covered how to use SleepWatcher to automatically mute your Mac when it goes to sleep. But SleepWatcher can do far more than just volume control—it can trigger any script or command on sleep, wake, or even idle. In this post, we’ll explore a few advanced use cases and unlock even more automation potential for macOS.
⚙️ What Is SleepWatcher (Quick Recap)
SleepWatcher is a lightweight daemon that listens for system sleep, wake, and idle events. It executes user-defined scripts at each of those lifecycle points.
With a bit of shell scripting, it becomes a powerful automation tool for macOS power users.
🔍 Use Cases for Advanced SleepWatcher
Here are a few real-world scenarios where SleepWatcher becomes an essential part of your Mac automation toolkit:
1. 🧹 Clear Workspace on Sleep
Want to make sure you’re leaving no loose ends before walking away?
Add this to your ~/.sleep
script:
#!/bin/bash
# Clear Desktop clutter to a staging folder
mv ~/Desktop/* ~/Documents/Desktop_Staging/ 2>/dev/null
# Mute audio
osascript -e "set volume with output muted"
2. 🔒 Auto-Lock & Turn Off Display on Wake
Add security and reduce energy use by automatically locking and dimming the display right after waking.
In your ~/.wakeup
script:
#!/bin/bash
# Unmute volume
osascript -e "set volume without output muted"
# Lock the screen immediately
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
💡 Pro Tip: This behaves like hitting Control + Command + Q to lock the screen.
3. ☁️ Sync Local Files with Cloud
Run a backup job to Dropbox, rsync, or an external drive when your system idles for a certain amount of time.
Add this to a new file: ~/.idle
#!/bin/bash
# Sync important files to external drive
rsync -avh --delete ~/Projects/ /Volumes/BackupDrive/Projects/
✅ You'll also need to configure SleepWatcher to monitor idle states, which we'll cover below.
🧠 Configuring Idle Scripts with ~/.sleepwatcherrc
To use an idle script, you’ll need to set up a .sleepwatcherrc
config file that SleepWatcher reads on launch.
Create or edit the file:
nano ~/.sleepwatcherrc
Add the following line to trigger the ~/.idle
script after 10 minutes of system idle time:
-idle ~/.idle 600
Make it executable:
chmod +x ~/.idle
And restart the SleepWatcher service:
brew services restart sleepwatcher
🧪 Debugging & Logging Output
You can direct the output of your scripts to a log file for debugging:
#!/bin/bash
echo "$(date): Wake event triggered" >> ~/sleepwatcher.log
This is incredibly useful if you’re running into unexpected behavior or want audit trails for automation.
📌 Wrap-Up
SleepWatcher is more than a mute-on-sleep tool—it’s a simple but powerful automation layer for macOS. From cleanup scripts to cloud syncs and lock-on-wake security, it opens up workflows that just work in the background.
If you’re serious about building a Mac that works for you, SleepWatcher is a must-have in your toolbox.