This page was translated from Spanish with AI. The original version is in Spanish: read the original.

Polybar Spotify Module

November 29, 20224 minutes

If you use polybar, you have probably thought about adding a module to control Spotify and see which song is playing. In this post you will learn how to do it

Polybar Spotify Module

What is it?

It is a simple polybar module that shows which song is playing, and you can add buttons to pause and skip songs.

How do I add it to polybar?

To add it to polybar, follow these two steps: go to the file where you keep your polybar modules, in my case ~/.config/polybar/modules.ini, and copy the following at the very bottom

Modules

Song module

  • Add it if you want the currently playing song to be shown…
[module/song]
type = custom/script
exec = ~/.config/bin/song.sh
interval = 1
format-prefix = "  "
format = <label>

Previous module

  • Add it if you want a button to play the previous song
[module/previous]
type = custom/script
exec = ~/.config/bin/previous.sh
interval = 1
;format-prefix = "  "
format = <label>
click-left = dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous

Next module

  • Add it if you want a button to play the next song
[module/next]
type = custom/script
exec = ~/.config/bin/next.sh
interval = 1
;format-prefix = "  "
format = <label>
click-left = dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next

Play module

  • Add it if you want a button to pause and play a song
[module/play]
type = custom/script
exec = ~/.config/bin/play.sh
interval = 1
;format-prefix = "  "
format = <label>
click-left = dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

Files

For the modules to work, you have to put the following files in the ~/.config/bin/ folder. Remember to make all of them executable with chmod +x!

song.sh

  • Contents of song.sh
#!/bin/bash

if [[ "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames)" == *org.mpris.MediaPlayer2.spotify* ]]; then
	artist=$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' | awk '/artist/{getline; getline; split($0,a,"\""); print a[2]}')
	song=$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' | awk '/title/{getline; split($0,a,"\""); print a[2]}')
	echo $song - $artist
else 
	echo ""
fi

next.sh

  • Contents of next.sh
#!/bin/bash

if [[ "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames)" == *org.mpris.MediaPlayer2.spotify* ]]; then
        echo ""
else 
        echo ""
fi

previous.sh

  • Contents of previous.sh
#!/bin/bash

if [[ "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames)" == *org.mpris.MediaPlayer2.spotify* ]]; then
        echo ""
else 
        echo ""
fi

play.sh

  • Contents of play.sh
#!/bin/bash

if [[ "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames)" == *org.mpris.MediaPlayer2.spotify* ]]; then
        echo ""
else 
        echo ""
fi

New: Animated Song!

Some people who used these modules told me that very long songs took up a lot of space in polybar, so with the help of b3nder0n I made the following script, which basically animates the song text so it only takes up a predefined number of characters…

Configuration

The setup is almost the same as for the other modules. The steps are as follows

Files

For it to work properly, you have to create the following files in ~/.config/bin/, and don’t forget to make them executable with chmod

animated.sh
  • Contents of animated.sh
#!/bin/bash

lenght=15

if [[ "$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames)" == *org.mpris.MediaPlayer2.spotify* ]]; then
	artist=$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' | awk '/artist/{getline; getline; split($0,a,"\""); print a[2]}')
	song=$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' | awk '/title/{getline; split($0,a,"\""); print a[2]}')
	text="阮 $song - $artist "
    num_char=$(echo -n $text | wc -c)
	text2=${text// /#}
    

    if [[ $lenght < $num_char ]];then
        letter=$(cat ~/.config/bin/progress.txt)
        times=0
        result=()
        for (( i=0 ; i < ${#text} ; i++ )) {
            a[$i]=${text2:i:1}
        }

        until [ $lenght -lt $times ]; do
            letter=$(($letter + 1))
            times=$(($times + 1))
            if [[ $letter -lt $num_char ]]; then
                result+=(${a[$letter]})
            
            else
                letter=-1

            fi
            
        done

        echo $(($letter - $times + 1)) > ~/.config/bin/progress.txt
	    echo ${result[@]} | tr -d " " | tr "#" " " 

    else
        echo $text
    fi

else 
	echo ""
fi
progress.txt
  • You also need to create a file called progress.txt, also in ~/.config/bin/

Module

For it to work properly, you have to add it at the very bottom of your polybar modules file, in my case ~/.config/polybar/modules.ini, and then enable it in config.ini

[module/animated-song]
type = custom/script
exec = ~/.config/bin/animated.sh
interval = 1
format = <label>
  • To adjust the speed, change the interval value; the interval number is the speed per character.

  • To change the maximum number of characters, set it in the lenght variable in animated.sh