cmus-notify: simple bash script for cmus to print current song info w/ libnotify

cmusnot_2013-06-25
Standard

I’ll deviate a bit from the usual sketch and arts posts to share a little script in bash I’ve been using to enhance my user experience with cmus

what is cmus? (wiki)

Cmus is a console music player. I chose this one over a GUI option because all I want from a music player is to hear my songs. Because the need was so simple I decided to get something less resource hungry than my previous option (audacious). I tried exaile and other 2 options I no can no longer remember. In the end cmus worked as expected using almost no resources in this aging PC.

Cmus does a lot more stuff than just laying songs, and because of that there is little I miss about my previous player. There was only one thing I missed: The use of the mouse scroll wheel over the task bar icon to change songs.

cmus-remote

For controlling such behaviors cmus comes with a neat utility, a remote controller: cmus-remote, this is a console  application used to instruct cmus to do stuff, like playing, pausing, changing songs and also can be used to get media tag information.

Mapping keyboard shortcuts to the cmus-remote command is a breeze, but I still wanted some GUI feedback. To that end I made a little bash script to be used to print media tag information to the screen using libnotify: cmus-notify.sh

cmus-notify.sh

#!/bin/sh
Cmus_remote=$(cmus-remote -Q)
Instance=$(echo -e "$Cmus_remote" | wc -l)
Shuffle="Shuffle On"
if [ $Instance = 1 ]; then
    terminal -e cmus &&
    sleep 2
    cmus-remote -p
else
    if [ $1 == "-S" ]; then
        if [ `echo "$Cmus_remote" | grep shuffle | cut -d ' ' -f 3` == true ]; then
            Shuffle="Shuffle Off"
        fi
        cmus-remote $1
        notify-send -t 800 $Shuffle
    elif [ $1 == "-Q" ]; then
        Cur_song=$(echo "$Cmus_remote" | grep tag | head -n 3 | sort -r | cut -d ' ' -f 3- )
        notify-send -i multimedia-volume-control -t 1800 "$Cur_song"
#        artist=$(echo -e "$Cur_song" | head -n 2 | tail -n 1)
#        title=$(echo -e "$Cur_song" | head -n 1 )
#        cmus-updatepidgin artist "$artist" title "$title"
    else
        cmus-remote $1
        Cur_song=$(cmus-remote -Q | grep tag | head -n 3 | sort -r | cut -d ' ' -f 3- )
        notify-send -i multimedia-volume-control -t 1800 "$Cur_song"
    fi
fi

Usage is simple:

Copy and save the text above into a file. (In this case we create a file called cmus-notify.sh)

Make file executable. (Below is an example on how to do so using the terminal)

chmod u+x cmus-notify.sh

Then:

./cmus-notify.sh $option

The $option value can be replaced by any cmus-remote valid option. check cmus-remote man for complete reference, some commands include. “-p” for play, ‘-s’ to stop, ‘-S’ to toggle shuffle, ‘-Q’ to query player status.

The script is limited to work with single argument remote options, like play, stop, pause and toggle statuses.

How it works

  • The script first checks if ‘cmus-remote -Q’ returns anything, if it doesn’t it will launch a terminal (it uses the command “terminal” for that, but can be changed for xterm, aterm, gnome-terminal, etc.)
  • If there is an instance running, then it will take the option you gave as an argument and run cmus-remote with it unless the option is “-Q”, in this last case it will print the current song title and artist to libnotify. Im almost certain this could work on other notify sistems like xfce-notifyd, but I haven’t test it on them.
  • The commented out lines can be activated by removing the ‘#’ symbol. This lines require that you have the cmus-updatepidgin python module installed, and pidgin currently running for them to do something. Pidgin updater
  • If the option given is ‘-S’ it will switch the Shuffle setting on the player and print a notification about it.

Is it for you?

So long as you use cmus in a GUI environment and have libnotify installed, yes. I understand this is one of the most rare case scenarios, but if this script is not tailored to your environment you can always check my scripting and get some inspiration for a solution you might not yet know you need.

The script is not perfect, but it should be harmless.

2 thoughts on “cmus-notify: simple bash script for cmus to print current song info w/ libnotify

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s