Posted on

I bet you've been in a situation where you need to quickly mute or unmute your microphone on a video call. It's a common scenario for remote workers: you're in a long meeting, and suddenly a service or server goes down. You need to check it out and fix it fast, but you can't just leave the meeting. So, you mute yourself, quickly switch to the terminal, and start investigating the issue. A couple of minutes later, someone asks you a question, and now everyone is waiting for you to answer. This awkward silence settles in while you're trying to find the right window and the right button to unmute yourself and respond.

Most apps have a keyboard shortcut, but it only works inside the app. So in this scenario that I just described it wouldn't work.

I wish my Macbook had a built-in media key to toggle the microphone, just like my Thinkpad Nano does. But it doesn't. So I had to find a workaround...

The shell script

After some investigation I realized that I can control the microphone volume via Applescript with OSAScript (Open Scripting Architecture). So I wrote a simple shell script that alternates between 0% and 100% input volume, which in practice is the same as muting and unmuting the microphone.

#!/bin/sh

if [ $(osascript -e "input volume of (get volume settings)") -eq 0 ]; then
        osascript -e "set volume input volume 100"
        osascript -e "display notification \"You are now unmuted\" with title \"🎤 on\" sound name \"Funk\""
else
        osascript -e "set volume input volume 0"
        osascript -e "display notification \"You are now muted\" with title \"🎤 off\" sound name \"Funk\""
fi

I saved this file on my ~/.dotfiles/bin directory with the name toggle_mic and gave it permissions to execute:

chmod +x ~/.dotfiles/bin/toggle_mic

You can simply add this as a bash/zsh function instead if you just want to call it from the terminal. But I wanted to bind it to a keyboard key so saving it as an executable file was a much more flexible solution.

It would also be interesting to have a status bar icon indicating the current status of the microphone, but it's not possible to do without relying on a third party app. In my case, I have that status indicator via the app Krisp.ai that displays a red cross on top of it's menu bar icon when either sound input or output are muted.

The Krisp.ai menu bar icon
    indicating that the mic is disabled
Krisp.ai app menu bar icon showing that the microphone is muted.

In case you don't have Krisp.ai, you can use BitBar to place this info on the menu bar.

The keyboard shortcut

There are two options for creating a keyboard shortcut for the script we have just created.

  1. Using MacOS Automator (simplest approach)
  2. Using Karabiner-Elements app (only recommend if you already have it installed)

1. MacOS Automator

This option is the simplest and requires no extra software. Just open Automator, create a new Quick Action and make sure you have the following configuration:

Screenshot of Automator with
    a quick action configured to run a script
Creating an Automator Quick Action that uses Run Script

Since this action does not require any input, we just need to select the Run Script action from the Library and provide the script path so that Automator executes it.

Now you just need to create a Keyboard Shortcut that triggers the Automator Quick Action.

Go to System Settings > Keyboard > Shortcuts, select Services in the sidebar and find the name of your Automator Quick Action script, it usually appears under General. Now define a shortcut of your preference.

2. Karabiner-Elements

This solution uses the app Karabiner-Elements to map my Del key to run the above script. It's a bit overkill honestly and I don't recommend it unless you already use this app for something else.

To map a key to run a shell script, just add the following rule to your complex_modifications rules in your ~/.config/karabiner/karabiner.json config file:

{
  "description": "Toggle Mic",
  "manipulators": [
    {
      "from": {
        "key_code": "delete_forward"
      },
      "to": [
        {
          "shell_command": "~/bin/toggle_mic"
        }
      ],
      "type": "basic"
    }
  ]
}

Or if you prefer the UI:

The created shortcut on
    Karabiner Elements app
Creating a new shortcut to map the Del key to a shell command on Karabiner-Elements app.

The dedicated key

And now, the final touch, the keycap. I replaced the Del keycap with a Mic to make it stand out.

My Keychron K3 Max keyboard
    with a mic key replacing the original Del key
My Keychron K3 Max with a Mic keycap where once the Del key was.

This setup is working for me, even though there are some rough edges that could use some improvement:

  • Apps do not show that you're muted. The mute setting is at the OS level.
  • I have to rely on the Krisp.ai app to have a visual indicator of the mute status