Launching the right

Revision History
Revision 1.02006-03-04

First Version.

Revision 1.12006-04-16

Graeme Thomson wrote an email how open works on the Mac.

Revision 1.22006-08-17

Charles Kerr suggested to use rundll on Windows, I added a link to the Portland project

Revision 1.32008-01-29

Christian Kaiser explained Gnome.


Table of Contents

About this howto
1. Default applications
KDE
Gnome
Mac OS X
Windows
2. Launching the application
KDE
Gnome
Mac OS X
Windows
3. Specialities
Debian
A. Links
B. Version and author

About this howto

Many programs contains some lines of code that starts the web browser with the application's homepage, open a different program with a generated file or just have a mailto: link in the about dialog box opening the user's preferred mail program with the address of the apoplication programmer.

Doing this while respecting the user's preferences is not as easy as it seems: Perhaps there are three different browsers installed on the system the application runs - which one shall be opened? Just making a list like (if browser cannot be launced: try next browser) will choose the right one for a good part of the users, but you don't get 100% satisfaction.

This howto tries to explain how to launch the preferred program, independent of the operating system or desktop environment.

Note

Always keep in mind that the list of programs you know is incomplete, and tomorrow there will people wanting to use a program that wasn't there at the time of programming. So never rely on a hard-coded list of programs the first way, but try to use the native default application launching system.

Chapter 1. Default applications

Table of Contents

KDE
Gnome
Mac OS X
Windows

Every operating system has its own way to store preferences about associations of file types with programs. Mostly this is done by storing a list of MIME types linked with the programs that are able to open them, some use the file extensions instead. On nearly every system you are allowed to have multiple apps linked to a file type, and a preference which to launch first.

KDE

KDE allows you to associate applications with certains programs by using Konqueror/Settings menu/Konqueor settings/File associations. Search for a MIME type and edit the list of file extensions, the programs to be launched and their order, as well as the programs to use for embedding the file into the konqueror file view. Further, you can right-click on any file in Konqueror and have a "open with" submenu.

The preferences are stored locally in ~/.kde/share/mimelnk/*/*.desktop desktop files.

Global settings can be found in /usr/kde/major.minor/share/mimelnk/*/*.desktop.

Gnome

Gnome splits the settings for default application in system-wide and user specific settings. System wide application assignments can be found in /etc/gnome/defaults.list. User specific files are located in ~/.local/share/applications. If you make changes to this files execute 'update-desktop-database' as user root to apply changes.

Using the file browser Nautilus makes the process easy:

  • Select a file and choose Properties.

  • Go to tab "Open with"

  • Choose a gnome suggested program from list or add a new one by using the Add button.

  • The Program, whose radio box is marked, will be the default application for that file type.

Mac OS X

FIXME: I don't have a mac

Windows

Open the file explorer, and in the Extras menu you have "folder options", the tab "File types" gives you a list with all known file extensions and their respective description. You are allowed to associate one program with a file type for opening.

The settings are stored in the windows registry under the key HKEY_CLASSES_ROOT\.extension. The "Standard" key's value is most times "extensionfile" and means that the settings of the key HKEY_CLASSES_ROOT\extensionfile shall be used. That allows to associate multiple file extensions with the same settings, but is a step too much if you have just one extension.

Chapter 2. Launching the application

Table of Contents

KDE
Gnome
Mac OS X
Windows

With the knowledge of where the file type assocication settings are stored, you could write the code that chooses the right program and launches it.

Fortunately, all systems have either libraries whose methods you can use, and command line programs that do that for you. Using the command line tools is the most convenient method if you don't program for a specific desktop environment or operating system, but have a cross-plattform application written in Java, PHP, Python or Ruby.

Note that you should always use quotes around the filename, as files with spaces in the path will not be launched otherwise.

For running apps in Linux desktop environments, have a look at the Portland project.

KDE

KDE provides the program "kfmclient" tool for working with files. It allows you not only to launch the default app for a given file, but also specify that a specific app should be launched for the file and e.g. launch the file association dialog.

To launch the associated application and cause it to load the file, run the following command:

kfmclient exec filename

The application is automatically launched detached in the background, so that the command immediately returns.

You can detect a running KDE environment by checking the KDE_FULL_SESSION environment variable. It is set to "true" (string) if KDE is running.

Gnome

Use the following command:

gnome-open filename

This program also launches the application in the background and returns immediately.

Gnome can be detected by checking the GNOME_DESKTOP_SESSION_ID environment variable.

Mac OS X

open [-a application] filename

Just calling "open filename" will run the default application for the file type. Passing the -a parameter with the application to start will open the file in it, e.g.

open -a textedit.app filename

Windows

start "" filename

Note

It has been reported once that using start didn't work on a computer, but the rundll thing did.

If you want to wait until the application has been terminated, pass "/WAIT" as parameter before the filename.

To open URLs, you also can use the following call:

rundll32 url.dll,FileProtocolHandler filename

Chapter 3. Specialities

Table of Contents

Debian

Here are some special ways to launch something.

Debian

Debian systems allow to specify certain programs in a system-wide config file and launch them with sensible-* commands.

For example, the default browser can be launched with

sensible-browser

and the url as optional parameter. It basically checks the BROWSER environment variable, splits it by colon ":" and tries to execute any of that. If that doesn't work, it tries to execute /usr/bin/x-www-browser (if a DISPLAY is set) and /usr/bin/www-browser if not.

There are other sensible-* programs as sensible-editor and sensible-pager.

Warning

The sensible-* commands are meant to be used from command line. That means that e.g. lynx could be opened, something that you won't want in a graphical environment - as it doesn't open it's own window but relies on the assumption that it is started in a shell.

Appendix A. Links

  • Freedesktop.org desktop entry specification: http://freedesktop.org/wiki/Standards_2fdesktop_2dentry_2dspec

  • The Portland project provides command line tools to run programs independent of the desktop environment on Linux.

Appendix B. Version and author

This howto has been written by Christian Weiske and is available under a Creative Commons license at cweiske.de.

If you have questions, comments or corrections, feel free to write a mail to cweiske@cweiske.de.