Automate KDE with DCOP and PERL

One of the great things about KDE is that it makes heavy use of DCOP.

DCOP, which stands for Desktop COmmunication Protocol, is a light-weight interprocess and software componentry communication system. The main point of this system is to allow applications to interoperate, and to share complex tasks. Essentially, DCOP is a ‘remote control’ system, which allows an application or a script to enlist the help of other applications. It is built on top of the X Window System’s Inter-Client Exchange protocol.

Note: DSUB and KDSUB replaces DCOP and KDCOP in KDE4, so if you are using KDE4 use those respectively.

In this tutorial I will show you how to create a simple perl script to strip HTML and white-space from your klipboard contents.
The possible uses are practically endless as are the potential productivity gains.

Ok so lets begin.

The first thing we need to do is figure out if we can talk to klipper via DCOP.

dcop | grep klipper
klipper

Here I used grep to filter the results. To get a list of all running applications that use DCOP just type dcop.

The next thing we need to do is figure out what commands/methods are available. DCOP list these in groups and to get the list of groups we just type dcop followed by the application name.

dcop klipper
qt
MainApplication-Interface
klipper

We want to do so we want to take a look at the klipper group.

dcop klipper klipper
QCStringList interfaces()
QCStringList functions()
QString getClipboardContents()
void setClipboardContents(QString s)
void clearClipboardContents()
void clearClipboardHistory()
QStringList getClipboardHistoryMenu()
QString getClipboardHistoryItem(int i)
int newInstance()
void quitProcess()

Great, getClipboardContents looks like the thing we want, so let's put some text on our clipboard and try it.

To get some HTML on my clipboard that I want to cleanup, I am using the country selection list from the shopping cart on godaddy.com.
If I select the list, right click and select copy source I get the following.

<option selected="selected" value="226">United States</option>

<option value="4">Afghanistan</option>
<option value="7">Albania</option>
<option value="60">Algeria</option>

The entire list is 281 lines long, which is not something we would want to edit manually.
To print the clipboard contents via DCOP type dcop application group command

dcop klipper klipper getClipboardContents
<option selected="selected" value="226">United States</option>
<option value="4">Afghanistan</option>
<option value="7">Albania</option>

 

This is really too easy. Now all we need to do is write a perl script to remove the HTML tags and whitespace. I keep a bunch of nifty scripts like this in my home/bin directory.
I also have the strip html and strip white-space as seperate scripts because I don't always want to do both.
I don't want to turn this into a perl or regular expressions tutorial, so here is the code.

 

clipboard_strip_html

#!/usr/bin/perl

$string = `dcop klipper klipper getClipboardContents`;
$string =~ s/<[^>]+?>//ig;
`dcop klipper klipper setClipboardContents "$string"`;

the first line is just the path to perl so I can run the script by typing clipboard_strip_html instead of typing perl clipboard_strip_html. This only works if the file is set as executable and in a dir that is in your environment path. The backticks are just a simple way to run system commands from perl and capture the output. The regular expression is a very very simple html tag stripper.

Here is the script to remove white-space

clipboard_trim

#!/usr/bin/perl
 
$string = `dcop klipper klipper getClipboardContents`;
$string =~ s/^\s+//igm;
$string =~ s/\s+$//igm;
`dcop klipper klipper setClipboardContents "$string"`;

So now if I type clipboard_strip_html from a terminal I get the following pasted back to my clipboard.

    United States

    Afghanistan
    Albania
    Algeria

and clipboard_trim produces

United States
Afghanistan
Albania
Algeria

So now I can get a straight text list of all 240 countries in a matter of seconds.

Here is another tiny script to count the number of lines on the clipboard

clipboard_countlines

#!/usr/bin/perl

$count = `dcop klipper klipper getClipboardContents | wc -l `;
`kdialog --msgbox $count`;

Please dig this article if it helped yoiu out. Also, feel free to post questions and comments. If you have some cool dcop scripts you have written please share them in the comments.