edoceo

Creating Custom URI/URL Handlers in Xfce/FreeDesktop/XDG

Yes, FreeDesktop are cool but out of the box many don't come with a proper set of URI handlers. How to open, for example vnc:// or ssh:// or icr:// - or some that you may make up in the future. Some Web-Browsers, such as Firefox, have a built-in configuration that you can set per Browser profile, but that won't affect the whole desktop or other browsers, or other system users!

This How-To will demonstrate how to create a custom URI handler, for all desktop applications using FreeDesktop standard methods. We'll simply update the list of mime-handlers and create the custom wrapper. To start the configuration will work for the current user, but can be upgraded to a system-wide configuration.

Registering Custom URI Handler

There is a file, ~/.local/share/applications/mimeapps.list that contains a list of MIME handlers. Take note of the special MIME category of "x-scheme-handler", with it's sub-type begin a URI scheme. Here is a sample:

[Added Associations]
application/vnd.mozilla.xul+xml=mousepad.desktop;
application/javascript=mousepad.desktop;
text/html=mousepad.desktop;
application/x-php=mousepad.desktop;
x-scheme-handler/http=exo-web-browser.desktop
x-scheme-handler/https=exo-web-browser.desktop
x-scheme-handler/file=exo-file-manager.desktop
x-scheme-handler/trash=exo-file-manager.desktop
video/mpeg=vlc.desktop;
application/x-ole-storage=openoffice.org-calc.desktop;
image/png=ristretto.desktop;
image/jpeg=ristretto.desktop;
text/plain=leafpad.desktop;
application/x-wine-extension-ini=leafpad.desktop;
application/pdf=evince.desktop;

# These are Customs!
x-scheme-handler/vnc=exo-vnc-handler.desktop
x-scheme-handler/ssh=exo-ssh-handler.desktop

[Default Applications]
application/x-wine-extension-ini=leafpad.desktop
application/pdf=evince.desktop

To create a custom handler, simply create an entry under the [Added Associations] section. We have two examples listed, one for vnc and the other for SSH. The format is x-scheme-handler/%s=%d, where %s is the scheme name and %d is the desktop entry file.

Creating the Handlers

Some handlers are simple enough you can just pass the parameters directly, others may need some wrapper script. These handlers are created in the users application directory: ~/.local/share/applications/.

Here is a sample vnc:// handler Desktop file

[Desktop Entry]
Version=1.0
Type=Application
Exec=/home/atom/vnc-handler.sh %u
Icon=vncviewer
StartupNotify=true
Terminal=false
Categories=Utility;X-XFCE;X-Xfce-Toplevel;
MimeType=x-scheme-handler/vnc
Name=VNC Launcher
Comment=Launch VNC

And VNC Handler had to be specially built, it's not built-in, that's vnc-handler.sh

#!/bin/bash

host=$(echo "$1"|perl -ne '/vnc:\/\/(.+)[:\/]/ && print $1')
port=$(echo "$1"|perl -ne '/:(\d+)$/ && print $1')
vncviewer $host::$port

That will properly respond to URIs like: vnc://host:5900.

And here is a another example for ssh:// handler Desktop file (in ~/.local/share/applications/mimeapps.list.

[Desktop Entry]
Version=1.0
Type=Application
Exec=/home/atom/xdg/ssh-handler.sh %u
Icon=roxterm
StartupNotify=true
Terminal=false
Categories=Utility;X-XFCE;X-Xfce-Toplevel;
MimeType=x-scheme-handler/ssh
Name=SSH Launcher
Comment=Launch SSH

Another special handler that's exo-ssh-handler.sh

#!/bin/bash

(
    set -o xtrace
    echo ""
    echo "$1" | perl -ne '/^(ssh):\/\/([^:@]*)?:?([^:@]*)?[:@]?([^:@\/\?\#]*)/ && print "$1\n"; print "$2\n"; print "$3\n"; print "$4\n"'
    echo ""
    # 1 2 3 4 5 6 7 8 9
    user=$(echo "$1"|perl -ne '/ssh:\/\/([^:@]*)(:?.*)@?(\w+)/ && print $1')
    [ -z "$user" ] && user=$USER
    host=$(echo "$1"|perl -ne '/ssh:\/\/.*([\w\.]*):/ && print $1')
    port=$(echo "$1"|perl -ne '/:(\d+)$/ && print $1')
    [ -z "$port" ] && port="22"

    roxterm --separate --title="SSH $1" --execute ssh -p $port $user@$host

) >> /tmp/exo.log 2>&1

That will properly respond to URIs like: ssh://host or ssh://host:2200

GVFS Over-Rides

The gvfs system may take precident over what you have specified here, specifically the ssh:// handler is aliased to sftp://. Looking in the directory: /usr/share/gvfs/mounts/ we can find the handlers. Edit sftp.desktop and remove the SchemeAlias, like the following example.

[Mount]
Type=sftp
Exec=/usr/libexec/gvfsd-sftp
AutoMount=false
Scheme=sftp
# SchemeAliases=ssh
DefaultPort=22
HostnameIsInetAddress=true

Thanks

Shout out to "TGEN" on #xfce for some tips/tricks.

See Also

Loading Comments from Disqus...