Add Custom Actions to Your KDE Konqueror Pop-up Menus

How to set up Konqueror so that you can right-click on an icon for a Word Document and view that document as HTML. [This article initially appeared in TUX, issue 9.]

by Mango Parfait

In the August 2005 issue of TUX, I said, "You can customize KDE so that you can right-click on a .vbs file and choose Actions->Compile, which launches a compiler specifically for Visual Basic Script. This option will not show up for other types of files. You cannot do the same thing in GNOME. I can explain how to do all this in KDE. Maybe I will explain in a future issue."

This is the future issue. I am not going to show you how to compile scripts. I am going to show you how to customize KDE so you can right-click on a Microsoft Word document and view the document in a browser as HTML.

I am going to tell you that, but this article is not really about how to view Microsoft Word documents as HTML Web pages. This article is about how to make your own custom actions for KDE Konqueror. Maybe you do something many times a day. Maybe you do something like move documents to the same place or convert one file into another type of file. You can use this trick to make it easy in KDE.

I use this ability to view Word documents as HTML as an example of what you can do. I hope you will learn from the example so you can create your own custom menu actions. Most of what I will tell you is easy. The hard part is learning how to make commands like the one we will use to change Microsoft Word documents to HTML. If you decide to use this trick but do not know how to create the command for what you want to do, maybe you can get a geek friend to help you.

Once upon a Time

Here, maybe this makes more sense if I make up a story. You have many Word documents in your folders. You tell your geek friend that you want to look at these documents many times, but you do not want to open them in a word processor. You don't want to make changes. You don't want to wait for a word processor to load. She says, "First download this package. It is called wv or wvWare. It may be called something else for your distribution. Here is what it looks like in the Kubuntu 5.10 Breezy Badger Adept package manager." (See Figure 1.)

Figure 1. Install wv using the Kubuntu Adept package manager.

So you start the Adept package manager. You search for wv. You find the package. You click the Install button. You click the Commit Changes button in the toolbar. Now you have the wv package installed.

Then your geeky friend tells you, "Type this command to create a temporary directory in your home directory. You need this because sometimes when you convert Word documents to HTML you create a lot of files. You do not want to clutter up your directory with these files":

$ mkdir ~/tmp

She says, "Now, here is the command to convert the Word document to HTML":

$ wvHtml --targetdir=$HOME/tmp  temp.html

She continues, "Now open up the tmp folder in Konqueror, and then click on the file temp.html. This is how you can view the Word document as HTML."

You say, "This is too much trouble. I do this many times a day. I do not want to type commands. I want to open the folder, click on the icon for the Word document and tell Konqueror to show me the document as HTML. I do not know how to make the command work. You tell me the command and I will put it in the right place and use it. I know how to do this because I read the article by Mango Parfait."

The Easy Way
Okay, so here is how to do all the work one time and then get what you want, which is a point-and-click way to view the Microsoft Word document in your browser as HTML.

First, you need to install the wv package (or whatever it is called for your distribution). See the above example for how to install this with Kubuntu Breezy Badger. If you are not using Kubuntu Breezy Badger, you need to figure out how to find this package and install it. If I tell you the steps for every distribution, this article will be too many pages long.

You should create a temporary directory like your geeky friend says. You can do this without typing a command. Open up your home folder. Right-click on an empty space and select Create New->Folder from the pop-up menu. Name this new folder tmp.

Now we are going to teach KDE how to show you the Word document in HTML just by pointing and clicking.

Start your favorite text editor. My favorite text editor is Kate. I will tell you what to do in Kate, and you can figure out how to do the same thing with your editor if you do not like Kate.

Type the following text in your editor. Be very careful to type it exactly this way. This is the most important part of the trick:

[Desktop Entry]
ServiceTypes=application/msword
Actions=ViewDocAsHTML

[Desktop Action ViewDocAsHTML]
Name=View Word Doc as HTML
Exec=wvHtml --targetdir=$HOME/tmp %f temp.html; konqueror ~/tmp/temp.html

If you have a little knowledge, you will wonder why I use $HOME in one place and ~ in another place. They both mean the same thing, right? If you do not know this, I can tell you that this is right. They both mean the same thing. Both of these point to your home folder. But wvHtml does not understand the ~ symbol. So I have to use $HOME instead. I can use $HOME later, too, but I am lazy. The symbol ~ is one character. The name $HOME is more than one character. I take the easy way when it works and use ~.

Are you sure you typed everything right? Good. Now you must save this text file. It is important to save it in the right place. So select File->Save As from the editor menu. You will see a file save dialog. You are going to save this file in a folder that probably does not yet exist on your computer. Here is the folder you need to use (be sure to change mangoparfait to your user name):

/home/mangoparfait/.kde/share/apps/konqueror/servicemenus

But the servicemenus folder is probably not there. So here is how you can make this folder exist. Type this as the file save location (change mangoparfait to the name of your home directory):

/home/mangoparfait/.kde/share/apps/konqueror/

See Figure 2 for an example of how it looks when you use Kate.

Figure 2. Where to Navigate First with Save As

Now we must create the new folder. There is more than one way to do this. An easy way is to click on the folder icon with the star on it. This is the new folder icon. You will see a pop-up dialog. Type the name servicemenus in the dialog like you see in Figure 3 and click the OK button.

Figure 3. Name the new folder servicemenus.

This creates the folder and changes to that folder when you use Kate. So now you are ready to save the file. Save this file with the name view-doc-as-html.desktop. See Figure 4 for an example.

Figure 4. Save this file as view-doc-as-html.desktop.

Type the right name and then click the Save button.

You are done! Open up a folder where you have Microsoft Word documents. Right-click on the icon for a document. Now select Actions from the pop-up menu. You should see a menu selection called View Word Doc as HTML. So select Actions->View Word Doc as HTML from the pop-up menu. You should see a new Konqueror window open, and it will display the Word doc as an HTML file.

How It Works

The servicemenus folder you made earlier is a special folder. If you put the right kind of files in this folder, it will add actions to the pop-up menu for Konqueror when you right-click on an icon. The file we put there is view-doc-as-html.desktop. Do you see that it contains these lines?

[Desktop Entry]
ServiceTypes=application/msword

The first line tells Konqueror what this file is for. Do not worry about it, just make sure you start with this line [Desktop Entry] when you make your own custom actions.

The second line tells Konqueror that this action is for Microsoft Word documents. Right-click on an icon for some other kind of file. Try a text file or a graphics image. Then select Actions from the pop-up menu. Do you see View Word Doc as HTML there? No you do not. Why? Because we told Konqueror that this action is only for Microsoft Word documents. If you read my words above that I said in the August 2005 issue of TUX, you will see that this is the difference between GNOME and KDE. You can create custom menu actions in GNOME, but they will pop up no matter what file you pick. GNOME is not smart enough to know that one action is for one file and another action is for another file.

Now look at the next line:

Actions=ViewDocAsHTML

This tells Konqueror where to look in this file for the custom action. You can have more than one action here, but I try to keep this simple for you. This points to the heading that comes next:

[Desktop Action ViewDocAsHTML]
Name=View Word Doc as HTML

Konqueror will look here for the action. It will also see that the name is View Word Doc as HTML. This name is what you see in the menu.

Now here is the hard part. This is the action or command. It is actually two commands separated by a semi-colon:

Exec=wvHtml --targetdir=$HOME/tmp %f temp.html; konqueror ~/tmp/temp.html

The wvHtml command converts a Microsoft Word document into an HTML document. It is not a very smart program, so we need to tell it some things. We need to tell it what directory to use. That is why we have the targetdir part of the command.

Konqueror will replace the symbol %f with the name of the file you have selected. This is the input file for wvHtml. Then wvHtml will create the output file temp.html in the directory we told wvHtml to use.

There are other symbols you can use. One example is %F. Konqueror will let you select many icons in a folder and it will send all the filenames to the place where you put %F. Be careful if you try this. Many programs do not know what to do with many filenames at once and your trick will not work. Here is a list of some of the symbols for you:

  • %f--the file you have selected.
  • %F--multiple files you have selected.
  • %u--a URL (like http://www.tuxmagazine.com).
  • %U--more than one URL.
  • %d--this is the directory for the file you have selected (%f).

Now look at the command line again. Next we have a semi-colon. This tells Konqueror that we are done with the first command and we want to start another command. This new command opens up Konqueror and tells it to load the temporary HTML file. This is the easiest way I know how to make it show you the file in HTML. I know harder ways. I know how to make the file show up in the folder window where you have the Word document. But that is too much to learn for one day, so I keep it simple.

Like I say above, most of this is easy. The hard part is the command. If you can do the rest by yourself, ask a geeky friend to tell you what command you can put on the line that starts with Exec=. Now you know how to make custom actions for Konqueror and I told you in this future issue, just like I said.

About the Author

I am a sweet, humble, delicate and very cute genius who is at your service to answer your Linux questions. Send your questions to mango@tuxmagazine.com.

Web Editor - Wed, 2005-12-07 01:02.
Categories: