I've been trying to write a shell script that will interface with cmus and then notify me of the track info using notify-send. Right now it is not working, mainly because xargs does not seem to pass 2 arguments to notify-send. It only sends one and I cannot figure out why. I've done everything I can think of with sed to get the right output but it doesn't work. Also, if I use notify-send with two arguments, it works, so I don't think it's a problem with notify-send.

The output of cmus-remote -Q is:

status paused
file /home/dennis/music/Coheed And Cambria/GOODAP~1/05 Crossing the Frame.mp3
duration 207
position 120
tag artist Coheed & Cambria
tag album Good Apollo I'm Burning Star IV Volume One: From Fear Through the Eyes of Madness
tag title Crossing the Frame
tag date 2005
tag genre Rock
tag tracknumber 5
tag albumartist Coheed & Cambria
set aaa_mode all
set continue true
set play_library true
set play_sorted false
set replaygain disabled
set replaygain_limit true
set replaygain_preamp 6.000000
set repeat false
set repeat_current false
set shuffle true
set softvol false
set vol_left 100
set vol_right 100

My code is terrible. I'm just starting to learn shell scripting so sorry about that.

# notify of song playing

info="$(cmus-remote -Q)"

title="`echo "$info" | grep 'tag title' | sed "s/'//g" | sed 's/tag title \(.*\)/'\''\1'\''/g'`"

artist="`echo "$info" | grep 'tag artist' | sed "s/'//g" | sed 's/tag artist \(.*\)/ '\''\1/g'`"
album="`echo "$info" | grep 'tag album ' | sed "s/'//g" | sed 's/tag album \(.*\)/ \1'\''/g'`"

echo "$stupid" | xargs notify-send
  • and if there's a better way to do this I'm definitely open to suggestions :) – Dennis Jun 18 '11 at 2:24

xargs is working as intended; each line is taken as a parameter. If you want multiple parameters, separate them with newlines.

{echo "$title"; echo "$artist"; echo "$album"} | xargs notify-send

That said, you're doing far too much work for something quite simple:

title="$(echo "$info" | sed -n 's/^tag title //p')"
artist="$(echo "$info" | sed -n 's/^tag artist //p')"
album="$(echo "$info" | sed -n 's/^tag album //p')"
notify-send "$title" "$artist" "$album"

(Also note one other gotcha: notify-osd sends the messages it's passed through Pango, so you need to escape anything that might be mistaken for Pango markup. This means <, >, and & in practice, much as with HTML and XML. The above doesn't try to handle this.)

  • I can only have two arguments. The first is the "title" and the second is the rest of the info. However for some reason when I do say: notify-send "$title" "$artist" the notification only shows the $title and not the $artist – Dennis Jun 18 '11 at 2:30
  • @Dennis: then I would check for embedded newlines or carriage returns. You can also combine the artist and album while leaving the title separate; and, not all that widely known, notify-send uses Pango so you need to escape any Pango markup in your strings (like HTML, you need to watch out for <, >, and &). – geekosaur Jun 18 '11 at 2:34
  • ran them through tr -d '\n' but no luck – Dennis Jun 18 '11 at 2:48
  • Ah you were right. It was the Pango. I tried it without the & and that fixed it. Is there a way I could pass the argument through and keep the ampersand? By the way thanks a bunch. – Dennis Jun 18 '11 at 2:52
  • Same as with HTML: replace < with &lt;, > with &gt;, and & with &amp;. – geekosaur Jun 18 '11 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.