Ubuntu – Setting different default applications for different Desktop Environments

default-programsdesktop-environmentsfile format

I am using Ubuntu 12.04 with default Unity interface. I installed later the KDE desktop, XFCE, LXDE, gnome-shell and Cinnamon.

The KDE comes with different default applications than Unity, such as

  • kwrite for text editing,
  • konsole as virtual terminal,
  • kfontview for font viewing and installing,
  • dolphin as File browser etc.

Other DE come with some other default applications.

The problem arises when you want to open a file such as a text file, with which can both be opened by gedit and kwrite, I want to use kwrite on KDE and gedit on Unity or Gnome. But, there is no way to set like this. I can set default application for text file by changing respective settings in both KDE and Unity, but It become default for both DE.

For example, If I set kfontviewer as default font viewing application in KDE, it also opens fonts when I am in Unity or Gnome and vice versa. This is a problem because, loading other DE's program takes long time than the default one for the used DE.

My question is: Can I use different default applications for different DE? How?

Best Answer

I've put together this solution to resolve your issue, and tested it on KDE and XFCE with opening text files and font files. It is a generic solution that should be applicable to any number of desktop environments and mime types. The way it works is there is a simple python script called custom-open that will open a file using different applications for different desktop environments. These are the steps to setup the solution:

  1. save custom-open script on your computer preferably on your path but doesn't have to be.
  2. save .custom-open.ini in your home directory ~/.custom-open.ini
  3. set custom-open as the default application for any file types you want handled by it.

custom-open

#!/usr/bin/env python
import traceback, sys, os, ConfigParser, os.path
from subprocess import Popen, check_output
from gtk import MessageDialog, MESSAGE_ERROR, BUTTONS_CLOSE

try:
    file, desktop = sys.argv[1], os.environ['DESKTOP_SESSION']
    mime = check_output(['file', '--mime-type', file]).strip().split(': ')[1]
    config = ConfigParser.RawConfigParser()
    config.read(os.path.expanduser('~/.custom-open.ini'))
    cmd = config.get(desktop, mime)
    Popen([cmd] + sys.argv[1:])
except:
    msg = "CUSTOM OPEN ERROR\n\n" + traceback.format_exc()
    MessageDialog(None, 0, MESSAGE_ERROR, BUTTONS_CLOSE, msg).run()

.custom-open.ini

[gnome]
text/plain = gedit
application/x-font-ttf = gnome-font-viewer

[xubuntu]
text/plain = leafpad
application/x-font-ttf = gnome-font-viewer

[kde-plasma]
text/plain = kate
application/x-font-ttf = kfontview

so what's great about this solution is that you can add as many new desktop environments as you want and as many mime type you want. to check what name you should provide for the desktop environment run the below command in a terminal.

env | grep -i DESKTOP_SESSION

to check the exact mime type of a file just run:

file --mime-type filename

EDITED: The need for symbolic links has been removed this should make it easier to use. I've also added a graphical error handler that will bring up an alert if an error occurs.