We've moved!

TechKnack.blogspot.com has officially moved to TechKnack.net. You should be redirected in 3-5 seconds. Thank you.

March 2, 2008

Add entries to Konqueror's "Create new" menu

Add this post to Del.icio.us. Del.icio.us (0 saved)

This has been a point of frustration for me since I first wanted to add "Create new .doc file" to Konqueror's context menu. To the best of my knowledge, there is no GUI method of adding filetypes to Konqueror's "Create new" menu, and there is little or no online documentation that details how to go about adding filetypes, or even how the system works. Grr.

The good news is that it is possible, even easy, to add filetypes to Konqueror's "Create new" menu. Best of all, it only requires adding a couple of files to an existing directory.

The entries in the "Create new" menu are actually called "templates". Which makes sense, since the newly created file is nothing more than a blank template to which you add content. The files that make up the menu entries are .desktop files located at /usr/share/templates/. The .desktop files point to template files located at /usr/share/templates/.sources, a hidden directory inside the templates directory. For my example, I'm going to add a .doc file template, since it is so often used in school. The basic idea can be applied to any file type, though.

First, open the program which can create the file you want to add (in this case, OpenOffice Writer). Create a new, blank document, and "Save as" the appropriate file (be sure to choose the .doc extension, not .odf) in an easily-accessible place (meaning user-accessible, a good place would be your desktop). I believe the name of the template file is used as the default name of the new document, though it will ask you for a filename before creating the file; I simply named my file "MS Word 2003 Doc.doc". After the template is created, fire up a command line and use

sudo mv "~/Desktop/MS Word 2003 Doc.doc" /usr/share/templates/.sources/

to move the template into its new permanent residence. Now go up a level to /usr/share/templates/. You will need root privileges of some sort (either a konqueror instance running under root, or sudoing at the command line). Copy one of the existing .desktop files (I recommend a filetype-specific file, such as the HTMLFile.desktop or TextFile.desktop files) to something like MSDoc.desktop. Open the new file for editing with root privileges (from a command line,

kdesu kate /usr/share/templates/MSDoc.desktop &

). Now you can either delete all the nonessential lines of foreign language code (keeping one line of each of Encoding, Name, Comment, Type, URL, and Icon instructions), and edit the remainder to fit your preferences, or you can copy my file's contents and paste it into your file:

[Desktop Entry] Encoding=UTF-8 Name=MS Word 2003 Document... Comment=Enter Document name: Type=Link URL=.source/MS Word 2003 Doc.doc Icon=document

I leave the Encoding line as-is from the HTMLFile.desktop file I copied it from. Name is the text that will show up in the menu. Comment is the text that will prompt for the new file's name. Type=Link specifies that this file is a link to another file (leave as is). URL points to the template file (which resides in the .source directory). Icon specifies what icon will be used in to identify the file type in the "Create new" menu. For Icon, "document" gives the file the icon associated with document files. The Icon for TextFile.desktop is "txt", and for HTMLFile.desktop, "html". I don't know how to go about finding the correct word form other filetypes, other than experimentation. A couple that seem to work for me include "spreadsheet" for a spreadsheet icon, "openofficeorg23-oasis-presentation" for the Oasis icon for OO Impress. One thing to try would be to open KControl, go to KDE Components -> File Associations, click on any mime type, and click the filetype's icon on the right. You should be taken to the Icon chooser. Select "Mime types" from the dropdown, and search for portions of keywords that match the icon you're looking for. Do not click any icons. To get the name of an icon, hover over the icon, and the chooser should give you a tooltip with the icon's name. Use that name in the .desktop file, and it should present that icon in the "Create new" menu.

If you followed everything correctly, you should now have an entry in Konqueror's "Create new" menu for creating a new .doc file. To recap:

  1. Create a blank template file
  2. Move the template file to /usr/share/templates/.source/
  3. Create a .desktop file in /usr/share/templates/ with the appropriate values
  4. Enjoy your new file-creation capabilities!

To extend functionality, you can, obviously, add template content to your template files. For example, if you develop websites and you're tired of always looking up the correct doctype declaration, you can build an html template file, complete with doctype, and simply create it from the Konqueror context menu. In fact, the existing HTMLFile.html file in my .source directory already has the basic structure of a standard web page. Of course, this isjust an example, and could be applied to other filetypes.

1 comment:

Anonymous said...

Thank you Andrew,
Your trick was what I 've been looking for since a lot.
About icons I discovered it's very simple.
It's enough to right click on the .desktop file in the template directory and choose "property". Once there, click on the icon to find the right one. This GUI mode will change the right parameter in the .desktop file.

Thx again

Francesco loriga