Automatically Mute Your MacBook When It Goes to Sleep

Since the COVID-19 pandemic, I've been working remotely full-time. One unexpected side effect of this shift is that my work laptop, which used to quietly rest in my backpack after hours, now sits permanently open on my desk.

This setup introduces a minor—but annoying—problem: every incoming alert chimes through the MacBook speakers when it's not muted. Text message? BING! Slack alert? BING! Email? You guessed it—BING!

If I remember to mute the volume before putting the Mac to sleep, all is well. But who wants to rely on muscle memory for something so trivial? So let's automate it.

This post walks you through setting up a simple and reliable way to mute your Mac when it sleeps, and unmute it when it wakes—automatically.


🔧 Prerequisites

To follow along, you’ll need:

  • macOS 10.15+ (Tested on Ventura 15.6)
  • Terminal access and basic command-line familiarity
  • Homebrew installed

🛠️ Step 1: Install SleepWatcher

SleepWatcher is a lightweight daemon that listens for sleep and wake events on macOS. To install it, open your terminal and run:

brew install sleepwatcher

If you're on an older version of macOS or Homebrew, you may also need to explicitly start the service:

brew services start sleepwatcher

📄 Step 2: Create Sleep and Wake Scripts

Next, create the two scripts that will run on sleep and wake:

touch ~/.sleep
touch ~/.wakeup

Give both scripts executable permissions:

chmod a+x ~/.sleep
chmod a+x ~/.wakeup

💬 Step 3: Writing the Scripts Using osascript

macOS ships with a command-line utility called osascript (reference), which allows you to control various system features via AppleScript. We’ll use it to manage volume settings (reference).

Mute Command

To mute the system volume:

osascript -e "set volume with output muted"

Unmute Command

To unmute:

osascript -e "set volume without output muted"

Check Mute Status (Optional)

To check if the volume is currently muted (returns true or false):

osascript -e "output muted of (get volume settings)"

✍️ Step 4: Add Commands to Scripts

.sleep File

Edit the .sleep script:

nano ~/.sleep

Insert the following:

#!/bin/bash
osascript -e "set volume with output muted"

Press Ctrl + X, then follow the prompts to save and exit.

.wakeup File

Edit the .wakeup script:

nano ~/.wakeup

Insert the following:

#!/bin/bash
osascript -e "set volume without output muted"

Save and exit with Ctrl + X.


✅ Testing & Verification

Confirm osascript Support

Try running this command:

osascript -e "set volume with output muted"

If the volume mutes, you're good to go. If not, your macOS version may not support this command.

Check SleepWatcher Installation

sleepwatcher -v

You should see something like:

sleepwatcher 2.2.1
Copyright (c) 2002-2019 Bernhard Baehr
This is free software that comes with ABSOLUTELY NO WARRANTY.

Check SleepWatcher Service Status

brew services info sleepwatcher

You’ll see output confirming its current status. Look for:

sleepwatcher (homebrew.mxcl.sleepwatcher)
Running: ✔
Loaded: ✔

Final Test: Sleep Behavior

To confirm the automation works:

  1. Temporarily rename .wakeup or comment out its contents.
  2. Put your Mac to sleep.
  3. Wake it up and check the volume—if muted, the .sleep script ran successfully!

🎉 Conclusion

And that's it! You’ve now automated your MacBook to mute itself when it sleeps and unmute on wake—no more surprise notification sounds after hours.

Hope this saves you from a few late-night *BING!*s.