Difference between revisions of "xdg-open/how to/add custom protocol"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
m (Woozle moved page xdg-open/notes to xdg-open/how to/add custom protocol without leaving a redirect: reorganizing, now that it's working)
 
(No difference)

Latest revision as of 00:46, 3 January 2024

How to add a custom protocol

The following instructions were adapted from ChatGPT[1] but tested and modified slightly for clarity.

Create a custom desktop file

Create a new desktop file for your custom protocol. Desktop files usually have a .desktop extension and are located in the /usr/share/applications/ directory or ~/.local/share/applications/ for user-specific settings.

Example (custom-protocol.desktop):

[Desktop Entry]
Name=Custom Protocol Handler
Exec=/path/to/your/custom-protocol-handler %U
Terminal=false
Type=Application
MimeType=x-scheme-handler/<protocol name>;

That is: if your custom protocol is (e.g.) "wooz://<URI>", then MimeType should be set to x-scheme-handler/wooz.

Customize the Exec line to point to the executable that will handle your custom protocol.

Update MIME types

Open the file /usr/share/applications/mimeinfo.cache or ~/.local/share/applications/mimeinfo.cache and add the following line:

x-scheme-handler/<protocol name>=custom-protocol.desktop;

If the mimeinfo.cache file doesn't exist, you may need to create it.

Update the MIME database

Run the following command to update the MIME database:

update-desktop-database ~/.local/share/applications/

If you added the desktop file to the system-wide directory (/usr/share/applications/), you might need to run the command with superuser privileges:

sudo update-desktop-database /usr/share/applications/

Test your custom protocol

After completing the above steps, you should be able to use xdg-open with your custom protocol. For example:

xdg-open <protocol name>://example

This should launch the specified handler for your custom protocol.

Remember to replace "custom-protocol" with your actual protocol name and update file paths accordingly. Additionally, depending on your desktop environment, there may be slight variations in the steps. If your desktop environment supports xdg-open, these general steps should work.

Footnotes

  1. 2024-01-01 ChatGPT 3.5, in response to "How can I add a custom protocol to xdg-open?"