xdg-open: Setting default handler for supported mime-types

April 16, 2023 - Reading time: 4 minutes

Many programs on Linux desktop use xdg-open program to start program given a file based on detected file mime-type.

This script will allow you to query and assign all supported mime-types to a default application.

Script

Save as xdg-mime-default and make executable:

#!/bin/sh
D=$1
if test -z "$D"; then
find /usr/share/applications/ ~/.local/share/applications/ -name '*.desktop' | while read DF; do
D=`basename "$DF"`
echo $D
cat "$DF" | grep MimeType | cut -f2 -d= | tr ';' "\n" | grep -v '^$' | while read M; do
echo -n "\t$M current: "
xdg-mime query default "$M"
done
echo
done
fi
if test -n "$D"; then
find /usr/share/applications/ ~/.local/share/applications/ -name "$D" | while read DF; do
cat "$DF" | grep MimeType | cut -f2 -d= | tr ';' "\n" | grep -v '^$' | while read M; do
echo "Using $D for $M"
xdg-mime default "$D" "$M"
done
done
fi

Querying

You can query available applications, their supported mime-types and currently assigned application by just running the script without any arguments.

./xdg-mime-default
PrusaGcodeviewer.desktop
text/x.gcode current: PrusaGcodeviewer.desktop

PrusaSlicer.desktop
model/stl current: PrusaSlicer.desktop
application/vnd.ms-3mfdocument current: PrusaSlicer.desktop
application/prs.wavefront-obj current: PrusaSlicer.desktop
application/x-amf current: PrusaSlicer.desktop

amfora.desktop
x-scheme-handler/gemini current: amfora.desktop

imv.desktop
image/bmp current: imv.desktop
image/gif current: wine-extension-gif.desktop
image/jpeg current: wine-extension-jfif.desktop
image/jpg current: imv.desktop
image/pjpeg current: imv.desktop
image/png current: wine-extension-png.desktop
image/tiff current: imv.desktop
image/x-bmp current: imv.desktop
image/x-pcx current: imv.desktop
image/x-png current: imv.desktop
image/x-portable-anymap current: imv.desktop
image/x-portable-bitmap current: imv.desktop
image/x-portable-graymap current: imv.desktop
image/x-portable-pixmap current: imv.desktop
image/x-tga current: imv.desktop
image/x-xbitmap current: imv.desktop
image/heif current: imv.desktop

...

Default application for all supported mime-types

You can assign the application to all its supported mime-types by running the script with desktop file name as first argument.

./xdg-mime-default imv.desktop
Using imv.desktop for image/bmp
Using imv.desktop for image/gif
Using imv.desktop for image/jpeg
Using imv.desktop for image/jpg
Using imv.desktop for image/pjpeg
Using imv.desktop for image/png
Using imv.desktop for image/tiff
Using imv.desktop for image/x-bmp
Using imv.desktop for image/x-pcx
Using imv.desktop for image/x-png
Using imv.desktop for image/x-portable-anymap
Using imv.desktop for image/x-portable-bitmap
Using imv.desktop for image/x-portable-graymap
Using imv.desktop for image/x-portable-pixmap
Using imv.desktop for image/x-tga
Using imv.desktop for image/x-xbitmap
Using imv.desktop for image/heif

Default application for selected mime-type

You can assign application to just one mime-type by using:

xdg-mime default firefox.desktop application/pdf

Mastodon