![]() | Scripting the Desktop |
| Prev | Tinkering Under the Hood of KDE | Next |
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. By way of an example, let's see how you could write a script to cycle the desktop background color through the spectrum.
Example 14.1. A Background Color Changing Script with DCOP
Broadly speaking, each KDE application provides one or more DCOP interfaces, which in turn provide methods that another application can call. So, the first step in writing our script is to find the appropriate method to change the desktop background color. The easiest way to do this is using the kdcop frontend to the available DCOP methods.
Run kdcop from a Konsole or the
minicli (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 for this example, we'll short
circuit that: the method we want is ->->. 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 value, isColorA, which specifies whether
the color is the first or second (this is useful for setting gradients
and so on).
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 the simple usage which we want, 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. To use our
setColor method on the command line, we use
the following:
We specify the application, interface and method in that order, followed by the arguments in the same order that they are shown in kdcop. To specify the color, we use the hexadecimal RGB value, as used in HTML. Note that it is enclosed in single quotes to protect the # from the shell. dcop has plenty of other options: take a look at the output of%dcop kdesktop KBackgroundIface setColor '#ffffff' false
dcop
--help.So, that's all we need from DCOP, now it's just a case of writing a script around it. Here's a rough implementation in Perl:
$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à! Instant scripting pleasure!
Related Information
| Prev | Home | Next |
| Tinkering Under the Hood of KDE | Up | Adding Extra Keybindings to KDE |