#!/bin/bash # 2022 R. Branten # Updated 2024 # ---- # custom control shortcut file # start spotify in the brave browser and make it the first tab # ---- # https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html # check if spotify is active on the dbus BUS=$(dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep -i spotify | sed -r 's/ |string|[\"]//g') CONTROL="spotify" if [ -z "${BUS}" ]; then # spotify not active, check brave browser BUS=$(dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep -i brave | sed -r 's/ |string|[\"]//g') CONTROL="brave" fi if [ -z "${BUS}" ]; then # spotify not active, check clementine BUS=$(dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep -i clementine | sed -r 's/ |string|[\"]//g') CONTROL="brave" fi if [ -z "${BUS}" ]; then # nothing to control notify-send "Nothing to control" exit fi # extract and set argument case $1 in pause) # for example space was pressed dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause SENDNOTIFICATION=true ;; next) # for example alt+Right was pressed dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next SENDNOTIFICATION=true ;; previous) # for example alt+Left was pressed SENDNOTIFICATION=true dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous ;; shuffle) # for example alt+s was pressed if [[ "$CONTROL" == "spotify" ]]; then notify-send "Shuffle on/off" SENDNOTIFICATION=false # not clear if shuffle is implemented yet, TESTED AS NOT WORKING dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Shuffle else notify-send "Shuffle not implemented" exit fi ;; *) exit ;; esac # status can be: Playing, Paused or Stopped status=`dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus'|egrep -A 1 "string"|cut -b 26-|cut -d '"' -f 1|egrep -v ^$` artist=`dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 2 "artist"|egrep -v "artist"|egrep -v "array"|cut -b 27-|cut -d '"' -f 1|egrep -v ^$` album=`dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 1 "album"|egrep -v "album"|cut -b 44-|cut -d '"' -f 1|egrep -v ^$` title=`dbus-send --print-reply --dest=$BUS /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 1 "title"|egrep -v "title"|cut -b 44-|cut -d '"' -f 1|egrep -v ^$` notify-send Media: "${status}\n${artist} - ${title}" exit