Tuesday, February 28, 2012

How to set default apps (aka how to use xdg-mime)

How to set default apps in Chromium / Chrome / Firefox / Ubuntu Linux

Situation: You hate the default app that opens when you double click a file or when you open it with chromium browser and you want to change it.

a) In Unity (and probably Gnome 3 Shell), some default apps can be changed through gnome-control-center: System settings > System info > Default applications

b) You can also right-click on a file > select Properties > Open with. See what applications/programs are listed there. You can also add them and set them as default.

c) If none of the above work, you can use xdg-utils (xdg-mime) to set a default application for a specific Mime Type

First, you have to figure out what text string to use as a "mime type". An easy way to find it is to look into the default application's desktop file.

Example: Chromium opens magnet links and bittorrent files with transmission-gtk by default. The .desktop files are usually in folder /usr/share/applications/. We use "cat" command to look into the file /usr/share/applications/transmission-gtk (a line that starts with "MimeType"):

$ cat /usr/share/applications/transmission-gtk.desktop
[Desktop Entry]
Exec=transmission-gtk %U

Bingo! x-scheme-handler/magnet and application/x-bittorrent are the mime types we're after.

How to use xdg-mime command? Simple: xdg-mime default myapp.desktop mymimetype

..where we substitute myapp.desktop and mymimetype with the values from above.

Following the example above, we want magnet links and bittorrent files to open with Deluge (deluge-gtk) application. First you have to install the deluge-gtk package (otherwise the .desktop file won't exist). Then you have to execute:

xdg-mime default deluge.desktop x-scheme-handler/magnet
xdg-mime default deluge.desktop application/x-bittorrent
grep -i deluge ~/.local/share/applications/*

The last command (grep) should return "application/x-bittorrent=deluge.desktop" and the other mime type for magnet. Otherwise, something went wrong (maybe the desktop file didn't exist?).

And that's about it! Close and open chromium-browser again (or logout/login).

For the sake of documentation, the files that have to do with mime types and default apps are:



Anonymous said...

Really cool. But is there any fast way to define a single program for ALL audio/* mimetypes?

Anonymous said...

I have no idea... have you actually tried audio/* ?:)

k said...

This wouldn't really work in all cases, because there are countless audio/* MIME-types, files of which your average audio player doesn't play well. If you want a particular player to be the default for the files it can play, do as above, examine its desktop file for MIME-types it can play and set it as default for those.