Create Your Own Custom KDE Service Menu


by Paul Arnote (parnote)

You are a KDE4 user. KDE’s Ark is your favorite file compression utility.You open Dolphin in KDE4, move into your “Downloads” directory, and select the latest *.zip file that you just downloaded, filled with pictures from your Uncle Ben’s vacation. You right click on the file and select “Extract Archive Here, Autodetect Subfolder” from the “Extract” menu on the Dolphin context menu. Tada! All of Uncle Ben’s vacation pictures are now in their own folder, ready for your viewing pleasure.

Welcome to the world of KDE service menus. In the language of KDE, that’s what they are called, anyways. Thunar has user definable custom actions built right in. Nautilus uses custom bash scripts, which it refers to as Nautilus scripts. Sorry PCManFM users, but PCManFM has not matured to the point of having customizable context menus, so you are out of luck when it comes to customization.

KDE defines service menus in .desktop files. While not particularly difficult to understand, the real chore is in figuring out where KDE keeps them stashed. There are a few places to look. All the locations are defined in the $KDEHOME environment variable. Instead of spending an eternity looking around for the location of where your “services” directories are located, just run the following command from a command line prompt in Konsole (or other favorite terminal):

kde4-config --path services

Mine happens to list two directories: /home/paul_lenovo/.kde4/share/kde4/services/ and /usr/share/kde4/services/. The differences are that the former directory lists “services” that are available only to me, as a solitary user, while the latter directory lists “services” that are available globally, to all users.

What Is A Service Menu?

Just as with many files in Linux, the .desktop files that define the KDE service menus are simple text files. Armed with your favorite text editor and an idea, you can easily create your own custom service menu. Before we can customize a KDE service menu, we must first understand what it is.

Here is the first part of the aaphoto.desktop file (located in /usr/share/kde4/services/) that adds the aaphoto service menus to Dolphin:

[Desktop Entry]

Type=Service

Name=aaphoto

Encoding=UTF-8

ServiceTypes=KonqPopupMenu/Plugin,image/*

Actions=Correction;FlipX;FlipY;

MimeType=mif;PNM;pgm;ppm;bmp;ras;JP2;JPC;jpg;jpeg;png

Icon=image-x-applix-graphics

X-KDE-Submenu=aaphoto

The first line specifies the “type” as a “Service.” The second line specifies the name for the service. In this case, it specifies “aaphoto” as the name for the top level menu entry in the file manager context menu. The third line specifies the encoding, typically UTF-8.The fourth line specifies the service type. Here, it is “KonqPopupMenu/Plugin,image/*”. Both Dolphin and Konqueror will utilize the same service menus.The fifth line specifies the actions that will be defined later in the aaphoto.desktop file.

The sixth line, “MimeType”, specifies what types of files the service menu is intended for. In the aaphoto.desktop file, this line lists all the image file types that aaphoto can work its magic on. If you want the service menu to be available for all file types, use “application/octet-stream” as the mime type. If you want the service menu to be active for use on directories, use “inode/directory” as the mime type. Separate multiple file types with a semicolon, and be careful to not include any spaces.

The seventh line, “Icon,” specifies just the name of the icon file to use to display next to the menu entry. There is no need to specify the file extension, but remember that the icon must exist in a directory in your path. The eighth line reiterates the name of the submenu to be inserted into the file manager context menu.

Now that we have the foundation laid, so to speak, it’s time to specify the individual actions in the service menu. Here are the individual actions, as they are defined in the aaphoto.desktop service menu file:

[Desktop Action Correction]

Name=Automatic photo adjusting

Name[de]=Automatische Foto Korrektur

Exec=aaphoto -a %f

Icon=image-x-applix-graphics

[Desktop Action FlipX]

Name=Mirror image horizontally

Name[de]=Bild spiegeln horizontal

Exec=aaphoto --flipx %f

Icon=image-x-applix-graphics

[Desktop Action FlipY]

Name=Mirror image vertically

Name[de]=Bild spiegeln vertikal

Exec=aaphoto --flipy %f

Icon=image-x-applix-graphics

Notice that each section begins with “Desktop Action” followed by the action that was specified in the first section of the aaphoto.desktop file – all enclosed in brackets. This part is case sensitive, so be careful to get capitalization correct.

Next, each section is assigned a “Name,” and this is the text that will appear in the aaphoto submenu. If you want to include multiple languages, specify each on its own line, starting with Name, followed by the two letter language code in brackets, then the text for that language. In our example above, there is the “Name[de]” line, for the German language. A line for the Spanish language would start with “Name[es]” and the Finnish language would be “Name[fi].” Continue on in this manner for each language you want to provide a translation for. You can find a list of all the two letter language codes here.

The third line of each desktop action section is where everything happens. The “Exec” line specifies the action to execute. In our case with the aaphoto.desktop file, it’s the execution of the aaphoto command line application, complete with the appropriate command line switches. Take notice of how the command ends with %f. Known as a “field code,” the %f passes a single filename to aaphoto, even though aaphoto can process a whole directory at once. Other “choices” might be %F (multiple files), %u (a single URL or file path), or %U (multiple URLs or file paths). You can view the entire list of recognized field codes here, from the freedesktop.org standards. Which field code you use is dependent on whether or not the application you’re calling requires filenames or URLs, and whether or not the application can process single or multiple files or URLs.

If you have a complex task that requires more than one command to be executed, the KDE developers recommend using a shell. For example, the “Exec” line in your .desktop file should read Exec=/bin/sh -c “;<YOUR COMMANDS HERE>” – or something similar.

Customizing The KDE Service Menu

As I mentioned in my article about aaphoto, the choices for the included KDE service menus is certainly WAY less than ideal. But armed with our new knowledge about KDE service menus, we can correct that entire situation, and give ourselves some KDE service menus that are actually useful.

For starters, let’s add service menus to the aaphoto.desktop file that rotate images. (NOTE: you can only edit the aaphoto.desktop file as the root user!) In the first part of the aaphoto.desktop file, let’s add Rotate90, Rotate180 and Rotate270 to the Actions entry (line 5). Your “Actions” entry should now read like this:

Actions=Correction;FlipX;FlipY;Rotate90;Rotate180;Rotate270

Now, add the following sections to the end of the aaphoto.desktop file:

[Desktop Action Rotate90]

Name=Rotate 90 Degrees

Exec=aaphoto --rotate90 %f

Icon=image-x-applix-graphics

[Desktop Action Rotate180]

Name=Rotate 180 Degrees

Exec=aaphoto --rotate180 %f

Icon=image-x-applix-graphics

[Desktop Action Rotate270]

Name=Rotate 270 Degrees

Exec=aaphoto --rotate270 %f

Icon=image-x-applix-graphics

Be sure to save the updated aaphoto.desktop file when you are done editing it. Once saved, you should be able to right click on any file type listed in the MimeType line and instantly have access to your new image rotation menus.

Just as easily, you can continue to add other tasks that aaphoto is capable of performing to the aaphoto KDE service menu. For example, you can add commands that resize images to specific sizes (e.g., aaphoto --png -r600 %f to output a PNG file and resize the long side of the image to 600 pixels). Rather than hold your hand through that process, I’m going to leave the addition of the other aaphoto commands for you to discover and figure out on your own.

Summary

I have to admit to having some kind of “block” when it came to creating KDE service menus. I had looked at them several times before a lightbulb went on and the entire process “clicked” in my brain. As you can see, they really aren’t all that difficult. But now that you know how to use and create them, you can now go forth and customize your KDE service menus to your heart’s content. The only limitation is your own imagination.