Skip to content

Instantly share code, notes, and snippets.

@pritambaral
Last active Dec 22, 2015
Embed
What would you like to do?
Quick search and select track from MPD playlist. REQUIRES: bash, grep, zenity, notify-send
#!/bin/bash
# Prompts user to select song from mpd playlist by search tag.
# Randomizes if multiple matches found and first try was unwanted
if [ ! -f /tmp/jumpd.status ]
then
touch /tmp/jumpd.status;
fi
CACHE=$(cat /tmp/jumpd.status);
QUERY=$(zenity --entry --entry-text="$CACHE");
if [ "$?" -ne "0" ]
then
exit #User cancelled action
fi
echo "$QUERY" > /tmp/jumpd.status
LINES=$(grep -i "$QUERY" ~/.mpd/state | wc -l);
if [ "$LINES" -eq "0" ]
then
notify-send -t 500 'No song found' "For query $QUERY"
exit
fi
#What if user doesn't like the first result?
#No problem, we'll pick the next one
#Just repeat query
if [ "$CACHE" == "$QUERY" ]
then
INDEX=$(cat /tmp/jumpd.index 2>/dev/null)
INDEX=${INDEX:-1}; #default value 1
INDEX=$((INDEX%LINES)) #upper-bound index by LINES
INDEX=$((INDEX+1));
else
INDEX=1
fi
echo $INDEX > /tmp/jumpd.index;
INDEX=$(grep -i "$QUERY" ~/.mpd/state | grep -m $INDEX -o "^[[:digit:]]*[^:]" | tail -1)
INDEX=$((INDEX+1));
TRACK=$(mpc play $INDEX | head -1 | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g')
notify-send -t 500 'Found and Playing' "$TRACK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment