Ubuntu: Spotify Screensaver Toggle

New version: I've updated the script to use D-bus with spotify for Linux version 0.4.8 see Spotify Screensaver Toggle with D-bus for further information.

In a previous post, Lock Screen and Pause Spotify I provided a rudimentary script to pause spotify when your screensaver kicks in. Now with the Spotify Linux preview being available I've started using (and paying for) a Spotify unlimited subscription. I decided I wanted to update the script to be able to pause Spotify when the screen is locked and also start playing after I log-in.

Initially I was hoping to do all of this by listening to Dbus events for when the screensaver is activated/deactivated. However, for some reason it's not possible to play/pause spotify based on the DBus event saying that the screensaver is active (Presumably because it's not possible to focus a window after the screensaver is active). Because of this current limitation the workaround is use this script to lock the screen for you. This way it locks the screen after it has set-up the listeners for the screensaver Dbus events.

When the screensaver is de-activated the script fires off the function to get Spotify playing again but only if it was playing before the screensaver was activated.

When spotify has an api, it will then make sense to use that instead, which should mean that this script can then be completely run by listening to dbus events which would be much more elegant.

Dependencies

The deps are the same as before - here's the incantation to install them for Ubuntu.
sudo apt-get install wmctrl xvkbd

Usage

To set it up, link the script to somewhere in your path and then create a keystroke in "System -> Preferences -> Keyboard Shortcuts" to lock the screen. Once that's done, you should see the screen lock when that keystroke is executed and Spotify will be paused if it was playing.

Get the code

You can grab the code from here. Feel free to send me improvements or suggestions on how to make it better.

Known issue: I've noticed a bug in that it doesn't pause if the window is hidden. This is tricky as it's not visible to the window list. Suggestions how to workaround this are welcomed.

Additional thanks

Tommy Mattila's Spotify Control which was based on my original script, provided useful insight into handling the active window.
Show Comments