Create a shortcut for URL?

Solution 1:

In Ubuntu an URL shortcut is stored in a .desktop file as follow (for example):

[Desktop Entry]
Encoding=UTF-8
Name=Link to Ask Ubuntu
Type=Link
URL=http://www.askubuntu.com/
Icon=text-html

You can drag links from Firefox or Chrome and drop them on the Desktop or any other folder where you have permissions to save files.

Note: Link will appear on your Desktop or your file explorer (i.e. caja) under the name in the line Name=…, not by its actual filename. And without any ….desktop extension.

Solution 2:

This solution is multi-platform also:

  1. Create a new simple text file with an .html extension and whatever name you want.

  2. Edit the file with the program you want and add this content:

    <html>
        <head>
            <meta http-equiv="refresh" content="0; url=https://askubuntu.com" />
        </head>
        <body> </body>
    </html>
    
  3. Save the file.

Solution 3:

I needed something like this, but Gnome (now?) requires *.desktop files to be in specific locations, so using those directly wouldn't solve my problem, as I wanted web-links for reference mostly in project folders. And they wouldn't be cross-platform compatible either.

Eventually I turned to Microsoft's .url files, which are easily constructed and look like this:

[InternetShortcut]
URL=https://askubuntu.com

(I read that the trailing line break is important, and probably should be \r\n for Windows compatibility)

And created a .desktop specification to handle them:

[Desktop Entry]
Type=Application
Name=URL Handler
MimeType=application/x-mswinurl;
Exec=bash -c "set -e; P=$(python3 -c 'import configparser,sys,urllib.parse; c=configparser.ConfigParser(); c.read(sys.argv[1])\ntry:\n    u=c[\"InternetShortcut\"][\"URL\"]\n    if not urllib.parse.urlparse(u).scheme in [\"http\",\"https\",\"ftp\",\"ssh\"]: raise Exception(\"Invalid scheme in URI\")\n    print(u)\nexcept Exception as e: print(e,file=sys.stderr); exit(3);' %f); xdg-open \"$P\""

Put that into a file in ~/.local/share/applications/<whatever>.desktop. In my case Gnome immediately bound *.url files to them.

This requires xdg-utils package (for xdg-open, it's likely there if you have a desktop-environment) and python 3.

I really really didn't want to use python, but doing unchecked parsing on something like this didn't suit me. The largest parts of the python script are to avoid infinite loops among other dangers -- in case someone gets funny ideas and puts a file name in a .url file. It ensures that a scheme is present and is one of http(s), ftp, ssh. I guess that list can be easily extended, but I actually don't know which schemes Windows supports.

Solution 4:

Add this to your ~/.bashrc:-

function createUrlShortcut {
    if [ "$#" -ne 3 ]; then
        echo "Illegal number of parameters. Usage : createUrlShortcut Name Url FileBaseName"
    fi
    printf "[Desktop Entry]\nEncoding=UTF-8\nName=$1\nType=Link\nURL=$2\nIcon=text-html" > ~/Desktop/$3.Desktop
}

To create a shortcut, do as follows:-

createUrlShortcut RGB-Dataset https://vision.in.tum.de/data/datasets/rgbd-dataset/download RGBD-Dataset-Link

The first argument is the name you want to be displayed in nautilus.
The second argument is the url.
The third argument is the actual name of the file which will be appended by .Desktop extension.

Note that this will create a file with name RGBD-Dataset-Link.Desktop but will be displayed as RGB-Dataset in nautilus.