Short D-Bus tips
August 24, 2008 KDE 2 CommentsDcop was a handy tool for instance to communicate with KDE applications from the shell. D-Bus allows for the same, and while searching for dbus equivalents for some old dcop commands I used I came up with some nice scripts that shall be backed up here now:
To get started, something simple: Lock the screen.
qdbus org.kde.krunner /ScreenSaver Lock
Use qdbusviewer to find more commands like that:

The SimulateUserActivity method highlighted in the screenshot above can be used for a nice wrapper script: Imagine you want to watch a movie or display a presentation and the application does not support disabling the screensaver itself. Use this little wrapper script to start the application:
#!/bin/bash
$* &
while jobs | grep -q Running
do
qdbus org.kde.krunner /ScreenSaver SimulateUserActivity
sleep 30
done
Save it as, let’s say, /usr/local/bin/noscreensaver and use it to open mplayer, for example: noscreensaver mplayer (I am aware that mplayer has a -stop-xscreensaver parameter). As long as the application is running, every thirty seconds user input is simulated with a dbus call to prevent the screen saver from starting. Once the application is closed, the script itself closes as well and the screen saver is enabled again (if it was before).
The next script is a bit more complicated: Whenever a tinyurl gets copied to the clipboard, let’s replace it with the link target. For instance, when copying http://tinyurl.com/5bz8v5 to the clipboard, I want it being replaced with http://planetkde.org instead. Here we go:
#/bin/bash
while true
do
if qdbus org.kde.klipper /klipper getClipboardContents | egrep -q ‘^(http://)?tinyurl.com/’
then
in=”$(qdbus org.kde.klipper /klipper getClipboardContents)”
target=”$(wget ${in} -O /dev/null 2>&1 | grep Location | sed ’s/^Location: \([^ ]*\) .*$/\1/’)”
qdbus org.kde.klipper /klipper setClipboardContents “${target}”
fi
sleep 1
done
The script however is not really suitable for every-day usage as it relies on polling for clipboard changes. If signals are available however, we can do fancy things like opening instant messages from a specific person directly:
#/bin/bash
dbus-monitor “type=’signal’,sender=’org.kde.kopete’,interface=’org.kde.Kopete’,path=’/Kopete’,member=’contactChanged’” | awk ‘/string “/ {print $2; fflush() }’ | sed -u ’s/”//g’ | while read contact
do
if [[ "$contact" == "${*}" ]]
then
if qdbus org.kde.kopete /Kopete contactProperties “$contact” | grep pending_messages: | grep -q ‘<p’
then
qdbus org.kde.kopete /Kopete openChat “$contact”
fi
fi
done
Save it as kopete-open-directly.bash, make it executable and call it like ./kopete-open-directly.bash <id-of-a-contact>
<id-of-a-contact> has to be a Kopete contact ID. To get a list of valid ones, run
qdbus org.kde.kopete /Kopete contacts | while read contact
do
qdbus org.kde.kopete /Kopete contactProperties “$contact” | grep display_name | sed “s/display_name:/${contact} <=> /”
done
If John has the Kopete ID MSNProtocol:me@someprovider.org:john@example.com for instance, executing kopete-open-directly.bash MSNProtocol:me@someprovider.org:john@example.com would open messages from John directly as long as the script is running.
Please notice that you might have to install some additional packages to get qdbusviewer, dbus-monitor, wget or other programs used in the scripts above. The D-Bus interfaces match todays SVN trunk, some of them might not be available in KDE 4.1 (the Kopete ones for example).










