docs.kde.org
Scripting the Desktop
Prev
Next

Scripting the Desktop

KDE provides a powerful interprocess communication system in DCOP, the Desktop COmmunication Protocol. Using DCOP, you can control a wide range of functions in KDE from the command line or from a script written in your favorite scripting language. You can also get information out of KDE applications: for example, several KDE media players provide methods to query the player for information about the currently-playing track.

Broadly speaking, each KDE application provides one or more DCOP interfaces, which in turn provide methods (or, if you prefer, functions) that another application can call. So, the first step in using DCOP is to find the appropriate method for the task. The easiest way to do this is using the kdcop frontend to the available DCOP methods.

Run kdcop from a Konsole or the mini-CLI (the window which pops up on Alt+F2). The kdcop window shows the applications currently running which provide DCOP interfaces, using a tree view. In general, finding the correct method requires a little bit of searching through the tree view, but a useful hint is that the interface marked “(default)” usually contains the most frequently-used functions.

To test that the function does what we expect, double-click on the setColor entry. To set the color c, click on the color selector button, and choose a color. Set whether the color should be color A with the checkbox. Click OK and the background color is set.

To access the DCOP method from your favorite scripting language, you can either use DCOP bindings, if available in the kdebindings module, or call the dcop command-line application. For simple usage, calling the dcop command-line application is sufficient. To call a DCOP method on the command line, we need to specify the application and interface owning the method, the method itself, and the arguments, in a form suitable for the shell.

We specify the application, interface and method in that order, followed by the arguments in the same order that they are shown in kdcop. dcop has plenty of other options: take a look at the output of dcop --help.

That's enough theory: time for an example:

Example 16.1. A Background Color Changing Script with DCOP

With the dcop command-line application and a little bit of Perl, we're going to make a simple script which slowly cycles the desktop background through the spectrum.

Firstly, we look for the appropriate method with kdcop. For this example, we'll short circuit the searching, and go straight to it: the method we want is kdesktop->KBackgroundIface->setColor. The arguments and return type of the function are shown in the style of the C++ language. For setColor, the arguments are a color, c, which specifies the new background color, and a boolean (true or false) value, isColorA, which specifies whether the color is the first or second (this is useful for setting gradients and so on).

To use our setColor method on the command line, we use the following:

% dcop kdesktop KBackgroundIface setColor '#ffffff' false

To specify the color, we used the hexadecimal RGB value, as used in HTML. Note that it is enclosed in single quotes to protect the # from the shell.

To find the hexadecimal RGB value of a color, open any color chooser dialog in a KDE application (for example, in KDE Control Center, Appearance & Themes->Colors), select the color you want, and use the value given in the HTML text box.

So, that's all we need from DCOP; now it's just a case of writing a script around it. Here's a (very!) rough implementation:


$min=49;  # Minimum value of R, G, or B colour
$max=174; # Maximum value of R, G, or B colour
$step=5;  # Amount to step colour by on each step
$sleeptime=15; # Interval in seconds between each step

@start = ($max, $min, $min);
@colour = @start;

while (1) {
	foreach (0..5) {
		my $which = $_ % 3; # Which colour (R, G or B) to change
		my $updown = $_ % 2; # Whether to increase or decrease the colour value
		do {
			if ($updown == 0) { $colour[$which]+=$step; }
			if ($updown == 1) { $colour[$which]-=$step; }
			my $dcopcall=sprintf "dcop kdesktop KBackgroundIface setColor '#%x%x%x' true\n", @colour;
			system($dcopcall);
			sleep $sleeptime;
			} while (($colour[$which] >= $min) and ($colour[$which] <= $max));
		}
}

Just run the script with no arguments, and it will cycle the background colour through a slightly muted spectrum until it is killed. Voilà!

Of course, Perl isn't the only language you can use to write scripts with DCOP—if you prefer shell scripting, that's available too:

Example 16.2. Setting a background from the Internet

The following script gets the main image from the “User Friendly” comic strip and sets it as the desktop wallpaper, using commonly available tools and a little bit of DCOP:


#!/bin/sh
COMICURL=`wget -qO - http://www.userfriendly.org/static/index.html | \
          grep Latest | sed -e "s,.*SRC=\",," -e "s,\">.*,,"`
TMPFILE=`mktemp /tmp/$0.XXXXXX` || exit 1
wget -q -O $TMPFILE $COMICURL
dcop kdesktop KBackgroundIface setWallpaper $TMPFILE 1

The first line after the #!/bin/sh uses wget and some regular expression magic to extract the image location from the main page's HTML source. The second and third lines download the image, and finally, dcop sets the downloaded image as wallpaper.

Prev
Next
Home


docs.kde.org