DEV Community

Cover image for Creating XDG custom url scheme handler
Izhar Firdaus
Izhar Firdaus

Posted on • Originally published at on

Creating XDG custom url scheme handler

If you develop system tools or desktop software on Linux that also have an accompanying web application, you might want to have a way for the web application to launch the tool with some parameters specified through a web based link. For example, a link with dnf://inkscape as url, might be used to launch Gnome Software, and display the description of Inkscape, so that user may choose to install it or not.

In Linux, registering a custom URL handler can be done using XDG desktop file, of which it is configured to open x-scheme-handler MimeType.

To achieve this, you can simply create a .desktop in ~/.local/share/applications/, or/usr/local/share/applications, and configure it with MimeType=x-scheme-handler/<your-custom-proto>.

For example, if you have a script dnfurl which takes dnf://<package-name> as its first parameter and launch Gnome Software with the package name, you can create a .desktopfile with this content:

[Desktop Entry]
Exec=dnfurl %U
Enter fullscreen mode Exit fullscreen mode

After installing the .desktop file, do run update-desktop-database to update the related indexes/cache. If you installed the .desktop file in ~/.local/share/applications/, you will have to run update-desktop-database ~/.local/share/applications. If nothing went wrong with the setup, you should be able to open links with the custom url protocol afterwards.

If you interested with the example dnfurl program above, you can check it out at this git repository: Or if you are in Fedora, you can install it from copr by running:

dnf copr enable izhar/dnfurl
dnf install dnfurl
Enter fullscreen mode Exit fullscreen mode

Have fun~.

Discussion (0)