Mute Your Mic on Schedule with Python

For both Windows and Mac OS

So I found this script on staticacedamy written by Abdur Rahman and I was thinking…. well….. let’s make it better.

So, Automate muting/unmuting your mic at specific times with this simple script and say goodbye to awkward moments!

"""
Microphone Auto-Mute Script (Cross-Platform)
Author: Abdur Rahman (https://medium.com/@abdur-rahman)
Code found on: https://blog.stackademic.com/
Optimized by: Theo van der Sluijs / https://itheo.tech

This script automatically mutes the microphone every hour to enhance privacy.
The script is designed to work on both Windows and macOS platforms.

- For Windows, it uses `nircmd.exe` (make sure it’s in the system PATH).
- For macOS, it utilizes `osascript` to toggle the microphone.
- Includes error handling for unsupported platforms and operational issues.

**Note:** macOS requires appropriate permissions for microphone control.
"""

import os
import time
import platform

def mute_mic():
    """
    Mutes the microphone depending on the operating system.

    Returns:
        bool: True if the microphone was muted successfully, False otherwise.
    """
    system_platform = platform.system()

    try:
        if system_platform == "Windows":
            # Mute mic using nircmd for Windows
            os.system("nircmd.exe mutesysvolume 1")
            print("Muted mic on Windows.")
        elif system_platform == "Darwin":
            # Mute mic using osascript for macOS
            os.system("osascript -e 'set volume input volume 0'")
            print("Muted mic on macOS.")
        else:
            print("This platform is not supported.")
            return False
        return True
    except Exception as e:
        print(f"Error muting mic: {e}")
        return False

def toggle_mic_every_hour():
    """
    Continuously mutes the microphone every hour.

    Returns:
        None
    """
    while True:
        success = mute_mic()
        if success:
            print("Muted mic. Sleeping for 1 hour.")
        else:
            print("Failed to mute mic. Retrying in 1 hour.")
        time.sleep(3600)  # Wait for 1 hour before re-muting

# Start the mic toggle process
toggle_mic_every_hour()

Did you find this article valuable?

Support itheo.tech, I automate Sh!t by becoming a sponsor. Any amount is appreciated!