Team LiB
Previous Section Next Section

Hack 39. Script Hacks with DCOP

Use the KDE scripting tool of choice to tweak your desktop for maximum flexibility.

For most users, interaction with the desktop is a simple process that is limited to clicking buttons and typing things in from the keyboard. Although fine for most cases, this normal form of interaction limits what you can accomplish with an application, because you are restricted to the program elements that the GUI has been designed to show, and because complex or repetitive tasks require the user to input every command himself. Many programs can be augmented with a scripting tool to overcome traditional input methods.

Within the KDE feature set is a special tool called the Desktop COmmunications Protocol (DCOP). This facility can access a number of so-called "hooks" in KDE applications that can allow programs to communicate with other applications either remotely, with a scripting language, or from the command line. DCOP essentially provides a means for you to hook together graphical programs in the fine tradition that Unix and Linux command tools can be connected with pipes.

DCOP is a tool that typically resides in the developer's toolbox, out of sight from most users. But DCOP is also a tool that you can use in everyday desktop practice. Although hackers are animals that enjoy wallowing in reams of code, a little GUI application is available, which nonhackers can use as an interface to DCOP to facilitate the process of writing DCOP programs. It is called KDCOP and you can start it by typing kdcop into a console or in the Run application dialog box (Alt-F2).

5.6.1. DCOP and Processes

Each KDE application that you are running on the system has a number of DCOP "interfaces" that provide methods for learning information about the application and adjusting how it works. When you run KDCOP, each interface is displayed and you can play with them inside the program. For example, run KDCOP and then load a Konqueror browser. You will see Konqueror added to the list of applications in the KDCOP window. You should also see a number to the right of the entry, such as konqueror-1377. This number relates to the process number of the application. This number is added, because often more than one process is running for each program at any one time. If you click the Konqueror entry you are presented with a list of categories that contain DCOP interfaces.

To test an interface, look in the category called KonquerorIface and double-click the entry DCOPRef createNewWindow(QString url), as shown in Figure 5-3.

Figure 5-3. Browsing DCOP interfaces with KDCOP


Not surprisingly, this string creates a new window. The QString url part in brackets means you can pass the interface a web address or file location and the interface will display that resource in the window. When you double-click the interface, you are prompted for this value and then Konqueror opens a new window with the requested URL.

You can tie DCOP functionality into a number of places. For example, you can access a similar DCOP interface from the command-line shell with:

foo@bar:~$ dcop konqueror-1377 KonquerorIface openBrowserWindow http://www.oreilly.com/

Notice how you need to specify the process number of the application within the application name (your process number might be different from mine). This is so you can probe the correct Konqueror process and open a new browser window.

5.6.2. Write a DCOP Shell Script

The true power of DCOP comes into its own when you start writing shell scripts. As an example, imagine that numerous applications are in use across all your virtual desktops. Things are getting a little messy on these desktops and you want to tidy up. You can solve this problem by writing a little script that switches to each desktop and runs the cascadeDesktop interface, which organizes the windows in a cascaded order.

To create this script, create a new file called cascade.sh and add the following code:

#!/bin/bash
for i in seq 1 5:
do
  dcop kwin KWinInterface setCurrentDesktop $i
  dcop kwin KWinInterface cascadeDesktop
done

This code creates a bash for loop that loops four times (for the number of desktops). Inside the loop, it sets the current desktop to that of the loop number, and then it issues the cascadeDesktop interface command.

To run the script, you need to turn on the execute bit on the file:

foo@bar:~$ chmod a+x cascade.sh

Then you can run the script with:

foo@bar:~$ ./cascade.sh

As another example, say you want to receive an email when your friend Nick logs on to your instant messenger network. This could be particularly useful if you leave Kopete (the KDE instant messenger) running at home and you want to know when someone is online while you are at work. You can accomplish this with the following script:

#!/bin/bash

nick=$1;
echo Waiting for $nick
while ((`dcop kopete KopeteIface reachableContacts | grep -c $nick` == 0))
do
sleep 10
done
echo Sending message
mail -s "$nick is online" you@foo.com

Save this script to a file such as immail.sh and run it, like so:

foo@bar:~$  ./immail.sh bob

This script works by first making the command-line argument available as the $nick variable. After this, a while loop is started that uses grep to search for the chosen nick in $nick within the output of the reachableContacts interface in KopeteIface. Nick is not online, so the process will just sleep for 10 seconds. When Nick does come online, an email is sent to you@foo.com using the mail command.

    Team LiB
    Previous Section Next Section