Create a Custom URL Protocol with XDG in Ubuntu

In this article, we would see how to create a Custom URL Protocol with XDG in Ubuntu. We will provide you an overview of what we are trying to achieve with the help of an example.

There are times when URL Protocols aren’t recognised by the web browser. If we click on the web address, it just does nothing. The issue crops up because we haven’t configured our System settings. The browser simply just doesn’t respond. In such a scenario, we need to create a Custom URL Protocol with XDG.

Hence, first instruct the web browser to open an associated application whenever it encounters a specific protocol. We will take the example of Unity Hub protocol later in the Additional Info section. But, it is applicable to other URL protocols as well.

Note: Following operations would require you to have superuser privileges. In case, you don’t have one then we advise you to contact your System Administrator.

Create a Custom URL Protocol with XDG in Ubuntu

We have tested the following instructions in Firefox web browser.

Part I. First, we need to install a package: xdg-utils (Move to Part II, if the package is already installed). The package contains a command-line utility: xgd-mime which would be utilised to add details of new file types.

Update the standard Ubuntu repository. This makes sure we have the latest version of the package available through repository. Hence, open a terminal and issue the following –

sudo apt update

To install xdg-utils

sudo apt install xdg-utils

Part II. Lets say we have URL Protocol and an .AppImage for the application –

xyz://qwerty/abc

First, open a text editor and append the file with the following –

[Desktop Entry]
Name=AppName
Comment= The Comment
Exec="/path/to/App-Name.AppImage" %U
Terminal=false
Type=Application
MimeType=x-scheme-handler/xyz

Save the file name with extension: .desktop
For example – AppName.desktop
This creates a Desktop entry.

Move the AppName.desktop to $HOME/.local/share/applications

Now, issue the following in terminal –

xdg-mime default AppName.desktop x-scheme-handler/xyz

This time around when you open the URL Protocol through the web browser, it should open the associated application.

In conclusion, we have discussed how to create a custom URL Protocol with XDG in Ubuntu.

Additional Info –

Lets say, our System isn’t configured to recognise unityhub protocol. The protocol would be like –

unityhub://werty/qwerty

As already discussed above, create a Desktop Entry – UnityHub.desktop and save it to $HOME/.local/share/applications/

And, append UnityHub.desktop with the following –

[Desktop Entry]
Name=UnityHub
Comment= Unity Hub
Exec="/path/to/UnityHub.AppImage" %U
Terminal=false
Type=Application
MimeType=x-scheme-handler/unityhub

Now, open a terminal and issue the following –

xdg-mime default UnityHub.desktop x-scheme-handler/unityhub

Open the relevant URL through Firefox to see if it launches the UnityHub.

Similar Posts