dmenu_music (2935B)
#!/bin/bash
MPC="mpc -h 127.0.0.1 -p 6664 "
PLAYLIST_DIR="$HOME/Music/playlists/"
MUSIC_DIR="$HOME/Music/"
OPT=$(echo -e "0: Load Playlist\n1: Play Playlist\n2: Create New Playlist\n3: Add song to Playlist\n4: Remove song from Playlist\n5: Clear Playlist\n6: Update DB\n7: Toggle Random\n8: Stop Music" | dmenu -p "Select an option..." | awk 'BEGIN{FS=":"}{print $1}')
if [[ "$OPT" = "0" ]]; then
PLAYLIST=$(find "$PLAYLIST_DIR" -maxdepth 1 -type f -regextype posix-extended -regex ".*\.m3u$" | sed -E 's/(.*)+\/([^/]+)(\.m3u$)/\2/' | dmenu -l 20 -i -p 'Open which playlist? (type "any" for random)')
notify-send $(${MPC}load $PLAYLIST)
elif [[ "$OPT" = "1" ]]; then
EXISTS_CURRENT_PLAYLIST=$($MPC playlist | wc -l)
if [[ $EXISTS_CURRENT_PLAYLIST -gt 0 ]]; then
${MPC}play
else
notify-send "No playlist has been selected"
fi
elif [[ "$OPT" = "2" ]]; then
NEW_PL_NAME=$(dmenu -i -p 'Enter filename')
if [ -f ${PLAYLIST_DIR}/${NEW_PL_NAME}.m3u ]; then
notify-send "File already exists!"
else
cat "" > ${PLAYLIST_DIR}/${NEW_PL_NAME}.m3u
if [ -f ${PLAYLIST_DIR}/${NEW_PL_NAME}.m3u ]; then
notify-send "Playlist ${NEW_PL_NAME} created!"
fi
fi
elif [[ "$OPT" = "3" ]]; then
MUSIC_FILE=$(find "$MUSIC_DIR" -type f -regextype posix-extended -regex ".*\.mp3$" | dmenu -l 20 -p "Select a file...")
if [ ! -f "$MUSIC_FILE" ]; then
notify-send "No file was found!"
else
OPT_PLAYLIST=$(find "$PLAYLIST_DIR" -maxdepth 1 -type f -regextype posix-extended -regex ".*\.m3u$" | sed -E 's/(.*)+\/([^/]+\.m3u$)/\2/' | dmenu -l 20 -i -p 'Append to which playlist?')
REAL_OPT_PLAYLIST=${PLAYLIST_DIR}${OPT_PLAYLIST}
if [[ $(grep -c "$MUSIC_FILE" "$REAL_OPT_PLAYLIST") -gt 0 ]]; then
notify-send "File already in playlist $OPT_PLAYLIST"
else
echo "$MUSIC_FILE" >> "$REAL_OPT_PLAYLIST"
notify-send "File added to playlist $OPT_PLAYLIST"
fi
fi
elif [[ "$OPT" = "4" ]]; then
OPT_PLAYLIST=$(find "$PLAYLIST_DIR" -maxdepth 1 -type f -regextype posix-extended -regex ".*\.m3u$" | sed -E 's/(.*)+\/([^/]+\.m3u$)/\2/' | dmenu -l 20 -i -p 'Append to which playlist?')
REAL_OPT_PLAYLIST=${PLAYLIST_DIR}${OPT_PLAYLIST}
TARGET_SONG=$(cat "$REAL_OPT_PLAYLIST" | dmenu -l 20 -p "Remove which song?")
TARGET_LINE_NUMBER=$(grep -En "$TARGET_SONG" "$REAL_OPT_PLAYLIST" | awk 'BEGIN{FS=":"}{print $1}')
sed -i "${TARGET_LINE_NUMBER}d" file
notify-send "Song removed from playlist $OPT_PLAYLIST"
elif [[ "$OPT" = "5" ]]; then
${MPC}clear
notify-send "Playlist cleared!"
elif [[ "$OPT" = "6" ]]; then
notify-send $(${MPC}update)
elif [[ "$OPT" = "7" ]]; then
${MPC}random && notify-send $(${MPC}status | awk '(NR==3){match($0, /random: [a-zA-Z]+/, m); print m[0];}')
elif [[ "$OPT" = "8" ]]; then
${MPC}stop
fi