39

I want to use xdg-open to open irc:// links, how can I make the required parameter?

Oxwivi
  • 17,849

3 Answers3

27

I'll describe this with magnet: URI type and Transmission (i.e. case of bittorent), but the same method can be applied to any scheme or file type.

Also I checked this with Debian Jessie, and I don't actually have Ubuntu machine, but I believe it should work the same (at least for xdg-open, note that file managers may choose to use different logic).

  1. Find out the MIME type string. For file MIME types, you can find it out with file command:

    $ file -i Broken_Blossoms.webm 
    Broken_Blossoms.webm: video/webm; charset=binary
    $
    

    For the above file, MIME type is video/webm.

    For URI handlers, the type is x-scheme-handler/<scheme>, where <scheme> is the part of URI before colon, e.g. "http", "mailto" "irc" or "magnet". Following are examples of valid MIME types:

    x-scheme-handler/http
    x-scheme-handler/irc
    x-scheme-handler/magnet
    x-scheme-handler/mailto
    
  2. Find out the name of application .desktop file.

    Often it's not the same as the "official" name but rather lowercase version of it, or a completely different name. Installed .desktop files live under /usr/share/applications. Since they are normal text files and contain the "official" name, following command can help you:

    $ grep "Transmission" -l -r /usr/share/applications
    /usr/share/applications/transmission-gtk.desktop
    $
    

    The command effectively means "list files under this directory that contain word 'Transmission'". Some applications may be installed only for user, in that case the path would be ~/.local/share/applications.

    In case you have "strange" application that may not have the file at all, you can always create one (and perhaps send it to the app developers). Easy way would be to copy an existing one, rewrite fields you understand and remove those you don't. Refer to the specification for details.

  3. Make the assignment using xdg-mime command:

    $ xdg-mime default transmission-gtk.desktop x-scheme-handler/magnet
    $
    

    Note that no matter where the file actually is (/usr/share/applications, ~/.local/share/applications...), you always use only the name, not the full path.

    Normally the command will not output anything--that's OK. If you want to verify what you just did or see what is currently assigned to any MIME type without opening it:

    $ xdg-mime query default x-scheme-handler/magnet
    transmission-gtk.desktop
    $
    

Note 1: If you want to check out other MIME types, you can look at /etc/mime.types. It does not contain all types in the world; for example the URI handlers, but it could be used for "aggressive" form of handling the associations. For example:

grep ^video/ | cut -d\t -f1 | xargs xdg-mime default vlc.desktop

would associate all known video formats to VLC.

Note 2: The .desktop files often contain list of MIME types that they claim to be able to handle using MimeType field. xdg-mime man page says that the .desktop file must claim the MIME type before the above mentioned command will work, but for me it seems to work even if the field is missing. (I mean, the association will be applied and the application will launch--if it really can handle the type is a different question). I'm not sure what is drawback (maybe in future the xdg-mime will be more restrictive).

  • https://wiki.archlinux.org/index.php/xdg-open gives more or less the same. still good one there. – shirish Apr 20 '15 at 22:07
  • 1
    This answer is the best way. Tip: 1) xdg-mime query filetype FILE is the XDG way to find mime types. 2) You can also edit the defaults yourself to keep things more organized and back them up later: .config/mimeapps.list is the file. – Ciro Santilli OurBigBook.com Mar 20 '16 at 10:14
  • rolled back 2 edits: @frank, care to explain how xdg-mime "works better"? IMO file is the autoritative tool for MIME types, xdg-mime query filetype xyz.wbemOTOH gives less info and is way more to type. – Alois Mahdal May 15 '16 at 18:11
  • rolled back 2 edits: @muru, frankly I did not see how your edits improved the text; e.g. I added quoting since it's "this is what my term showed", also backtick vs. cursive: IMO cursive is for mentioning jargon (which tool names fall into), backticks are for code examples, other changes may be matter of taste... – Alois Mahdal May 15 '16 at 18:15
  • If that's your opinion, keep it. – muru May 15 '16 at 18:17
  • 1
    Great explanation and the tip to use the only name of the desktop file was the solution I was looking for thanks! – Andrea Richiardi Aug 15 '17 at 13:36
  • 1
    this advice finally helped me solve not being able to log in using slack desktop app on linux. slack redirects to a browser, but then the browser uses a URI scheme to redirect back to slack. your tips helped me see that the slack URI xdg-mime mapping was gone. so glad I found this after an hour of searching! thank you! – pestophagous Oct 22 '19 at 20:57
  • tyvm This is the only way I could get xdg-open to work properly with magnet links, so glad you used that as an example. BUT worth adding that you should uninstall the current application association before or after adding your new association. Even though I added qbitorrent xdg-open kept using ktorrent, until I used xdg-mime uninstall /usr/share/applications/org.kde.ktorrent.desktop command. – nobody special Dec 21 '19 at 10:49
19

xdg-open basically just looks to see which desktop environment you have and then runs gnome-open, gvfs-open, xfce-open, etc. See below for desktop environment specific instructions...

Gnome

Gnome uses the gnome-open program which uses gconf to store everything. For example on my machine with Ubuntu 10.10 running gnome-open irc://blah opens up xchat because xchat includes a gconf setting patch to add an irc:// handler.

gconf-editor showing irc with xchat configuration

This shows how gnome does this, with a gconf settings in /desktop/gnome/url-handlers/. See xchat-2.8.8/src/common/dbus/apps_xchat_url_handler.schemas as an example.

KDE

For KDE you should look at the .protocol files in /usr/share/kde4/services/, create a new one for your new protocol and put it in ~/.kde/share/kde4/services/, if it's super useful then consider adding it to the package as a fix for other users.

KDE is using kde-open or kfmclient depending on what's available and what version of KDE you have.

XFCE

XFCE uses a program called exo-open, this program doesn't have any way to configure it or add uri handlers. Looking through the source code shows that is uses desktop files to specify only three types of programs. TerminalEmulator, WebBrowser and EmailClient.

With XFCE4 (and probably also others) it is possible to configure xdg-open to define a custom protocol handler. In some you have to create/edit the following files:

~/.local/share/applications/protocolhandler.desktop ~/.local/share/applications/mimeapps.list

An example adding a handler for the ed2k protocol is provided at stackexchange.com2.

  • 1
    Can you mention how to do that for all official DEs? – Oxwivi Sep 22 '11 at 20:17
  • Not exactly related to the question, but can you quote in full the directory at the bottom of the screen shot? Thank you. – Oxwivi Sep 23 '11 at 13:03
  • 8
    xdg-open basically just looks to see which desktop environment you have and then runs gnome-open, gvfs-open, xfce-open, etc. --- I don't think so. First, the manpage does not say that, and second, xdg-open works perfectly without desktop environment. For example I use it with i3 window manager. – Alois Mahdal Nov 30 '14 at 20:21
  • 1
    Is there a generic way to create new url/app association during installation of the app? – jayarjo Apr 21 '19 at 06:26
  • XFCE: exo-preferred-applications is the tool to set options for exo-open which is in turn used by xdg-open. – poleguy Jun 21 '23 at 17:58
8

I wanted to associate postman links with my manually installed Postman app (not the chrome extension) so that I could open postman docs from the browser like this:

enter image description here

enter image description here

The link I wanted to associate looks like this:

postman://app/collections/import/39995-2b0394ab-b007-488d-9a0a...

To do the association I did the following steps:

  1. Install the app manually (download and unpack a tar.gz) in /home/andrzej.rehmann/software/postman/
  2. Create a desktop file in /usr/share/applications/Postman.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=Postman
Icon=/home/andrzej.rehmann/software/postman/app/resources/app/assets/icon.png
Exec="/home/andrzej.rehmann/software/postman/Postman" %u
Comment=Develop with pleasure!
Categories=Development;
Terminal=false
StartupWMClass=Postman
  1. Associate the postman xdg link with the application by running:

xdg-mime default Postman.desktop x-scheme-handler/postman


I've ansibled this configuration if anyone is interested: https://github.com/hoto/ansible-home-fedora/blob/fedora/roles/postman/tasks/postman_installer.yml

Andrzej Rehmann
  • 189
  • 1
  • 6