Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
set -e
# Toggle is possible only while track is playing
if playerctl --player=spotify status | grep -v Playing; then
exit 1
fi
# Kill other instances of this script (to avoid windows swithing races)
kill $(pgrep -f $(basename $0) | grep -v $$) || true
PREVIOUS_WINDOW_ID=`xdotool getactivewindow`
eval $(xdotool getmouselocation --shell)
PREVIOUS_MOUSE_X=$X
PREVIOUS_MOUSE_Y=$Y
eval $(xdotool search --class Spotify getwindowgeometry --shell %2) # don't know what %1 is
SPOTIFY_WINDOW_ID=$WINDOW
SPOTIFY_WINDOW_HEIGHT=$HEIGHT
TRACK_TITLE_X_POSITION=18
TRACK_TITLE_Y_POSITION=$((SPOTIFY_WINDOW_HEIGHT - 46))
TOGGLE_FAVORABILITY_X_POSITION=60
TOGGLE_FAVORABILITY_Y_POSITION=$((SPOTIFY_WINDOW_HEIGHT - 135))
# Unfortunately `--sync` in `xdotool windowactivate` command later does nothing in this case,
# so we have to wait for unminimizing if window was minimized
if xprop -id $SPOTIFY_WINDOW_ID | grep '_NET_WM_STATE_HIDDEN'; then
wmctrl -ia $SPOTIFY_WINDOW_ID
sleep 0.3
fi
xdotool \
windowactivate --sync $SPOTIFY_WINDOW_ID \
mousemove --window $SPOTIFY_WINDOW_ID $TRACK_TITLE_X_POSITION $TRACK_TITLE_Y_POSITION \
click 3 \
mousemove --window $SPOTIFY_WINDOW_ID $TOGGLE_FAVORABILITY_X_POSITION $TOGGLE_FAVORABILITY_Y_POSITION \
click 1 \
mousemove $PREVIOUS_MOUSE_X $PREVIOUS_MOUSE_Y
sleep 1 # to be sure that everything is OK (since there is no desktop notification on this action)
wmctrl -ia $PREVIOUS_WINDOW_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment