4

I'm creating a GUI program with python and PyQt5, I want to create an "Open with:" option for it. How do I get a list of all installed software that can run a specific file type? In this case the file type would be image.

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
オパラ
  • 317
  • 2
  • 10
  • 1
    It would depend on what flavour of Linux you are using and also (probably) on the Desktop Environment you are using - but you should be able to find something in `/usr/share/applications/`. See if this helps - https://askubuntu.com/questions/162612/how-can-i-add-an-application-to-the-list-of-open-with-applications – Mortz Sep 22 '21 at 08:12

2 Answers2

1

The MIME type of the file

Are you able to figure out or specify the mime-type of the file(s) in question?

Besides a more approximate hint like filename extension (mostly used on Windows) the MIME type is the association to find a program capable opening the file (and its content).

You could use some Python package to figure out the MIME type of a file, e.g.:

Find programs associated with certain MIME types (Linux)

If you are looking for a solution on Linux only, I would recommend:

Or try to collect the MIME related info stored in Desktop entry files (.desktop) commonly in folder /usr/share/applications/, as commented by Mortz. As these entries are specified by Freedesktop.org also abbreviated as "XDG" they should be pretty independent from the Linux flavor or desktop-environment.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • Thank you for the answer. It seems like `XDG-mime` is only for either getting the default program for a file type, or for getting the default program for a file. I'm looking for a way to get a list of programs (not just the default) for a file type (not a file). However, I found out about `/etc/mailcap` in another SO post which seems to have exactly what I am looking for; a list of installed programs to run a specific file type. – オパラ Sep 22 '21 at 08:55
0

Going through some other SO posts based on the answers on this post, I happened across the mailcap library in python. It was basically what I needed, but not quite. Then finally, though my GUI is made with PyQt5, I could get what I wanted with Gio from PyGtk. Code to get it:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio

print(Gio.app_info_get_all_for_type('image/png'))
オパラ
  • 317
  • 2
  • 10