Control Spotify from shell

First thing to do is to be able to control spotify from command line. Spotify has DBus interface and supports MPRIS2 DBus specification. But I’ve found a bash script, which wraps it in convenient functions here. Since I’d like to display current artist and song in the Awesome widget I need them to be in one line, so I’ve added another method to the bash script and added it in my Github:

function sp-current-oneline {
  sp-metadata | grep -E "(title|artist)" | sed 's/^\(.\)*|//' | sed ':a;N;$!ba;s/\n/ /g'
}

Let’s install it:

git clone https://gist.github.com/fa6258f3ff7b17747ee3.git
cd ./fa6258f3ff7b17747ee3 
chmod +x
sudo cp ./sp /usr/local/bin/

So from now you are able to control Spotify from command line! Try it: sp help.

Widget, Awesome widget

This widget is not compatible with v4.0 of Awesome WM. For v4.0 use this one

Create spotify.lua with following content under /home/.config/awesome or clone it from this repo.

local wibox = require("wibox")
local awful = require("awful")

spotify_widget = wibox.widget.textbox()
spotify_widget:set_font('Play 9')

function updateSpotifyWidget(widget)
  local current = awful.util.pread('sp current-oneline')
  widget:set_text(current)
end

spotify_timer = timer ({timeout = 10})
spotify_timer:connect_signal ("timeout", function() updateSpotifyWidget(spotify_widget) end) 
spotify_timer:start()

spotify_timer:emit_signal("timeout")

Now include it in rc.lua and add to your wibox:

require("spotify")
...
right_layout:add(spotify_widget)

Restart awesome, turn on some music and you’ll see it: spotify widget

Keyboard shortcuts

Nothing special here, I prefer to use Mod4+. for next track, Mod4+, for previous and Mod4+/ for play/pause:

awful.key({ modkey, }, "/", function () awful.util.spawn("sp play", false) end),
awful.key({ modkey, }, ".", function () awful.util.spawn("sp next", false) end),
awful.key({ modkey, }, ",", function () awful.util.spawn("sp prev", false) end)