mpris

on kitten's pastes | raw

expires: 07 May, 2024

 1#!/bin/sh -x
 2
 3parse_list_names() {
 4	while IFS='"' read -r t s _; do
 5		case $t in *string*) case $s in
 6			org.mpris.MediaPlayer2.*spotify*) player=$s break; ;;
 7			org.mpris.MediaPlayer2.*) player=$s ;;
 8		esac; esac
 9	done <<EOF
10$(dbus-send --session --print-reply \
11	--dest=org.freedesktop.DBus /org/freedesktop/DBus \
12	org.freedesktop.DBus.ListNames)
13EOF
14}
15
16dbus_mpris_send() {
17	parse_list_names
18
19	[ -n "$player" ] || {
20		echo "no music player found" >&2; exit 1
21	}
22
23	dbus-send --print-reply --dest="$player" /org/mpris/MediaPlayer2 \
24		"org.mpris.MediaPlayer2.Player.$1"
25}
26
27case $1 in
28	play) dbus_mpris_send PlayPause ;;
29	next) dbus_mpris_send Next ;;
30	prev) dbus_mpris_send Previous ;;
31	stop) dbus_mpris_send Stop ;;
32	*) echo "usage: mpris play|next|prev|stop" >&2; exit 1 ;;
33esac