Get a list of applications to open a specific file

How can I get a list of applications that can open a specific type of file / MIME type? I am looking for a desktop independent Linux solution.

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

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

Then I could request a default application for this MIME type .:

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

Is there a solution for getting a list of programs (not the default) that I can use for this file?

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

GNOME dialog is open with another application

If I choose, show all applications, I can also see the following related application (LibreOffice Writer) .:

GNOME dialog shows all applications

I found the file /home/user/.config/mimeapps.list , which has content such as:

 [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 related applications for the mime type, but I cannot find the global mimeapps.list file, which is mentioned in the Arch Linux wiki .

+5
source share
2 answers

GNOME most likely parses all .desktop files and searches for them that declare support for the requested MIME type. This is the only way to solve your problem. With appropriate parsing libraries and a lower level language, this should be a relatively quick operation. In addition, they can put some cache files to speed things up.

But if "specific" is not required and "probably" enough for you, then all the MIME files and the .desktop files associated with them are stored in mimeinfo.cache files. I am not sure what the actual guarantee of this file is, and maybe I am using it incorrectly, but the following function works 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 this code leads to the 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 can be removed, but this is not trivial.

But note that some desktop computers generally ignore XDG. If you want a truly cross-desktop way, you have to put the mailcap files somewhere in the mix. I strongly believe that GNOME actually ignores it.

+5
source

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

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

 # determine a file 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 , the long option --ask-default , allows the user to select a new default program for the given files.
The -n option, the long option --no-ask , does not ask the user which program to use, it selects the default program or the first program known to process the mimetype file.

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

0
source

All Articles