Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

12.3. Configuring File Associations

12.3.1. What Are MIME Types?

In GNOME, MIME (Multipurpose Internet Mail Extension) types are used to identify the format of a file. The GNOME Desktop uses MIME types to:
  • Determine which application should open a specific file format by default.
  • Register other applications that can also open a specific file format.
  • Provide a string describing the type of a file, for example, in a file properties dialog of the Files application.
  • Provide an icon representing a specific file format, for example, in a file properties dialog of the Files application.
MIME type names follow a given format:
media-type/subtype-identifier

Example 12.7. MIME Types Format

image/jpeg is an example of a MIME type where image is the media type, and jpeg is the subtype identifier.
GNOME follows the freedesktop.org Shared MIME Info specification to determine:
  • The machine-wide and user-specific location to store all MIME type specification files.
  • How to register a MIME type so that the desktop environment knows which applications can be used to open a specific file format.
  • How the user can change which applications should open what file formats.

12.3.1.1. What Is the MIME Database?

The MIME database is a collection of all MIME type specification files that GNOME uses to store information about known MIME types.
The most important part of the MIME database from the system administrator's point of view is the /usr/share/mime/packages/ directory where the MIME type related files specifying information on known MIME types are stored. One example of such a file is /usr/share/mime/packages/freedesktop.org.xml, specifying information about the standard MIME types available on the system by default. That file is provided by the shared-mime-info package.
Getting More Information
For detailed information describing the MIME type system, see the freedesktop.org Shared MIME Info specification located at the freedesktop.org website:

12.3.2. Adding a Custom MIME Type for All Users

To add a custom MIME type for all users on the system and register a default application for that MIME type, you need to create a new MIME type specification file in the /usr/share/mime/packages/ directory and a .desktop file in the /usr/share/applications/ directory.

Procedure 12.3. Adding a Custom application/x-newtype MIME Type for All Users

  1. Create the /usr/share/mime/packages/application-x-newtype.xml file:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
      <mime-type type="application/x-newtype">
        <comment>new mime type</comment>
        <glob pattern="*.xyz"/>
      </mime-type>
    </mime-info>
    The sample application-x-newtype.xml file above defines a new MIME type application/x-newtype and assigns file names with the .xyz extension to that MIME type.
  2. Create a new .desktop file named, for example, myapplication1.desktop, and place it in the /usr/share/applications/ directory:
    [Desktop Entry]
    Type=Application
    MimeType=application/x-newtype
    Name=My Application 1
    Exec=myapplication1 field_code
    The sample myapplication1.desktop file above associates the application/x-newtype MIME type with an application named My Application 1, which is run by the command myapplication1.
    Based on how myapplication1 gets started, choose one respective field code from Desktop Entry Specification. For example, for an application capable of opening multiple files, use:
    Exec=myapplication1 %F
  3. As root, update the MIME database for your changes to take effect:
    # update-mime-database /usr/share/mime
  4. As root, update the application database:
    # update-desktop-database /usr/share/applications
  5. To verify that you have successfully associated *.xyz files with the application/x-newtype MIME type, first create an empty file, for example test.xyz:
    $ touch test.xyz
    Then run the gvfs-info command:
    $ gvfs-info test.xyz | grep "standard::content-type"
      standard::content-type: application/x-newtype
    
  6. To verify that myapplication1.desktop has been correctly set as the default registered application for the application/x-newtype MIME type, run the gvfs-mime --query command:
    $ gvfs-mime --query application/x-newtype
    Default application for 'application/x-newtype': myapplication1.desktop
    Registered applications:
    	myapplication1.desktop
    Recommended applications:
    	myapplication1.desktop

12.3.3. Adding a Custom MIME Type for Individual Users

To add a custom MIME type for individual users and register a default application for that MIME type, you need to create a new MIME type specification file in the ~/.local/share/mime/packages/ directory and a .desktop file in the ~/.local/share/applications/ directory.

Procedure 12.4. Adding a Custom application/x-newtype MIME Type for Individual Users

  1. Create the ~/.local/share/mime/packages/application-x-newtype.xml file:
    <?xml version="1.0" encoding="UTF-8"?>
    <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
      <mime-type type="application/x-newtype">
        <comment>new mime type</comment>
        <glob pattern="*.xyz"/>
      </mime-type>
    </mime-info>
    The sample application-x-newtype.xml file above defines a new MIME type application/x-newtype and assigns file names with the .xyz extension to that MIME type.
  2. Create a new .desktop file named, for example, myapplication1.desktop, and place it in the ~/.local/share/applications/ directory:
    [Desktop Entry]
    Type=Application
    MimeType=application/x-newtype
    Name=My Application 1
    Exec=myapplication1 field_code
    The sample myapplication1.desktop file above associates the application/x-newtype MIME type with an application named My Application 1, which is run by the command myapplication1.
    Based on how myapplication1 gets started, choose one respective field code from Desktop Entry Specification. For example, for an application capable of opening multiple files, use:
    Exec=myapplication1 %F
  3. Update the MIME database for your changes to take effect:
    $ update-mime-database ~/.local/share/mime
  4. Update the application database:
    $ update-desktop-database ~/.local/share/applications
  5. To verify that you have successfully associated *.xyz files with the application/x-newtype MIME type, first create an empty file, for example test.xyz:
    $ touch test.xyz
    Then run the gvfs-info command:
    $ gvfs-info test.xyz | grep "standard::content-type"
      standard::content-type: application/x-newtype
    
  6. To verify that myapplication1.desktop has been correctly set as the default registered application for the application/x-newtype MIME type, run the gvfs-mime --query command:
    $ gvfs-mime --query application/x-newtype
    Default application for 'application/x-newtype': myapplication1.desktop
    Registered applications:
    	myapplication1.desktop
    Recommended applications:
    	myapplication1.desktop

12.3.4. Overriding the Default Registered Application for All Users

The /usr/share/applications/mimeapps.list and /usr/share/applications/[desktop environment name]-mimeapps.list file are the package-installed defaults, which specify which application is registered to open specific MIME types by default.
To override the system defaults for all users on the system, system administrators need to create the /etc/xdg/mimeapps.list or /etc/xdg/[desktop environment name]-mimeapps.list file with a list of MIME types for which they want to override the default registered application.
The order in which the configurations are applied is as follows:
  1. /usr/share/applications/
  2. /etc/xdg/
Within a particular location, the configurations are applied in this order:
  1. mimeapps.list
  2. [desktop environment name]-mimeapps.list
System administrator's configuration thus takes precedence over package configuration. And within each, desktop-specific configuration takes precedence over the configuration that does not specify the desktop environment.

Note

Red Hat Enterprise Linux versions prior to 7.5 used the defaults.list file instead of the mimeapps.list file.

Procedure 12.5. Overriding the Default Registered Application for All Users

  1. Consult the /usr/share/applications/mimeapps.list file to determine the MIME types for which you want to change the default registered application. For example, the following sample of the mimeapps.list file specifies the default registered application for the text/html and application/xhtml+xml MIME types:
    [Default Applications]
    text/html=firefox.desktop
    application/xhtml+xml=firefox.desktop
    
    The default application (Firefox) is defined by specifying its corresponding .desktop file (firefox.desktop). The default location for other applications' .desktop files is /usr/share/applications/.
  2. Create the /etc/xdg/mimeapps.list file. In the file, specify the MIME types and their corresponding default registered applications:
    [Default Applications]
    text/html=myapplication1.desktop
    application/xhtml+xml=myapplication2.desktop
    This sets the default registered application for the text/html MIME type to myapplication1.desktop, and the default registered application for the application/xhtml+xml MIME type to myapplication2.desktop.
    For these settings to function properly, ensure that both the myapplication1.desktop and myapplication2.desktop files are placed in the /usr/share/applications/ directory.
  3. You can use the gvfs-mime query command to verify that the default registered application has been set correctly:
    $gvfs-mime query text/html
    Default application for 'text/html': myapplication1.desktop
    Registered applications:
    	myapplication1.desktop
    	firefox.desktop
    Recommended applications:
    	myapplication1.desktop
    	firefox.desktop

12.3.5. Overriding the Default Registered Application for Individual Users

The /usr/share/applications/mimeapps.list and /usr/share/applications/[desktop environment name]-mimeapps.list file are the package-installed defaults, which specify which application is registered to open specific MIME types by default.
To override the system defaults for individual users, you need to create the ~/.local/share/applications/mimeapps.list or ~/.local/share/applications/[desktop environment id]-mimeapps.list file with a list of MIME types for which you want to override the default registered application.
The order in which the configurations are applied is as follows:
  1. /usr/share/applications/
  2. /etc/xdg/
  3. ~/.local/share/application/
Within a particular location, the configurations are applied in this order:
  1. mimeapps.list
  2. [desktop environment name]-mimeapps.list
User's configuration thus takes precedence over system administrator's configuration, and system administrator's configuration takes precedence over package configuration. And within each, desktop-specific configuration takes precedence over the configuration that does not specify the desktop environment.

Note

Red Hat Enterprise Linux versions prior to 7.5 used the defaults.list file instead of the mimeapps.list file.

Procedure 12.6. Overriding the Default Registered Application for Individual Users

  1. Consult the /usr/share/applications/mimeapps.list file to determine the MIME types for which you want to change the default registered application. For example, the following sample of the mimeapps.list file specifies the default registered application for the text/html and application/xhtml+xml MIME types:
    [Default Applications]
    text/html=firefox.desktop
    application/xhtml+xml=firefox.desktop
    
    The default application (Firefox) is defined by specifying its corresponding .desktop file (firefox.desktop). The system default location for other applications' .desktop files is /usr/share/applications/. Individual users' .desktop files can be stored in ~/.local/share/applications/.
  2. Create the ~/.local/share/applications/mimeapps.list file. In the file, specify the MIME types and their corresponding default registered applications:
    [Default Applications]
    text/html=myapplication1.desktop
    application/xhtml+xml=myapplication2.desktop
    This sets the default registered application for the text/html MIME type to myapplication1.desktop, and the default registered application for the application/xhtml+xml MIME type to myapplication2.desktop.
    For these settings to function properly, ensure that both the myapplication1.desktop and myapplication2.desktop files are placed in the /usr/share/applications/ directory.
  3. You can use the gvfs-mime --query command to verify that the default registered application has been set correctly:
    $gvfs-mime --query text/html
    Default application for 'text/html': myapplication1.desktop
    Registered applications:
    	myapplication1.desktop
    	firefox.desktop
    Recommended applications:
    	myapplication1.desktop
    	firefox.desktop