Skip to content

Instantly share code, notes, and snippets.

@xthesaintx
Last active January 29, 2022 02:36
Show Gist options
  • Save xthesaintx/21773e594c9769cfeab348b158654f83 to your computer and use it in GitHub Desktop.
Save xthesaintx/21773e594c9769cfeab348b158654f83 to your computer and use it in GitHub Desktop.
Python Script that switches through audio output on Mac, can add uid to an ignore list, pops up a notification with output name. USE: I bind the script to a shortcut and add a keyboard shortcut to switch between audio outputs. (Uses https://github.com/deweller/switchaudio-osx)
#!/usr/bin/env python3
import os
import subprocess as sp
import json
def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
## PATH TO SwitchAudioSource
switch = "/opt/homebrew/opt/switchaudio-osx/SwitchAudioSource"
## IGNORE LIST
ignore_list =["BuiltInSpeakerDevice"]
## LOAD DEVICES INTO LIST
devices =[]
device_json = sp.getoutput(switch+' -a -f json')
device_json_list = device_json.split("\n")
for i in device_json_list:
json_output = json.loads(i)
if str(json_output["uid"]) not in ignore_list:
devices.append(json_output["uid"])
##Current
current_device_raw = sp.getoutput(switch+' -c -f json')
current_device_json = json.loads(current_device_raw)
current_uid = current_device_json["uid"]
## CHECK TO SEE IF THE UID IS IN THE ALLOWED LIST
if current_uid in devices:
device_index = devices.index(current_uid)
if device_index == len(devices)-1:
device_index = 0
else:
device_index +=1
elif current_uid not in devices:
device_index = 0
## SETS THE OUTPUT
output = sp.getoutput(switch+' -u "'+devices[device_index]+'"')
# POPS UP THE NOTIFATION
output_text = sp.getoutput(switch+' -c')
notify("Audio Output", output_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment