Skip to content

Instantly share code, notes, and snippets.

@henrik242
Last active December 20, 2024 19:42
Show Gist options
  • Save henrik242/1c5010734d6993123d276c3e4d07852f to your computer and use it in GitHub Desktop.
Save henrik242/1c5010734d6993123d276c3e4d07852f to your computer and use it in GitHub Desktop.
  1. Open Automator.app
  2. Create new Quick Action
  3. Select Run AppleScript
  4. Add this:
set inputVolume to input volume of (get volume settings)
if inputVolume = 0 then
	set inputVolume to 100
	display notification "Volume set to 100" with title "✅ Microphone is on"
else
	set inputVolume to 0
	display notification "Volume set to 0" with title "❌ Microphone is off"
end if
set volume input volume inputVolume
  1. Save as mute-microphone
  2. Go to System Preferences -> Keyboard -> Shortcuts -> Services -> General
  3. Find the mute-microphone service and set the hotkey (e.g. control-cmd-option M)
@henrik242
Copy link
Author

I've found https://redsweater.com/fastscripts/ to be better/faster than Automator for this task. More info at https://gist.github.com/henrik242/4e64e208a184f540a80a343187e075c1

@jacobilinden
Copy link

jacobilinden commented Nov 22, 2021

I needed to include without output muted to keep my speakers from also muting.

ie

if input volume of (get volume settings) = 0 then
	set volume input volume 100
	display notification "✅ unmuted" with title "Microphone"
else
	set volume input volume 0 without output muted
	display notification "❌ muted" with title "Microphone"
end if

Overall, works great. Thanks for the script.

@olivier3lanc
Copy link

Works great on MacOS Monterey, thank you ;)

@mohmdshbbr
Copy link

It doesn't work for me on Monterey. It just says ❌ muted. But the sliders of Input doesn't go down. Also, the microphone remains just as it was before. When trying to run the code again via the keyboard shortcut, it will does the same thing instead of acting as a toggle.

My Automator app does have the accessibility permission but still facing this issue. I have searched a lot. Can someone please assist me on this?

@liketheduck
Copy link

It doesn't work for me on Monterey. It just says ❌ muted. But the sliders of Input doesn't go down. Also, the microphone remains just as it was before. When trying to run the code again via the keyboard shortcut, it will does the same thing instead of acting as a toggle.

My Automator app does have the accessibility permission but still facing this issue. I have searched a lot. Can someone please assist me on this?

This article will walk you through the nitty gritty steps. Just substitute his code above instead of the link's (though it does the same thing).

https://medium.com/macoclock/how-in-the-bleep-do-i-mute-my-mic-anywhere-on-macos-d2fa1185b13

@blahster
Copy link

blahster commented May 3, 2022

It doesn't work for me on Monterey. It just says ❌ muted. But the sliders of Input doesn't go down. Also, the microphone remains just as it was before. When trying to run the code again via the keyboard shortcut, it will does the same thing instead of acting as a toggle.
My Automator app does have the accessibility permission but still facing this issue. I have searched a lot. Can someone please assist me on this?

This article will walk you through the nitty gritty steps. Just substitute his code above instead of the link's (though it does the same thing).

https://medium.com/macoclock/how-in-the-bleep-do-i-mute-my-mic-anywhere-on-macos-d2fa1185b13

Doesn't work for bluetooth devices since MacOS 12.2.
This used to be so useful before Monterey.

@sgandhi-astd
Copy link

Thank you so much. It works on Monterey, although sometimes the shortcut keys don't work until I reopen the Automator app.

@zxdposter
Copy link

not working in Monterey, same issue like @mohmdshbbr , @mohmdshbbr do you have way to solved it ?

@austinhamilton
Copy link

It works for some devices, but not all. And it seems to work for me with at least one bluetooth device, but not with two USB devices.

@ninadpchaudhari
Copy link

ninadpchaudhari commented Jul 7, 2022

For individuals who have an issue of this not working on Monterey,
The simple modification is to compare the inputVolume to be < 10

In Monterey, apparently, the mic volume cannot be set to 0 lol, After someone sets it to 0, the OS makes it as 4 for some unknown reason! Here is the modified version:

set inputVolume to input volume of (get volume settings)

log inputVolume

if inputVolume < 5 then
	set inputVolume to 100
	display notification "Volume set to 100" with title "✅ Microphone is on"
else
	set inputVolume to 0
	display notification "Volume set to 0" with title "❌ Microphone is off"
end if
set volume input volume inputVolume

P.S> THe script runs by itself, but even after enabling Automator in Security & Privacy as Accessibility, the keyboard shortcut does seem to trigger the same. Gotta solve this later

@cdrhim
Copy link

cdrhim commented Aug 1, 2022

@ninadpchaudhari's solution solves the problem. Works on my Monterey 12.4. Thanks!

@rdanilin
Copy link

rdanilin commented Aug 3, 2022

@ninadpchaudhari Did you fix the problem with the shortcut?
I'm on BigSur and shortcut does not work.

@bachmannworks
Copy link

that works kind of like a charm with stream deck and audio mute BUT not 100%. if i mute via stream deck I cannot unmute via hotkey

@liamwye
Copy link

liamwye commented Sep 19, 2022

Muting works for me, but notifications just don't pop up (they are in the notifcation centre though).

@taylorwaddell
Copy link

If you use focus modes/do not disturb, you'll have to add "Script Editor" to the allowed app's section under the focus mode settings.
Screen Shot 2022-10-07 at 1 50 19 PM

@ShanikaEdiriweera
Copy link

This only works when System Preferences is open. Any help?

@IonelLupu
Copy link

What about MacOS 13.0.1 (Ventura)?

@gr8pathik
Copy link

Please give the solution for MacOS Ventura

@IonelLupu
Copy link

This is what I use for MacOS Ventura:

on getMicrophoneVolume()
	input volume of (get volume settings)
end getMicrophoneVolume
on disableMicrophone()
	set volume input volume 0
	display notification "❌" with title "Microphone is off"
end disableMicrophone
on enableMicrophone()
	set volume input volume 100
	display notification "✅" with title "Microphone is on"
end enableMicrophone

if getMicrophoneVolume() is greater than 0 then
	disableMicrophone()
else
	enableMicrophone()
end if

@gr8pathik
Copy link

But I cannot find this mute-microphone in System Preferences -> Keyboard -> Shortcuts -> Services -> General in macOs Ventura

@IonelLupu
Copy link

I had the same issue as you. System Preferences doesn't update the list after you save the script. After saving it, you have to close System Preferences and open it again.

@kylechadha
Copy link

kylechadha commented Mar 24, 2023

Works for me consistently on 13.2.1, with a bit of lag. Thanks for a great solution!

@IonelLupu
Copy link

I also have some lag. @kylechadha

yeah. MacOS sucks 😂 😭

@reme3
Copy link

reme3 commented Apr 19, 2023

How to always see microphone status indicator (muted/on)? In dock/bar near clock, battery, wifi status. @kylechadha

@tsinflam2
Copy link

Perfect!

@arstagaev
Copy link

set inputVolume to input volume of (get volume settings)

if inputVolume = 0 then
	set inputVolume to 100
	display notification "Volume set to 100" with title "✅ Microphone is on"
	delay 2
else
	set inputVolume to 0
	display notification "Volume set to 0" with title "❌ Microphone is off"
	delay 2
end if
set volume input volume inputVolume
delay 5

Also u can save as app this script, and launch him with shortcut

@JonathanWheeler
Copy link

JonathanWheeler commented Feb 23, 2024

At first, I was frustrated that @henrik242's script didn't work for me. I suspect that many of you may be running into the same issue I had.

The problem for me was that my Shure Digital X2U microphone input had no way of digitally muting it:
Screenshot 2024-02-23 at 3 25 10 AM copy

My solution was to create a Loopback device using the aptly named app called Loopback with the following simple configuration:
Screenshot 2024-02-23 at 3 25 56 AM copy

I now have a device that I can select as my input that has an "input volume" slider:
Screenshot 2024-02-23 at 3 25 14 AM copy

Problem solved! Now @henrik242's unmodified AppleScript works flawlessly on macOS Sonoma 14.3.1.

I got the script itself to run instantly using a keyboard shortcut in FastScripts.


Additional notes:

Automator: I too have noticed in the past that using Automator is slow. Don't bother, unless you need something free.

BetterTouchTool: This works in BetterTouchTool as well, but the "display notification..." doesn't work; you'll have to use the built-in one, but that one can't alternate between Muted/Unmuted notifications.

So, ideally just use FastScripts for this.

I also slightly tweaked @henrik242's script as follows, incorporating some of the flourishes of others:

set inputVolume to input volume of (get volume settings)

if inputVolume < 50 then
	set volume input volume 100
	display notification "✅ Unmuted" with title "Microphone 🎤"
else
	set volume input volume 0
	display notification "❌ Muted" with title "Microphone 🎤"
end if

You can link others to this solution here.

@zaibakker
Copy link

Thanks a lot. works great for sonoma!
if you can add or explain how to add an icon in status menu item (i'm not sure about the name, near the clock). It will be perfect. I don't find yet.

mac newbie.

@henrique-arthur
Copy link

Thanks, it works on sequoia 15.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment