Adding Spotify current track to tmux status bar

I love tmux. I’m a very recent convert.

I just discovered/embraced tmux over screen, so I’ve been hacking my ~/.tmux.conf a lot in the last couple days. It’s strangely satisfying to make your status bar display remote and local content in clever ways. The possibilities are endless.

Spotify has also been a necessity lately at work and at home. I’m using the Spotify Linux Preview

Since I’m running debian + openbox based Crunchbang lately as my daily driver, I usually run Spotify in a dedicated workspace. Sometimes I hear a good track, and I want to know the Artist / Track, so I shift over to my spotify workspace, and see who it is. That’s tedious. Obviously this is a task for tmux.

So how do we put the current “Artist – Track” in our tmux status bar?

The first thing I thought was: Can I use the Spotify API? So I tried that. I couldn’t find an API call for what I needed. There may be a way to use the API to find what you are listening to at the moment, but I couldn’t find it. Even if there is a way, it seems inefficient. I’m running the Spotify client locally — so how can I see what track is playing?

While looking into this, I noticed something interesting:

kevin@ox:~$ sudo netstat -tnlp | grep -i spotify
tcp 0 0 127.0.0.1:4371 0.0.0.0:* LISTEN 11227/spotify
tcp 0 0 0.0.0.0:57621 0.0.0.0:* LISTEN 11227/spotify
tcp 0 0 127.0.0.1:4381 0.0.0.0:* LISTEN 11227/spotify
tcp 0 0 127.0.0.1:8099 0.0.0.0:* LISTEN 11227/spotify

So Spotify is acting as a server from my machine. Hmm..

This guy Carl already figured this out for us. Check out his awesome post about this. So how can we talk to this local Spotify server and snag info?

Luckily for us, this other guy, nadavbar, based on Carl’s findings, took it a step further with a nodejs module. Awesome.

So this is now SUPER easy. We’re going to use nadavbar’s nodejs spotify-webhelper module to get our current track.

Create the following script somewhere, like ~/scripts/tmux/spotify-get-current-track.js

var nodeSpotifyWebHelper = require('node-spotify-webhelper');
var spotify = new nodeSpotifyWebHelper.SpotifyWebHelper({port: '4371'});

// get the name of the song which is currently playing
spotify.getStatus(function (err, res) {
if (err) {
return console.error(err);
}

var song = res.track.artist_resource.name + ' - ' + res.track.track_resource.name;
console.log(song.substring(0, 32));
});

You need to install nodejs. See this

Add the module:

npm install node-spotify-webhelper

Note that I’m using substring(0, 32) to limit the Artist – Track to 32 characters. This is so extra long artist/track names don’t break our tmux status bar.

You may also have to change the port in the script above. Check the output of: netstat -tnlp

Okay. Let’s test the script:

node ~/scripts/tmux/spotify-get-current-track.js

You should have gotten “Artist – Track” from that, assuming you’re running spotify currently. So let’s add it to our status bar in tmux.

Edit your ~/.tmux.conf :

set -g status-right '#[bg=black]#[fg=white]# ♫ #[fg=green]#(node /home/kevin/scripts/tmux/spotify-get-current-track.js'

Now we have something like this (I have some other things in my status-right in the image below — Ubersmith tickets, WHMCS tickets, Exchange server emails, Laptop battery status, etc.. maybe I’ll make another post about those):

tmux-kq-ex

3 thoughts on “Adding Spotify current track to tmux status bar”

  1. Hey Kevin,
    Thanks for the awesome hack, really useful.
    Just one thing, TYPO: a missing curved bracket

  2. Hi, it looks like it’s no longer working.
    I’ve made my own alternative to this using playerctl for Linux, no clue if it would work on osx.
    My solution has the advantage that it does not depends on Spotify but on playerctl which mean that even though I’m using it, it is working with any other players.

    ~/.scripts/music.sh:
    #!/bin/bash
    MUSIC=”♫ $(playerctl metadata xesam:albumArtist) – $(playerctl metadata xesam:title)”;
    echo ${MUSIC:0:40}

Leave a Reply

Your email address will not be published. Required fields are marked *