- About Us
KDE's Konqueror is as multifunctional as a Swiss Army knife. It works as both a file manager and a Web browser, and you can enhance it even further by adding new commands to its repertoire by means of service menus. The new commands appear in Konqueror's context menu when you right-click a file. Here's how to create service menus, and some specific commands that you might want to use in them.
Service menus are specified with .desktop files. In KDE 3.5, global service menus (those available to all users) reside at /opt/kde3/share/apps/konqueror/servicemenus. If you're just creating a service menu for your own use, create the file in your specific KDE directory, which you can find out by using
kde-config --localprefix; in my case, it's /home/fkereki/.kde3/share/apps/konqueror/service.
You can pick any name for your service menu file, as long as it ends with .desktop. For instance, suppose you want to create a menu command to count words and lines in text files without opening a console window and using
wc. You can create count_words_and_lines.desktop in your KDE directory using any text editor.
A .desktop file has a specific structure. It includes a single Desktop Entry sections (see below) in key=value pairs. You can then specify several actions in Desktop Action entries; each action stands for a specific command you will run, and that will appear in the servicemenu. A minimal service menu looks like this:
[Desktop Entry] ServiceTypes= ... X-KDE-Submenu= ... Actions= ... [Desktop Action actionname] Name= ... Icon= ... Exec= ...
When you create a new menu, you first need to determine what kind of files it should apply to. To learn the correct description, open Konqueror, go to Settings -> Configure Konqueror, and pick the File Associations tab. In the tree at the right you will be able to browse through all valid descriptions. In my case, I wanted to use my service menu both for plain-text files and for HTML files. I edited the "ServiceTypes" line so it read
ServiceTypes= text/plain,text/html. (Spaces around the equals sign are optional.) You could also use
text/* to specify all text files;
all/all to specify all files, including directories;
all/allfiles to specify all files but no directories; and
inode/directory for directories.
Next, you need to organize the service menu. You can set your actions to appear directly on the right-click menu, or you can create a submenu for them. Just for the sake of illustration, I decided I wanted to have three commands (count words, count lines, and count words and lines), so I included
X-KDE-Submenu= Count...; if you don't want a submenu, omit that line. The ellipsis -- the three dots -- at the end of the description is standard for a menu entry that doesn't do anything itself but rather opens another menu. Each action needs its own name, and the name can be anything you wish, since it won't be displayed anywhere. I wished to have three actions, so after settling on appropriate names, I edited the Actions line to read
Actions= countLinesAndWords;countLines;countWords -- note the semicolons separating the action names.
For each action, you need a [Desktop Action actionname] section with three lines: Name, to set whatever text is going to be displayed in the menu; Icon, to specify what icon will be shown next to the menu entry (you can use any icon in /opt/kde3/share/icons, but don't include the final .png in its name); and Exec, to define the actual command that will be executed. For example, for the Count Lines and Words action, I initially wrote:
[Desktop Action countLinesAndWords] Name=Count lines and words Icon=background Exec=wc -l -w %U
But that failed roundly -- no output was shown! (I'll get to the reason later.) The Exec line shows the command that is to be executed. %U stands for "all selected files"; another popular option is
%u, which stands for a single filename. You can even find a few more, less used, options. You can execute any program you wish, and you can use DCOP (or D-Bus in KDE 4) commands. You can even execute several commands or scripts by writing something like
Exec=/bin/sh -c "a list of commands" or
For most commands, what we did would have worked, but whenever a command needs to produce some output (as in this case, obviously), there is a problem: you don't get it! Konqueror runs the command and just ignores its output, so the net effect is that words and lines are counted, but you don't learn the results.
The easiest way to see the output we want is to use KDialog, a neat little program included with KDE that can produce pop-up windows (and do much more) with any given text. (An alternative to KDialog would be Zenity, which is geared toward GTK+ rather than Qt, as used by KDE.) In my case, the solution was to write
Exec=kdialog --msgbox "`wc %u`". Note the use of backticks so the output of the wc command will appear as the quoted parameter of KDialog. Of course, you could get fancier and use other commands to whip wc's output into more appropriate shape, but I'll leave that as an exercise for you.
The final service menu, as I use it in my box, is then:
[Desktop Entry] ServiceTypes=text/html,text/plain Actions=countLinesAndWords;countLines;countWords X-KDE-Submenu=Count... [Desktop Action countLinesAndWords] Name=Count lines and words Icon=background Exec=kdialog --msgbox "`wc -l -w %U`" [Desktop Action countLines] Name=Count lines Icon=background Exec=kdialog --msgbox "`wc -l %U`" [Desktop Action countWords] Name=Count words Icon=background Exec=kdialog --msgbox "`wc -w %U`"
You can customize KDE in many ways to suit your needs. Service menus are easy to create and let you add your specific actions to the right-click menu.