The Schworak Site | Log In | Up One Level

How to make xdg-open handle custom protocols such as SSH

You may have a need to have custom URLs such as ssh://yoursite.com processed with your own script. This is pretty easy to do with just a couple commands.

Lets start by creating the script to actually do the work. For this example we are going to launch a ssh so the script is very simple. In fact, we could actually skip making a script for ssh but it there may be more logic you want to add beyond simply launching a single command so a script file is needed.

Edit the file /home/yourname/bin/ssh-handler with your favorite editor. 

#!/bin/bash
p=`echo "$1" | sed "s/\([^:]*\).*/\1/"` # get the protocol
u=`echo "$1" | sed "s/[^:]*:\/\/\(.*\)/\1/"` # get the url
ssh -t -X $u

Make this file exicutable using chmod. Next we need to create the desktop file that tells the system how to get to the script file we just created.

Edit the file /home/yourname/.local/share/applications/ssh.desktop.

[Desktop Entry]
Terminal=false
Type=Application
Exec=/home/yourname/bin/ssh-handler %U

The %U on the last line is very important. That is the URL that is to be passed along to the script. You will need the full path to the script. You can't short cut using ~/ if you expect your script to run.

Finally we need to tell xdg to use the desktop file we just created when the ssh protocol is used in a link. The command looks like this...

xdg-mime default ssh.desktop x-scheme-handler/ssh

Next time you create a link to any ssh site using the format ssh://somesite.com, your script will fire off and perform the needed steps.

Comments

All content on this site is copyright ©2004-2019 and is not to be reproduced without prior permission.