How do I create an internet shortcut within a folder? [duplicate]
How can I create a shortcut for URL in my desktop? I want to use them in Firefox or Chrome.
If a copy and paste didn't work.
What script I have to use?
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:
-
Create a new simple text file with an .html extension and whatever name you want.
-
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>
-
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.