Skip to content

Instantly share code, notes, and snippets.

@csssuf
Last active April 19, 2023 10:52
Show Gist options
  • Save csssuf/13213f23191b92a7ce77 to your computer and use it in GitHub Desktop.
Save csssuf/13213f23191b92a7ce77 to your computer and use it in GitHub Desktop.
Adds now playing indicator from spotify or MPD to i3status
#!/bin/bash
i3status | while :
do
read line
dir=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
spotify_status=$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus' | tail -n1 | cut -d'"' -f2)
spotify_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 -f ${dir}/spotify_song.awk | head -n 1 | cut -d':' -f2)
spotify_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 -f ${dir}/spotify_song.awk | tail -n 1 | cut -d':' -f2)
mpd_song=$(mpc current)
if [ "$spotify_status" = "Playing" ] ; then
echo -n "NP: $spotify_artist - $spotify_song | $line" || exit 1
continue
elif [ "$mpd_song" != "" ]; then
mpd_status=$(mpc status | tail -2 | head -1 | cut -d' ' -f1 | tr -d '[]')
if [ "$mpd_status" = "playing" ]; then
echo -n "NP: $mpd_song | $line" || exit 1
continue
fi
echo -n $line || exit 1
continue
fi
echo -n $line || exit 1
done
/string *"xesam:artist/{
while (1) {
getline line
if (line ~ /string "/) {
sub(/.*string "/, "artist:", line)
sub(/".*$/, "", line)
print line
break
}
}
}
/string *"xesam:title/{
while(1) {
getline line
if (line ~ /string "/) {
sub(/.*string "/, "title:", line)
sub(/".*$/, "", line)
print line
break
}
}
}
@mstruebing
Copy link

If I'm using this I Lost the color definition for wlan up and down for example.
Do you know any workaround?

@Romern
Copy link

Romern commented Apr 10, 2017

To preserve color I enabled
output_format = "i3bar"
in i3status config and mutated the string with sed (line 14):
echo $(echo "$line" | sed 's/{\"name/{\"name\":\"music\",\"full_text\":\"'"$spotify_artist"' - '"$spotify_song"'\"},{\"name/') || exit 1
A bit messy, but it works.
https://gist.github.com/Romern/8cb31ae62b3f8e5dedb90c2c739dac70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment