7

How can I get a list of applications that are capable to open a specific filetype / MIME-type? I'm searching for a desktop-environment independent solution on Linux.

I found the possibilty to get the MIME type for a file.:

~> xdg-mime query filetype test.svg 
image/svg+xml

Then I could query for the default application for that MIME type.:

~> xdg-mime query default image/svg+xml
eog.desktop

Is there also a solution to get a list of programs (not default) that I can use for that file?

For example on a GNOME desktop, if I choose open with another application for *.json file, I can see three (Atom, Gedit, Builder) applications that are recommended for opening the file.:

GNOME dialog open with another application

If I choose, show all applications, I can also see a further associated application (LibreOffice Writer).:

GNOME dialog show all applications

I found the file /home/user/.config/mimeapps.list which has content like:

[Added Associations]
text/html=atom.desktop;firefox.desktop;midori.desktop;org.gnome.gedit.desktop;brackets.desktop;
application/javascript=atom.desktop;org.gnome.Builder.desktop;org.gnome.gedit.desktop;

and also has associated applications for a mime type, but I can not find a global mimeapps.list file which is mentioned in the Arch Linux wiki.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
  • Does the command "file -i " help.? – crafter Aug 17 '15 at 21:07
  • I don't know. As far as I can see, I get a wrong MIME type a SVG file `file -i test.svg test.svg: text/html; charset=us-ascii` Also I don't know, how to get the desired applications list. – BuZZ-dEE Aug 17 '15 at 21:13
  • OK, I see what you want to do now. Try looking at this file : /usr/share/applications/defaults.list (on Debian/Ubuntu for me). – crafter Aug 17 '15 at 22:02
  • I also want applications that are not default. Those applications are not in that file. – BuZZ-dEE Aug 18 '15 at 07:35

2 Answers2

6

What GNOME most likely does is parsing all .desktop files and looking for these that declare support for requested MIME type. This is the only certain way of solving your problem. With proper parsing libraries in place and lower-level language, this should be relatively fast operation. Additionally, they may put some cache files to speed things up further.

But if "certain" is not required and "probably" is good enough for you, then all MIME types and .desktop files associated with them are stored in mimeinfo.cache files. I am not sure what is the actual guarantee of that file and maybe I am using it the wrong way, but following function seems to work just fine

#!/bin/bash

xdg-all-apps() {
    LOCAL="${XDG_DATA_HOME:-$HOME/.local/share}/applications/mimeinfo.cache"
    GLOBAL="/usr/share/applications/mimeinfo.cache"

    MATCHING="$(grep -h "$1" "$LOCAL" "$GLOBAL")"
    if [ -z "$MATCHING" ]; then
        echo "There are no application associated with $1"
        return
    fi
    echo "$MATCHING" |cut -d = -f 2 |\
        sed -z -e 's:\n::;s:;:\n:g' |\
        sort |uniq
}

xdg-all-apps text/plain
xdg-all-apps audio/mpeg
xdg-all-apps image/svg+xml
xdg-all-apps application/json

On my system, running that code generates following output:

gvim.desktop
kde4-kate.desktop
kde4-kwrite.desktop
kde4-okularApplication_txt.desktop
kwrite-usercreated.desktop
libreoffice-writer.desktop
vim.desktop

easytag.desktop
smplayer.desktop
smplayer_enqueue.desktop
vlc.desktop

gimp.desktop
inkscape.desktop
kde4-kolourpaint.desktop
midori.desktop
There are no application associated with application/json

As you can see, some applications provide more than one desktop file (smplayer.desktop and smplayer_enqueue.desktop). These functional duplicates could be removed, but that isn't trivial.

But please note that some desktops ignore XDG altogether. If you want really cross-desktop way, you should put mailcap files somewhere in the mix. I strongly believe that GNOME actually ignores it.

Mirek Długosz
  • 4,205
  • 3
  • 24
  • 41
  • For `application/json` I only get `org.gnome.Builder.desktop`, but there is also Atom and Gedit, if you look to the above screenshot. – BuZZ-dEE Sep 29 '15 at 18:29
  • 1
    What about `text/javascript` and `text/plain`? It seems that [JSON MIME type wasn't standardized in the past](http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type), so maybe GNOME falls back to different types to handle these files. You would have to find .desktop files of Atom and Gedit and see what MIME types they declare they do support. Or, equally likely, my solution is not good enough after all. – Mirek Długosz Sep 30 '15 at 12:19
3

Instead of writing your own script as suggested by @MirosławZalewski, consider using the perl-file-mimeinfo tool (ArchWiki link).

perl-file-mimeinfo provides the tools mimeopen and mimetype. These have a slightly nicer interface than their xdg-utils equivalents:

# determine a file's MIME type
$ mimetype photo.jpeg
photo.jpeg: image/jpeg

# choose the default application for this file
$ mimeopen -d photo.jpeg
Please choose an application

    1) Feh (feh)
    2) GNU Image Manipulation Program (gimp)
    3) Pinta (pinta)

use application #

# open a file with its default application
$ mimeopen -n photo.jpeg

The -d option, long option --ask-default, lets the user choose a new default program for given files.
The -n option, long option --no-ask, does not ask the user which program to use, it chooses the default program or the first program known to handle the file mimetype.

In Fedora, this package is called perl-File-MimeInfo.
In Debian and Ubuntu, it is called libfile-mimeinfo-perl.

Bob
  • 196
  • 3
  • 10