D-bus programming

Some experiments with D-bus programming. Goal is to control and get info from the mediaplayer VLC.

A script to get the songinfo from VLC:

#!/bin/bash
while [ 1 ]
do
info=$(dbus-send --print-reply --type=method_call --dest=org.mpris.vlc /Player org.freedesktop.MediaPlayer.GetMetadata $@|grep variant|grep string|sed 's/^.\+"\(.\+\)"/\1/'|grep -v http)
dt=$(date)
echo $info $dt
echo $info-$dt > /home/patrik/vlcInfo.txt
sleep 10
done

The text is printed to vlcInfo.txt and then used by a screensaver that shows the info. VLC must be launched like "vlc --control dbus".
This script can only be used locally, ie in the same session. If we run VLC in one session (say in X locally) and then connects via SSH, we can't connext to that d-bus. A work-around is to find the other sessions d-bus-address and set it in our ssh-session.

Get Pid of VLC:

PID=$(pidof vlc)

Set d-bus-address:
DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
/proc/$PID/environ | \
sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`

Export:

export DBUS_SESSION_BUS_ADDRESS

Run command as the user running VLC:

su patrik -c "dbus-send --print-reply --type=method_call --dest=org.mpris.vlc /Player org.freedesktop.MediaPlayer.GetMetadata"

Then som cut & grap to get important info:
dbus-send --print-reply --type=method_call --dest=org.mpris.vlc /Player org.freedesktop.MediaPlayer.GetMetadata |grep -A 1 nowplaying|grep variant|cut -d "\"" -f2-

Lionel Ritchie - Endless Love (1981)"