select-sound-output.sh 1.1 KB

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env zsh
  2. local info="$(pactl list sinks)"
  3. local default_name="$(pactl info | grep -Po '(?<=Default Sink: ).+')"
  4. local ids=(${(@f)"$(echo "${info}" | grep -Po '(?<=Sink #).+')"})
  5. local names=(${(@f)"$(echo "${info}" | grep -Po '(?<=Name: ).+')"})
  6. local descs=(${(@f)"$(echo "${info}" | grep -Po '(?<=Description: ).+')"})
  7. local default_id=""
  8. local query_string=""
  9. for ((i = 1; i <= "${#ids}"; ++i)); do
  10. if [[ "${names[${i}]}" == "${default_name}" ]]; then
  11. default_id="${ids[${i}]}"
  12. fi
  13. query_string+="${ids[${i}]}: ${descs[${i}]} (${names[${i}]})\n"
  14. done
  15. [[ -v WAYLAND_DISPLAY ]] \
  16. && prompt_cmd=("fuzzel" "-dp" "${default_id}> ") \
  17. || prompt_cmd=("dmenu" "-p" "${default_id}:")
  18. local choice
  19. choice="$(echo "${query_string%"\n"}" | ${prompt_cmd})"
  20. if (( "${?}" != 0 )); then
  21. exit
  22. fi
  23. local selected_id="$(echo "${choice}" | grep -Eo '^[0-9]+')"
  24. pactl set-default-sink "${selected_id}"
  25. local selected_name="$(echo "${choice}" \
  26. | cut -c "$(("${#selected_id}" + 3))-" \
  27. | grep -Po '^.+(?= \()')"
  28. notify-send 'Output Device' "${selected_name} (${selected_id})"