How to programmatically register in the OS a program for opening a certain type of files by default, and also teach the program to understand that something is opened in it.

Windows version of the question: How can I programmatically register a program in the OS to open a certain type of file by default? (Windows)

  • Related question: How to set default program? - jfs
  • @jfs, I need to be able to make a script that installs the program automatically for the format. - Mihail Ris
  • @MihailRis, 1. you just want to add your handler for the existing mime-type, or in addition also describe the new mime-type? 2. What does python have to do with the question? - aleksandr barakin
  • 2
    The comment is not the answer. Therefore, my link is not intended to answer your question by itself. Linux is not Windows, so "install the program automatically for the format" may not work as you expect (no assoc, ftype on Linux). After reading the link, you might mention: "I want to call mimeopen -d .avi in Python" or "I want to know how to edit the program: ~/.config/mimeapps.list " (see how xdg-mime is implemented or directly call this script ) or vice versa, say "I want to run .py files" (then the answer is different: add shebang #! and call chmod +x ). - jfs
  • There is also an update-alternatives mechanism that allows you to choose which specific program is launched by specified names such as editor , display , rename , java , nc , etc. There is not enough information in the question to understand whether it is applicable in your particular case. - jfs

1 answer 1

A detailed answer requires, of course, a detailed analysis of both the xdg specifications (concerning mime information in general and desktop files in particular), as well as various implementations of these standards.


and the short answer, in the style of “do this, and it will most likely work,” may sound like this: add a line connecting the desktop file of your program with the mime type you need to the file /usr/share/applications/defaults.list (some de seem to use in addition and /usr/share/applications/mimeapps.list ) .


example:

so that by default, when you open the file with the mime type image/png , the program described in the desktop /usr/share/applications/display-im6.desktop launched, add the following line to /usr/share/applications/defaults.list :

 image/png=display-im6.desktop; 

if the file did not exist before, it makes sense for the “beauty and correctness” to specify the section in the first line:

 [Default Applications] 

Of course, this behavior can be overridden by a similar user-defined file ~/.local/share/applications/defaults.list .


The system-wide binding of mime types to file suffixes is located in the /etc/mime.types file.

The system-wide cache for all bindings of registered desktop files to mime types is located in the /usr/share/applications/mimeinfo.cache file.

  • On different systems, different files can be used, look at xdg-mime mentioned above — by the way, if this utility works on the selected system, it may be easier to call it than to edit files by hand. - jfs
  • @jfs, 1. reservations about brevity and about "most likely earns" - are given. 2. xdg-mime changes only user files, without interfering with system-wide settings. - aleksandr barakin
  • 1- run xdg-mime no less “briefly” than your suggestion to find the file and edit it yourself, but it may be more portable. This sentence to improve the answer (otherwise I would give my own) 2 - why change the file system-wide associations? (What use-case?) If you really want to, then from under root you can try to start? (Not recommended option) - jfs
  • @jfs, one answer to both points is a quote from the question: “register a program with the OS”. I wrote about system-wide settings, deliberately omitting both user settings (although I mentioned them) and, of course, programs for changing them ( xdg-mime and the xdg-utils package in general is far from the only implementation). - aleksandr barakin
  • 2
    @MihailRis, if you have a new question, you can ask it using the appropriate button in the upper right corner of the page. - aleksandr barakin