aboutsummaryrefslogtreecommitdiff
path: root/.config/polybar/scripts/get_spotify_status.sh
diff options
context:
space:
mode:
authorsrdusr <trevorgray@srdusr.com>2022-11-16 16:28:29 +0200
committersrdusr <trevorgray@srdusr.com>2022-11-16 16:28:29 +0200
commit23015965a4cecbd7a15049f0239fed4cdf540248 (patch)
tree497d48b86e8f0f5e7b09cd0a7150c7f3a7579d50 /.config/polybar/scripts/get_spotify_status.sh
parent6c42a0ae74a91e8c8a2fd65911e4a498555f2952 (diff)
downloaddotfiles-23015965a4cecbd7a15049f0239fed4cdf540248.tar.gz
dotfiles-23015965a4cecbd7a15049f0239fed4cdf540248.zip
Added polybar
Diffstat (limited to '.config/polybar/scripts/get_spotify_status.sh')
-rwxr-xr-x.config/polybar/scripts/get_spotify_status.sh50
1 files changed, 50 insertions, 0 deletions
diff --git a/.config/polybar/scripts/get_spotify_status.sh b/.config/polybar/scripts/get_spotify_status.sh
new file mode 100755
index 0000000..e4ec716
--- /dev/null
+++ b/.config/polybar/scripts/get_spotify_status.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+# The name of polybar bar which houses the main spotify module and the control modules.
+PARENT_BAR="now-playing"
+PARENT_BAR_PID=$(pgrep -a "polybar" | grep "$PARENT_BAR" | cut -d" " -f1)
+
+# Set the source audio player here.
+# Players supporting the MPRIS spec are supported.
+# Examples: spotify, vlc, chrome, mpv and others.
+# Use `playerctld` to always detect the latest player.
+# See more here: https://github.com/altdesktop/playerctl/#selecting-players-to-control
+PLAYER="spotify"
+
+# Format of the information displayed
+# Eg. {{ artist }} - {{ album }} - {{ title }}
+# See more attributes here: https://github.com/altdesktop/playerctl/#printing-properties-and-metadata
+FORMAT="{{ title }} - {{ artist }}"
+
+# Sends $2 as message to all polybar PIDs that are part of $1
+update_hooks() {
+ while IFS= read -r id
+ do
+ polybar-msg -p "$id" hook spotify-play-pause $2 1>/dev/null 2>&1
+ done < <(echo "$1")
+}
+
+PLAYERCTL_STATUS=$(dbus-send --player=$PLAYER status 2>/dev/null)
+EXIT_CODE=$?
+
+if [ $EXIT_CODE -eq 0 ]; then
+ STATUS=$dbus-send_STATUS
+else
+ STATUS="No player is running"
+fi
+
+if [ "$1" == "--status" ]; then
+ echo "$STATUS"
+else
+ if [ "$STATUS" = "Stopped" ]; then
+ echo "No music is playing"
+ elif [ "$STATUS" = "Paused" ]; then
+ update_hooks "$PARENT_BAR_PID" 2
+ dbus-send --player=$PLAYER metadata --format "$FORMAT"
+ elif [ "$STATUS" = "No player is running" ]; then
+ echo "$STATUS"
+ else
+ update_hooks "$PARENT_BAR_PID" 1
+ dbus-send --player=$PLAYER metadata --format "$FORMAT"
+ fi
+fi