#/bin/bash # # author: arian # date: 2024-05-06 # # purpose: # use this script on a keyboard shortcut to automatically # change your output device to the next one using 'pactl' # # usage: # ./pactl-next-output-dev.sh /path/to/log # #region run in debug if $DEBUG [ "$DEBUG" = 'true' ] && set -x #endregion #region globals CYAN='\e[0;36m' PURPLE='\e[0;35m' NORMAL='\e[0m' LOGFILE=${1:-'/var/log/arian/pactl-next-output-dev.sh.log'} LOGDIR="$(dirname "${LOGFILE}")" #endregion #region functions function log() { # logs strings to both the terminal and a file # usage: log [1-3] 'String' # 1 => INFORMATION # 2 => ERROR # 3 => DEBUG case $1 in 1) msg_type="INFORMATION" ;; 2) msg_type="ERROR" ;; 3) msg_type="DEBUG" ;; *) echo "Wrong input; exiting" exit 1 esac msg="[${PURPLE}$(date -Ins)${NORMAL}] [${CYAN}${msg_type}${NORMAL}]: $2" # tee command from https://unix.stackexchange.com/questions/694671/leave-color-in-stdout-but-remove-from-tee # (omits coloring in logfile) echo -e "${msg}" | tee >(sed $'s/\033[[][^A-Za-z]*[A-Za-z]//g' >> ${LOGFILE}) } #endregion #region main [ -d "${LOGDIR}" ] || mkdir -p "${LOGDIR}" ALL_SINKS=($(pactl list short sinks | awk '{ print $2 }')) CURR_SINK=$(pactl get-default-sink) CURR_INDEX=-1 log 1 "Using current sink [${CURR_SINK}]" for i in "${!ALL_SINKS[@]}"; do if [[ "${ALL_SINKS[$i]}" = "${CURR_SINK}" ]]; then CURR_INDEX="$i" fi done NEXT_INDEX=$(($((CURR_INDEX + 1))%(${#ALL_SINKS[@]}))) NEXT_SINK="${ALL_SINKS[$NEXT_INDEX]}" log 1 "Switching to sink [${NEXT_SINK}]" pactl set-default-sink "${ALL_SINKS[$NEXT_INDEX]}" #endregion