You are not logged in.

#1 2009-10-30 22:52:16

q0tsa
Member
Registered: 2009-07-20
Posts: 39

mpd_control - simple mpd client using dmenu

I hacked together a simple bash script that lists artists, albums, playlists etc using dmenu.

An example of how I usually use it:
print a list of all artists => enter search string => choose artist => get list of albums by this artist in next screen => choose album => get list of all titles in this album => choose a title => add it to the playlist after the current song and play it.

Actually there are a lot more options.

I use it with a patched dmenu version from AUR (vertical- and xft-patch). But it should also work with normal dmenu.

Have Fun!

http://aur.archlinux.org/packages.php?ID=32179

http://github.com/bubbl3gum/mpd_control

#!/bin/bash

#when set to exit, mpd_control will exit if you press escape
#when set to break, mpd_control will go the upper level if possible
ESC_ACTION="break"
# source configuration file for dmenu if exists

if [ -f $HOME/.dmenurc ]; then
  . $HOME/.dmenurc
else
  DMENU='dmenu -i'
fi

addaftercurrent(){
    
    #playlist is empty, just add the song
    if [ "$(mpc playlist | wc -l)" == "0" ]; then
        mpc add "$1" 

    #there is no current song so mpd is stopped
    #it seems to be impossible to determine the current songs' position when 
    #mpd is stopped, so just add to the end
    elif [ -z "$(mpc current)" ]; then 
        mpc play
        CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
        END_POS=$(mpc playlist | wc -l)
        mpc add "$1"
        mpc move $(($END_POS+1)) $(($CUR_POS+1))    
        mpc stop

    #at least 1 song is in the playlist, determine the position of the 
    #currently played song and add $1 after it
    else

        CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
        END_POS=$(mpc playlist | wc -l)
        mpc add "$1"
        mpc move $(($END_POS+1)) $(($CUR_POS+1))    
    fi
}
addaftercurrentandplay(){

    #playlist is empty, just add the song
    if [ "$(mpc playlist | wc -l)" == "0" ]; then
        mpc add "$1" 
        mpc play

    #there is no current song so mpd is stopped
    #it seems to be impossible to determine the current songs' position when 
    #mpd is stopped, so just add to the end
    elif [ -z "$(mpc current)" ]; then 
        mpc play
        CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
        END_POS=$(mpc playlist | wc -l)
        mpc add "$1"
        mpc move $(($END_POS+1)) $(($CUR_POS+1))    
        mpc play $(($CUR_POS+1))

    #at least 1 song is in the playlist, determine the position of the 
    #currently played song and add $1 after it
    else

        CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
        END_POS=$(mpc playlist | wc -l)
        mpc add "$1"
        mpc move $(($END_POS+1)) $(($CUR_POS+1))    
        mpc play $(($CUR_POS+1))
    fi
}

case $1 in
    
    -a|--artist)
    
    while true; do

        ARTIST="$(mpc list artist | sort -f | $DMENU)";
        if [ "$ARTIST" = "" ]; then $ESC_ACTION; fi
        
        while true; do

            ALBUMS=$(mpc list album artist "$ARTIST" | sort -f);
            ALBUM=$(echo -e "replace all\nadd all\n--------------------------\n$ALBUMS" | $DMENU);
            if [ "$ALBUM" = "" ]; then $ESC_ACTION;
            
            elif [ "$ALBUM" = "replace all" ]; then
                CUR_SONG=$(mpc current)
                mpc clear
                mpc find artist "$ARTIST" | mpc add 
                if [ -n "$CUR_SONG" ]; then mpc play; fi
                $ESC_ACTION
            elif [ "$ALBUM" = "add all" ]; then 
                mpc find artist "$ARTIST" | mpc add
                $ESC_ACTION
            fi
            
            while true; do
                
                TITLES=$(mpc list title artist "$ARTIST" album "$ALBUM")
                TITLE=$(echo -e "replace all\nadd all\n--------------------------\n$TITLES" | $DMENU);
                if [ "$TITLE" = "" ]; then $ESC_ACTION
                elif [ "$TITLE" = "replace all" ]; then
                    CUR_SONG=$(mpc current)
                    mpc clear;
                    mpc find artist "$ARTIST" album "$ALBUM" | mpc add 
                    if [ -n "$CUR_SONG" ]; then mpc play; fi
                    $ESC_ACTION
                elif [ "$TITLE" = "add all" ]; then
                    mpc find artist "$ARTIST" album "$ALBUM" | mpc add 
                    $ESC_ACTION
                
                fi

                while true; do
                    DEC=$(echo -e "add after current and play\nadd after current\nreplace\nadd at the end" | $DMENU);
                    case $DEC in 

                        "")
                        $ESC_ACTION
                        ;;

                        "add after current and play")
                        addaftercurrentandplay "$(mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 )"
                        ;;

                        "add after current")
                        addaftercurrent "$(mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 )"
                        ;;

                        "replace")
                        CUR_SONG=$(mpc current)
                        mpc clear
                        mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 | mpc add
                        if [ -n "$CUR_SONG" ]; then mpc play; fi
                        ;;
                        
                        "add at the end")
                        mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 | mpc add
                        ;;

                    esac
                    $ESC_ACTION
                done
            done
        done
    done
    ;;

    -t|--track)
        
    TITLE=$(mpc list title | sort -f | $DMENU)
    if [ "$TITLE" = "" ]; then exit; fi
    
    SONG=$(mpc find title "$TITLE" | head -1) 
    addaftercurrentandplay "$SONG"
    ;;

    -p|--playlist)
    PLAYLIST=$(mpc lsplaylists | $DMENU);
    if [ "$PLAYLIST" = "" ]; then exit; fi
    CUR_SONG=$(mpc current)
    mpc clear
    mpc load "$PLAYLIST";
    if [ -n "$CUR_SONG" ]; then mpc play; fi
    ;;

    -j|--jump)
    
    TITLE=$(mpc playlist | $DMENU);
    if [ "$TITLE" = "" ]; then exit; fi
    POS=$(mpc playlist | grep -n "$TITLE" | awk -F: '{print $1}')
    mpc play $POS;
    ;;

    -l|--longplayer)
    
    while true; do

        ALBUM=$(mpc list album | sort -f | $DMENU);
        if [ "$ALBUM" = "" ]; then $ESC_ACTION;
        fi
        
        while true; do
            
            TITLES=$(mpc list title album "$ALBUM")
            TITLE=$(echo -e "replace all\nadd all\n--------------------------\n$TITLES" | $DMENU);
            if [ "$TITLE" = "" ]; then $ESC_ACTION
            elif [ "$TITLE" = "replace all" ]; then
                CUR_SONG=$(mpc current)
                mpc clear;
                mpc find album "$ALBUM" | mpc add 
                if [ -n "$CUR_SONG" ]; then mpc play; fi
                $ESC_ACTION
            elif [ "$TITLE" = "add all" ]; then
                mpc find album "$ALBUM" | mpc add 
                $ESC_ACTION
            
            fi

            while true; do
                DEC=$(echo -e "add after current and play\nadd after current\nreplace\nadd at the end" | $DMENU);
                case $DEC in 

                    "")
                    $ESC_ACTION
                    ;;

                    "add after current and play")
                    addaftercurrentandplay "$(mpc find album "$ALBUM" title "$TITLE" | head -1 )"
                    ;;

                    "add after current")
                    addaftercurrent "$(mpc find album "$ALBUM" title "$TITLE" | head -1 )"
                    ;;

                    "replace")
                    CUR_SONG=$(mpc current)
                    mpc clear
                    mpc find album "$ALBUM" title "$TITLE" | head -1 | mpc add
                    if [ -n "$CUR_SONG" ]; then mpc play; fi
                    ;;
                    
                    "add at the end")
                    mpc find album "$ALBUM" title "$TITLE" | head -1 | mpc add
                    ;;

                esac
                $ESC_ACTION
            done
        done
    done
    ;;

    -h|--help)
    echo -e "-a, --artist        search for artist, then album, then title"
    echo -e "-t, --track        search for a single track in the whole database"
    echo -e "-p, --playlist        search for a playlist load it"
    echo -e "-j, --jump        jump to another song in the current playlist"         
    echo -e "-l, --longplayer    search for album, then title"
    
    
    
    
    ;;
    
    *)
    echo "Usage: mpc_control [OPTION]"
    echo "Try 'mpc_control --help' for more information."
    ;;

esac

Last edited by q0tsa (2009-11-23 10:51:43)

Offline

#2 2009-10-31 04:47:17

_will
Member
Registered: 2004-07-08
Posts: 123

Re: mpd_control - simple mpd client using dmenu

nice. thanks!

Offline

#3 2009-11-01 20:17:22

prol
Member
Registered: 2008-03-27
Posts: 75

Re: mpd_control - simple mpd client using dmenu

PERFECT!! Have looking for something like this for a whileThank you for sharing this smile

Offline

#4 2009-11-04 18:23:52

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: mpd_control - simple mpd client using dmenu

Hello.
My version of dmenu MPD client stopped to work and I founded this and it AWESOME!
Thanks smile


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#5 2009-11-12 18:22:21

q0tsa
Member
Registered: 2009-07-20
Posts: 39

Re: mpd_control - simple mpd client using dmenu

Thanks for the comments.

Offline

#6 2009-11-23 10:52:22

q0tsa
Member
Registered: 2009-07-20
Posts: 39

Re: mpd_control - simple mpd client using dmenu

Now there is a --longplayer switch and a aur package.

Offline

#7 2009-12-23 22:09:27

YamiFrankc
Member
From: Mexico
Registered: 2009-06-19
Posts: 177
Website

Re: mpd_control - simple mpd client using dmenu

This is awesome.
But, is there a way to make it display the albums and songs like in ncmpcpp?
I mean, albums by year and songs by track number(if any).


Thanks and greetings.

Offline

#8 2009-12-24 21:11:06

~/home/
Member
Registered: 2009-11-06
Posts: 3

Re: mpd_control - simple mpd client using dmenu

this is a great script! thanks a lot and merry xmas wink

Offline

#9 2009-12-25 13:03:39

bharani
Member
From: Karaikudi, India
Registered: 2009-07-12
Posts: 202

Re: mpd_control - simple mpd client using dmenu

Thank You . Very nice script.


Tamil is my mother tongue.

Offline

#10 2010-03-25 07:29:05

MrAwesome
Member
From: San Francisco
Registered: 2007-10-30
Posts: 4

Re: mpd_control - simple mpd client using dmenu

Wow, this is fantastic! Thank you so much!


We all woke up 8 years later,
and there wasn't a lot left to not feel.

We figured out a way anyway.

Offline

#11 2010-03-25 22:56:24

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: mpd_control - simple mpd client using dmenu

thanks bro'.
keep sharing.

Offline

Board footer

Powered by FluxBB