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.*__aSyNcId_<_ajVAydMR__quot;) # 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. 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. -
Updates and suggestions are welcomed.