Skip to content

Instantly share code, notes, and snippets.

@literallylara
Last active June 26, 2025 11:54
Show Gist options
  • Save literallylara/8f9b0fadb6a9d285bb474865279f0fa3 to your computer and use it in GitHub Desktop.
Save literallylara/8f9b0fadb6a9d285bb474865279f0fa3 to your computer and use it in GitHub Desktop.
Like `wpctl set-default` but accepts a case-insensitive sink-name instead of an id
#!/bin/sh
query="$1"
debug="$2"
if [ -z "$query" ]; then
echo "Usage: $(basename "$0") <query> [debug]"
exit 1
fi
COL_GREEN='\e[0;32m'
COL_YELLOW='\e[1;33m'
COL_GRAY='\e[1;30m'
COL_RESET='\e[0m'
COL_CURRENT=$COL_RESET
log() {
if [ "$debug" != "debug" ]; then
return
fi
printf '%b%s\n' "$COL_CURRENT" "$*" >&2
printf '%b' "$COL_RESET" >&2
}
findSinkIdByName() {
inAudioSection=false
inSinkSection=false
wpctl status | while read -r line; do
if echo "$line" | grep -Eq '^Audio'; then
inAudioSection=true
COL_CURRENT=$COL_YELLOW
log "$line"
continue
fi
if echo "$line" | grep -q ' Sinks:'; then
inSinkSection=true
COL_CURRENT=$COL_YELLOW
log "$line"
continue
fi
if ! $inAudioSection || ! $inSinkSection; then
COL_CURRENT=$COL_GRAY
log "$line"
continue
fi
id=$(echo "$line" | grep -E '[0-9]+\. ' | sed -E 's/[^0-9]*([0-9]+)\..*/\1/' | xargs)
if [ -z "$id" ]; then
COL_CURRENT=$COL_GRAY
log "$line"
break
fi
name=$(echo "$line" | sed -E 's/[^.]*\.([^[]+).*/\1/' | xargs)
if echo "$name" | grep -iq "$query"; then
COL_CURRENT=$COL_GREEN
log "$line"
log ''
echo "$id:$name"
break
fi
COL_CURRENT=$COL_GRAY
log "$line"
done
}
sink=$(findSinkIdByName "$query")
sinkId=$(echo "$sink" | cut -d ":" -f1)
sinkName=$(echo "$sink" | cut -d ":" -f2)
if [ -z "$sinkId" ]; then
echo "No sink found with name: $query" >&2
exit 1
fi
echo "Setting default sink to: $sinkId ($sinkName)"
wpctl set-default "$sinkId"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment