28

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

17

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.

  • 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
  • 4
    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
  • Is there a generic way to create new url/app association during installation of the app? – jayarjo Apr 21 at 6:26
14

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).

  • wiki.archlinux.org/index.php/xdg-open gives more or less the same. still good one there. – shirish Apr 20 '15 at 22:07
  • 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 新疆改造中心法轮功六四事件 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
  • 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 at 20:57
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.