Skip to content

Instantly share code, notes, and snippets.

@bonnebulle
Last active June 10, 2024 14:33
Show Gist options
  • Save bonnebulle/93b148d9f391ed3d2a98f0ed7ce5a71c to your computer and use it in GitHub Desktop.
Save bonnebulle/93b148d9f391ed3d2a98f0ed7ce5a71c to your computer and use it in GitHub Desktop.
Change track with playerctl + notify !
#!/bin/bash
# DO Change track with playerctl + notify !
# USE Keyboard_media_key => "this_script.sh OPT_1 OPT_2"
# OPT_1 play, playpause, next, previous, infos
# OPT_2 no_notifiy ( disable notif )
# OPT_3/2 target ( app name )
## EX: player_playerctl_next_prev_notify.sh playpause spotify
# FIX If not works => use xtool search app xith its $classname
# VAR $classname "WebApp-Bandcamp6262" cf. $currentapp
# VAR app classname ... change w/ your need ==> use `wmctrl -lx` to list all
# DEP https://github.com/vlevit/notify-send.sh
# DEP https://github.com/altdesktop/playerctl
# URL https://gist.github.com/bonnebulle/93b148d9f391ed3d2a98f0ed7ce5a71c
# SOURCE https://github.com/altdesktop/playerctl#printing-properties-and-metadata
# SOURCE https://www.reddit.com/r/archlinux/comments/5np6ne/comment/dcdopbk/?utm_source=share&utm_medium=web2x&context=3
status=$(playerctl status)
if [[ $1 == "next" ]]
then
playerctl next
sleep 2
elif [[ $1 == "previous" ]]
then
playerctl previous
sleep 2
elif [[ $1 == "play" ]]
then
playerctl play
elif [[ $1 == "playpause" ]]
then
if [[ $3 ]]
then
playerctl -p $3 play-pause
elif [[ $2 && $2 != "no_notifiy" ]]
then
playerctl -p $2 play-pause
else
playerctl play-pause
fi
elif [[ $1 == "infos" ]]
then
# DO NOTHING continue
echo "infos"
elif [[ $status = "Playing" || $status = "Paused" ]]
then
# DO NOTHING continue
playerctl play
else
playerctl play
fi
artist=$(playerctl metadata --format "{{ lc(artist) }}")
album=$(playerctl metadata --format "{{ lc(album) }}")
title=$(playerctl metadata --format "{{ lc(title) }}")
url=$(playerctl metadata mpris:artUrl | cut -c 1-)
## Bug : Si == bandcamp => double next-track... Fix : bypass xdotool searching for bandcamp ... if current-window
## If NO Artist... Not works ... try xtool methode : switch into app + press key...
if [[ $artist != "" ]]
then
## WORKING => Send notif + end
# echo $status
if [[ $status == "Playing" && $1 != "playpause" ]] # IF Playing and NOT go pausing
then
notify-send.sh -t 6000 -i "$url" "$title" "→ $artist \n↦ $album"
elif [[ $status == "Paused" && $1 == "playpause" ]] # IF Paused and go palying
then
notify-send.sh -t 6000 -i "$url" "$title" "→ $artist \n↦ $album"
fi
else
## NOT WORKING => switch + key
currentapp=$(xdotool getactivewindow getwindowclassname)
titleonly=$(playerctl metadata --format "{{ lc(title) }}" | sed -e 's\| fil musique\\' | sed -e 's\| bandcamp\\')
if [[ $1 == "next" ]] # next
then
### if current-window != bandcamp => switch + key ; others => key only
if [[ $curryentapp != "WebApp-Bandcamp6262" ]]
then
xdotool search --name "bandcamp" key --window %@ Right
else
xdotool key Right
fi
elif [[ $1 == "previous" ]] # previous
then
if [[ $currentapp != "WebApp-Bandcamp6262" ]]
then
xdotool search --name "bandcamp" key --window %@ Left
else
xdotool key Left
fi
elif [[ $1 == "playpause" ]] # playpause
then
if [[ $currentapp != "WebApp-Bandcamp6262" ]]
then
xdotool search --name "bandcamp" key --window %@ Space
else
xdotool key Space
fi
fi
if [[ $2 != "no_notifiy" ]] # no_notifiy
then
sleep 0.5
notify-send.sh -t 6000 -i "$url" "$titleonly" ""
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment