Team LiB
Previous Section Next Section

Hack 40. Create Your Own KDE Right-Click Menu Actions

Create custom menu actions for when you right-click a file, directory, or group of files and/or directories.

When you use KDE, do you ever find yourself having to click too many times to do a simple operation, or resort to opening up a terminal to do a task at the command line that should have been a no-brainer feature of KDE? Now you can add your own features to KDE context menus. Right-click a file and click ActionMake Executable to make the file executable. Right-click an empty space in a folder and click ActionCreate a new Bash Script to open an editor and fill in the first line. This is a very cool hack because you can create a context menu to launch scripts or programs written in any language. It is especially powerful if you learn DCOP scripting [Hack #39] . That way you can use the context menu selection to control KDE programs.

Remember all the times you wrote a dandy new bash script and created an icon to launch it, but when you clicked it, it didn't run? Instead, it popped up in an editor, because you forgot to make the script executable. With this hack, you can create a script to make your file executable by right-clicking its icon and selecting ActionsMake file executable (see Figure 5-4). It takes much less time than traditional methods.

All you have to do to create the context menu that will make a file executable is create a text file formatted very much like an application link, and then drop it in a special directory. We'll start by creating a file called make_executable.desktop. Start up your favorite editor, and enter the following text:

[Desktop Entry]

[Desktop Action MakeExe]
Name=Make file executable
Exec=chmod +x %f

Save your work. The action this file takes is defined by the entry Exec=chmod +x %f. KDE substitutes the name of the selected file for %f. If you want to make this feature available to everyone who uses KDE on this computer, place the file here:

# cp make_executable.desktop <path to kde> 

Depending on your Linux distribution, the path might not be tied to your KDE directory. It might be /usr/share/apps/konqueror/servicemenus.

If you simply want to make this feature available to yourself, place the file here (assuming your KDE settings are kept in ~/.kde; your distribution might use ~/.kde3.3 or something similar):

$ cp make_executable.desktop ~/.kde/share/apps/konqueror/servicemenus

The ServiceTypes field in the file is set to the mime-type application/x-shellscript, which means the menu option will be available only when you right-click a shell script file.

Figure 5-4. Context-sensitive Actions menu

5.7.1. Symbols Available for Menu Actions

Konqueror passes the names of selected URLs, selected files, and other selected elements to your custom menu actions through the use of symbols. Symbols exist for single files, multiple files, single URLs, and more. The right-click action that allows you to change a file to be executable works, because it substitutes the name of the selected file for the symbol %f. Here are some other symbols available to you, and how they work.


A single filename, even if you select multiple files. In general, you use this only when you intend to select a single file. In some cases, you can use this to select several files for use by a program that knows how to launch a separate instance for each filename.


Multiple selected files. Use this for programs that can act upon several files at once.


A single URL.


Multiple URLs.


The single directory that contains the selected file represented by %f.


A list of the same directory containing an entry for every selected file in that directory (%F).


A filename without a path.


Multiple filenames without paths.


A file represented as a URI.

5.7.2. Create an Action to Jump-Start Script Writing

Here's another hack to give you a taste of some of the power behind KDE custom menus. This time, you will exploit three powerful features of KDE. The first is the ability to create submenus. You will create a general menu selection that lets you choose between two actions that appear as submenus. The second is the fact that your custom action can be a script instead of a simple command, such as the chmod +x command we used in the previous example. Finally, the script will exploit the ability to use DCOP to manipulate KDE applications [Hack #39] .

To get started, create the file /usr/local/bin/CreateBashScript.desktop. Use your favorite editor to put the following contents into that file:

[Desktop Entry]
X-KDE-Submenu=Create Script

[Desktop Action CreateBashScript]
Name=Create a bash script
[Desktop Action CreatePythonScript]
Name=Create a Python script

In this case, the ServiceTypes definition of inode/directory tells KDE that the submenu entry Create Script will appear under the Action menu selection only if you are pointing to a folder or to an empty area within the Konqueror file manager (which represents the currently open folder). When you select this action, it will give you the choice of creating a bash or Python script. If you choose ActionCreate ScriptCreate a bash script, it will launch the shell script /usr/local/bin/writeBashScript. Use your favorite editor with sudo (or log in as root) to create the writeBashScript file, with the following contents:


cd ~/bin
kwrite &
sleep 2
kwid=`dcop | grep kwrite | sort | tail -n 1`
echo $kwid
dcop $kwid EditInterface#1 insertText 0 0 '#!/bin/bash'
dcop $kwid EditInterface#1 insertLine 1 ''
dcop $kwid EditInterface#1 insertLine 2 ''
dcop $kwid ViewCursorInterface#1-1 setCursorPosition 2 0

Here's how it works. First, it changes to the user's directory where the user keeps his personal ~/bin scripts. This is totally arbitrary, and it assumes you want to place all your personal shell scripts in this directory. Replace this directory with whatever directory you prefer (and one to which you have read, write, and execute access). Then it launches kwrite followed by an ampersand to let it run in the background while the writeBashScript shell script finishes. Again, the use of kwrite is arbitrary, but it is a good example of how to use DCOP to automate actions, because it is a KDE editor with DCOP capabilities. Then the script sleeps for 2 seconds (you can set this to a higher number if your system is always slow and busy) to make sure kwrite is running before it starts issuing DCOP commands to it.

The next line is a bit tricky. Each time you launch an instance of kwrite, it gets a DCOP listing with a number attached to it. You want to address the most recently launched copy of kwrite, so the script runs dcop to list all DCOP applications, finds all the applications that match "kwrite," sorts the list, and picks the last entry. That entry's name is assigned to the variable $kwid. This works even if this is the first instance of kwrite you have launched.

Then the script issues a few DCOP instructions. It automatically enters the first line of a typical bash script, #!/bin/bash, after which it enters two more blank lines and moves the cursor to the last blank line.

Now create the bash script that gets you started writing a Python script. The only difference between /usr/local/bin/writeBashScript and /usr/local/bin/writePythonScript is that you will insert the first line as the full path to your Python interpreter instead of #!/bin/bash. That line of code should look like this:

dcop $kwid EditInterface#1 insertText 0 0 '#!/usr/bin/python'

Obviously, there's no limit to the kinds of templates you can create for various scripting languages.

These examples barely scratch the surface of what you can do. You can create a single menu entry that lets you select a half dozen directories, tar and compress them, and burn them to a multisession CD, all in one fell swoop. And you have the flexibility to do this as a bash script, as a Python script, or in any method you prefer. You'll be surprised at how much more you rely on the GUI interface of KDE once you have customized menu features for your personal convenience.

    Team LiB
    Previous Section Next Section