#!/bin/bash PARENT_BAR="bottom" PARENT_BAR_PID=$(pgrep -a "polybar" | grep "$PARENT_BAR" | cut -d" " -f1) PLAYERS=$(playerctl -l) readarray -t PLAYERS_ARRAY <<< "$PLAYERS" PLAYER= for CHECK_PLAYER in "${PLAYERS_ARRAY[@]}"; do PLAYERCTL_STATUS=$(playerctl status -p "$CHECK_PLAYER") if [ "$PLAYERCTL_STATUS" = "Stopped" ]; then continue elif [ "$PLAYERCTL_STATUS" = "Playing" ]; then PLAYER="$CHECK_PLAYER" break else PLAYER="$CHECK_PLAYER" fi done FORMAT="{{ title }} - {{ artist }}" update_hooks() { while IFS= read -r id do polybar-msg -p "$id" hook music-play-pause $2 1>/dev/null 2>&1 done < <(echo "$1") } PLAYERCTL_STATUS=$(playerctl --player=$PLAYER status 2>/dev/null) EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ]; then STATUS=$PLAYERCTL_STATUS else STATUS="No player is running" exit fi if [ "$1" == "--status" ]; then echo "$STATUS" elif [ "$1" == "--toggle" ]; then playerctl --player=$PLAYER play-pause elif [ "$1" == "--next" ]; then playerctl --player=$PLAYER next elif [ "$1" == "--prev" ]; then playerctl --player=$PLAYER previous else if [ "$STATUS" = "Stopped" ]; then echo "No music is playing" elif [ "$STATUS" = "Paused" ]; then update_hooks "$PARENT_BAR_PID" 2 playerctl --player=$PLAYER metadata --format "$FORMAT" elif [ "$STATUS" = "No player is running" ]; then echo "$STATUS" else update_hooks "$PARENT_BAR_PID" 1 playerctl --player=$PLAYER metadata --format "$FORMAT" fi fi