Skip to content

Instantly share code, notes, and snippets.

@khromalabs
Last active March 12, 2023 10:20
Show Gist options
  • Save khromalabs/70a21206c8864ce41cc645078a7f322a to your computer and use it in GitHub Desktop.
Save khromalabs/70a21206c8864ce41cc645078a7f322a to your computer and use it in GitHub Desktop.
Pulseaudio quick audio output / sink switcher script, toggles when two outputs available, shows zenity list when more outputs available
#!/bin/bash
# Pulseaudio sink switcher
# Required commands: notify-send, zenity
SINK_LIST=$(pacmd list-sinks | grep -E "(index:|device.description)" | xargs -n2 -d'\n')
COUNT=$(echo "$SINK_LIST" | wc -l)
NEW_SINK=""
if [ $COUNT -eq 0 ]; then
notify-send "No audio sinks available"
elif [ $COUNT -eq 1 ]; then
notify-send "There's just one audio sink available"
elif [ $COUNT -eq 2 ]; then
# Toggle current sink
NEW_SINK_LINE=$(echo "$SINK_LIST" | grep -v "* index" | head -1)
notify-send "Switching audio sink to \"$(echo "$NEW_SINK_LINE" | cut -d \" -f2)\""
NEW_SINK=$(echo "$NEW_SINK_LINE" | awk '{print $2}')
elif [ $COUNT -gt 2 ]; then
# Show zenity menu
CURRENT_SINK_LABEL=$(echo "$SINK_LIST" | grep "* index" | cut -d \" -f2)
NEW_SINK=$(echo "$SINK_LIST" | grep -v "* index" | sed -r 's/.*([0-9]+).*\"([^"]+)\"/\1\n\2/g' | zenity --list --title="Choose the new audio sink" --text="Currently used audio sink: \"$CURRENT_SINK_LABEL\"" --column="i" --column="Available Sinks" --hide-column=1 --width=400 --height=200)
fi
[ -n "$NEW_SINK" -a $NEW_SINK -eq $NEW_SINK ] && pacmd set-default-sink $NEW_SINK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment