Muffinresearch Labs by Stuart Colville

Ubuntu: Lock Screen and Pause Spotify | Comments (1)

Posted in Code, Linux/Unix on 22nd October 2009, 4:23 pm by Stuart

When I used a mac I had a shonky little applescript to pause iTunes when I locked the screen.

These days I mostly listen to Spotify (running under wine) on Ubuntu which of course doesn’t have applescript. What I wanted was a similar script so I can hit a keystroke and pause spotify and lock the screen at the same time.

With a few utilities I was able to programatically tell if Spotify is playing and get focus on the Spotify window and send a keystroke to get spotify to pause. But the solution it has to be said is far from perfect.

The need to know if Spotify is actively playing is as a result of there only being a play/pause toggle activated by spacebar. Without testing this locking the screen with Spotify already paused and open would result in it starting when you lock the screen.

You’ll need the following packages installed to get it to work:

sudo apt-get install wmctrl xvkbd

wmctrl is a window manager CLI tool and xvkbd is a vitual keyboard that also has a CLI interface to send keystrokes.

Here’s the script:

#!/usr/bin/env bash

# By Stuart Colville
# http://muffinresearch.co.uk/archives/2009/10/22/ubuntu-lock-screen-and-pause-spotify/
# sudo apt-get install wmctrl xvkbd

# Check the title in the window list to work out if it's playing
TITLE=$(wmctrl -l | grep -o -e "Spotify.*$")

# If it's playing pause it
if [[ "$TITLE" != Spotify ]]; then
    wmctrl -a "Spotify" && xvkbd -q -delay 100 -text '\ '
fi

# Lock the screen
gnome-screensaver-command --lock

exit 0

Save it somewhere and make it executable e.g. chmod +x ~/bin/lock_screen.sh.

If you want it to work on a keycombo then just add an item to System -> Preference -> Keyboard Shortcuts

Known Issue: A problem I’ve spotted is that locking the screen causes Spotify to start playing if it’s open and paused. So I need to add a way to detect if Spotify is currently playing without which this script is pretty much useless. Fixed with libinotify-tools

The EPIC Hack here is using libinotify to detect if Spotify is playing by watching file accesses to the Storage directory. Can’t help but feeling there’s got to be a better way. It also means that a delay of 5 secs is needed to wait for an event.There was a better way! This is now done by interogating the window list via wmctrl

Updates and suggestions are welcomed.

Post Tools

Comments: Add yours

1. On February 1st, 2010 at 2:09 pm Tommy said:

Hello Stuart,

I wanted something to control my Spotify with from the command line so I can bind the multimedia keys for Spotify use. Your script was a great guide in this. I created the script you can find here:
http://sites.google.com/site/tommymattila/home/spotifycontrol

It can play/pause and switch to the previous and next songs in spotify at the moment.

I needed to use the window class, instead of the title, because for example using chromium-browser and having your page open matches the “Spotify.*”-string you use to identify the window of the running spotify instance.

-Tommy







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

© Copyright 2004-10 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.600s.