9

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?

1

2 Answers 2

5
+100

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.

10
  • 1
    Thank you for your answer. I followed your steps and created all the according scripts, links and files. Your solution works great form the command line and performs as advertised. But when I try to set the script (or rather the symbolic link) as the default application to open a specific file type, it fails and falls back to the secondary default. I don't know, if I am doing something wrong. Oct 5, 2012 at 4:24
  • Here's how I defined the script as the default app: 1.) create a launcher (co.desktop) with Exec=/home/user/.scripts/Customization/Defaultapps/custom-open-text in ~/.local/share/applications. 2.) Edit ./local/share/applications/mimeapps.list and add the following default association: text/plain=co.desktop. Find any mistakes in this procedure? Oct 5, 2012 at 4:26
  • 1
    ok try out the new version, I made some enhancements to make it easier to use. I've also added the graphical error handler so now it should popup an alert with the error message. Try again and post the error message in a comment if it still doesn't work. Also could you mention what version of Ubuntu and which desktop environment you tried out. Oct 5, 2012 at 17:05
  • It works and it does so fantastically (using LXDE/Unity)! Your solution deserves every single last point of this bounty. Thank you very much for taking the time to do this. (P.S.: I still think a function like this should come with Ubuntu by default. DE-hopping might not be popular practice but it's common enough to warrant an official solution.) Oct 5, 2012 at 17:15
  • 1
    I am sorry, but I am not the OP, I merely set the bounty up. @Olive Twist will have to mark his own question as answered. Oct 6, 2012 at 3:50
2

If you don't find the "right" way to do it, you could use a brute force method something like:

  1. Set all the defaults in one desktop environment
  2. Do something like an cd $HOME;ls -Rlrt | less to find out what files were just updated - i.e where those choices were saved
  3. Make a separate copy of those files for each desktop environment (backed up).
  4. Repeat for each desktop environment
  5. Copy the appropriate set into place just before switching to that desktop environment (probably by logging out and logging back in and selecting the new desktop environment).

Hopefully, there's a way to switch desktop environments from a shell script with the option of telling the desktop environment where to get it's configuration files from. That might be cleaner than moving files around all the time, but I haven't researched if or how that works.

Another, much simpler solution, but not exactly what you asked for, would be to just:

  1. Create separate user accounts for each desktop environment
  2. Create a new group for these users
  3. Add all these users to the new group
  4. Make any common data files you need to access from all these users -rwxrwxr-x (chmod 775 list-of-files-and-directories). That way you won't get permission errors trying to write to a file that another one of your "users" wrote to first.

    • If you want some files to be the same across all these users, you can create them in one user and symlink them to the rest of them. This should work, but will require you to remember they're set up that way when you change any of them. For instance, if you edit one of them and your editor makes a backup file, that backup file will only be saved where you edited it, not on all the other user accounts.
1
  • You might note where per user file associations are stored ~/.local/share/mime/ Sep 15, 2012 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .