Create a gist now

Instantly share code, notes, and snippets.

@amol9 /vlc_dbus.sh
Last active Jan 1, 2016

What would you like to do?
A bash script using gdbus to call vlc dbus interface methods and get / set properties.
#!/bin/bash
# bash script using gdbus to call vlc dbus interface methods and get / set properties
iface='org.mpris.MediaPlayer2'
dest="$iface.vlc"
obj='/org/mpris/MediaPlayer2'
i_prop='org.freedesktop.DBus.Properties'
get_prop="$i_prop.Get"
set_prop="$i_prop.Set"
i_introspect='org.freedesktop.DBus.Introspectable.Introspect'
common="--session --dest $dest --object-path $obj"
# check command
if [[ ! $1 ]]
then
options=(urischemes mimetypes status add volume quit pause play id instances list_services)
echo 'please choose from the following options:'
for o in ${options[@]}
do
echo -n -e $o'\t'
done
echo
exit 0
fi
# functions
parse_list() {
read list
echo $list | grep -Po "'\w.*?'" | tr -d "'" | sort
}
list_services() {
gdbus call --session --dest org.freedesktop.DBus --object-path /org/freedesktop/DBus --method org.freedesktop.DBus.ListNames | parse_list
}
check_arg() {
if [[ ! $1 ]]
then
echo $2
exit 1
fi
}
parse_num() {
read val
echo $val | grep -Po "(\d+.)?\d+"
}
parse_str() {
read val
echo $val | grep -Po "'.*'" | tr -d "'"
}
round() {
read val
printf "%.2f\n" $(echo "scale=2;$val" | bc)
}
# handle command
case $1 in
urischemes) # get supported uri schemes
gdbus call $common --method $get_prop "$iface" "SupportedUriSchemes" | parse_list
;;
mimetypes) # get supported mime types
gdbus call $common --method $get_prop "$iface" "SupportedMimeTypes" | parse_list
;;
status) # get player status
gdbus call $common --method $get_prop "$iface.Player" "PlaybackStatus" | parse_str
;;
add) # add track to playlist
check_arg "$2" 'please provide file/dir path'
no_track="$obj/TrackList/NoTrack"
gdbus call $common --method $iface.TrackList.AddTrack "file://$2" $no_track true
;;
volume) # set / get volume
if [[ $2 ]]
then
gdbus call $common --method $set_prop "$iface.Player" "Volume" "<double $2>"
else
gdbus call $common --method $get_prop "$iface.Player" "Volume" | parse_num | round
fi
;;
quit) # quit vlc
gdbus call $common --method $iface.Quit
;;
pause) # pause playback
gdbus call $common --method $iface.Player.Pause
;;
play) # start playback
gdbus call $common --method $iface.Player.Play
;;
id) # property: Identity
gdbus call $common --method $get_prop "$iface" "Identity" | parse_str
;;
instances) # list dbus names of all running vlc instances
list_services | grep vlc.instance
;;
list_services) # get a list of all dbus services
list_services
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment