This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Desktop Software

Do-it-yourself Konqueror commands

By Federico Kereki on September 25, 2008 (7:00:00 PM)

Share    Print    Comments   

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= ...

Creating a menu

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 Exec=/bin/sh aScriptOfYourOwn.

Getting some feedback, or KDialog to the rescue!

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.

Federico Kereki is an Uruguayan systems engineer with more than 20 years' experience developing systems, doing consulting work, and teaching at universities.

Share    Print    Comments   

Comments

on Do-it-yourself Konqueror commands

Note: Comments are owned by the poster. We are not responsible for their content.

Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 82.158.26.94] on September 25, 2008 08:05 PM
As a file manager I prefer Krusader. It's faster and more stable than Konqueror or Dolphin (in KDE4.1.)

#

Re: Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 70.89.188.29] on September 25, 2008 08:38 PM
Then this article isn't much use to you.

#

Re(1): Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 84.195.15.220] on September 26, 2008 07:09 PM
I'm a Krusader-user myself and konqueror-service-menus do perfectly work in this excellent file-manager !

#

Re(1): Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 89.226.115.118] on September 28, 2008 06:37 PM
But why ? Your comment is of no use.

#

There is a service menu repository

Posted by: Anonymous [ip: 24.253.108.249] on September 25, 2008 08:51 PM
At kde-look.org/kde-apps.org there is a repository of service menus for just about anything you can think of. These can be used as-is, or customized. Or, they can be the spark for your own ideas.

#

Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 169.198.254.6] on September 26, 2008 04:58 PM

Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 71.188.236.146] on September 27, 2008 04:36 PM
Oh, I am bookmarking the hell out of this!

#

Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 213.47.44.9] on September 28, 2008 01:52 PM
Speaking of paths /opt/kde3 seems to be the author's installation prefix for KDE3.

Generally the list of available paths can always be determined by

% kde-config --path data

and use any of them as the base for service menus' relative paths konqueror/service/yourmenu.desktop

#

Very handy tips here

Posted by: Anonymous [ip: 151.188.247.104] on September 30, 2008 03:17 AM
OK, now this is quite useful. It's one of those "hey, here's a cool handy tool" articles much like last year's KIOSlaves article. After reading the KIOSlaves article, I started using them all the time, and the same will apply to this one. Thanks, Federico!

Not that GNOME sucks (it doesn't, it's actually pretty good), but KDE just rocks the house, especially Konqueror, for easily accessible stuff like this. How cool!

--SYG

#

Do-it-yourself Konqueror commands

Posted by: Anonymous [ip: 91.124.142.24] on October 09, 2008 09:27 AM
"
Good article. Here are a couple of other useful references on this topic:
http://developer.kde.org/documentation/tutorials/dot/servicemenus.html
http://legroom.net/2007/04/20/adding-custom-actions-kde-context-menus
"
Thanks for links!

<a href=" http://petmeds.somee.com/ ">heartgard for dogs</a>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya