Quick search and select track from MPD playlist. REQUIRES: bash, grep, zenity, notify-send
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/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