music-mpd.sh (2239B)
1 #!/bin/sh 2 3 # Music playing script (mpd/mpc) 4 5 DMENU_SCRIPT="$XDG_DATA_HOME/regexghost/wm-scripts/dmenu-runner.sh" 6 MUSIC_DIR="$HOME/Music" 7 FAVOURITES_DIR="$MUSIC_DIR/Favourites" 8 9 # Technically these first 4 are unnecessary, could just bind directly to the command 10 # but I like it all being in one file 11 if [ "$1" = "--toggle-pause" ]; then 12 mpc pause-if-playing || play 13 exit 14 elif [ "$1" = "--quit" ]; then 15 mpc stop 16 mpc clear 17 exit 18 elif [ "$1" = "--next" ]; then 19 mpc next 20 exit 21 elif [ "$1" = "--previous" ]; then 22 mpc prev 23 exit 24 elif [ "$1" = "--favourite" ]; then 25 song="$(mpc current -f '%file%')" 26 echo "$song" | grep -q "CurrentPlaylist/" && exit 27 favourite_path="$song" 28 favourite_dir="$(dirname "$favourite_path")" 29 mkdir -p "$FAVOURITES_DIR/${favourite_dir}" 30 ln -sf "${MUSIC_DIR}/${song}" "$FAVOURITES_DIR/${favourite_path}" 31 notify-send "Added to favourites" 32 exit 33 elif [ "$1" = "--get-song" ]; then 34 song="$(mpc current -f '%title%')" 35 [ "$song" = "" ] && echo "None" && exit 36 echo "$song" 37 exit 38 fi 39 40 # Start mpd if not running, and set mode to shuffle 41 if ! pgrep mpd 2> /dev/null > /dev/null; then 42 mpd 43 sleep 0.5 44 fi 45 46 # If mpd is currently playing music, just notify-send the song title 47 if ! mpc status | grep -q "^\[playing\]"; then 48 song_name="$(mpc current -f '%artist% - %title%')" 49 notify-send -i emblem-music-symbolic "Currently Playing:" "$song_name" 50 exit 51 fi 52 53 if [ "$1" = "--choice" ]; then 54 # Ask for playlist 55 playlist="$(ls "$MUSIC_DIR" | sed 's/\([A-Z][a-z]\)/ \1/g' | sed 's/\([a-z]\)\([0-9]\)/\1 \2/g' | sed 's/^ //g' | "${DMENU_SCRIPT}" "Select Playlist:")" 56 [ "$?" != "0" ] && exit 57 playlist_path="$(echo "$playlist" | sed 's/ //g')" 58 else 59 playlist_path="CurrentPlaylist" 60 fi 61 62 # Check for sub playlists 63 if [ $(find "$playlist_path" -type d | wc -l) -ne 1 ]; then 64 playlist="$(ls "$playlist_path" | sed 's/\([A-Z][a-z]\)/ \1/g' | sed 's/\([a-z]\)\([0-9]\)/\1 \2/g' | sed 's/^ //g' | awk 'BEGIN {RS = ""} {print "All\n"$0}' | "${DMENU_SCRIPT}" "Select Playlist:")" 65 [ "$?" != "0" ] && exit 66 # If all songs, don't change playlist path 67 if ! [ "$playlist" = "All" ]; then 68 playlist_path="$playlist_path/$(echo "$playlist" | sed 's/ //g')" 69 fi 70 fi 71 72 mpc clear 73 mpc add "$playlist_path" 74 mpc repeat on 75 mpc shuffle 76 mpc play