In the previous post about how to hibernate your Ubuntu machine I touched on the concept of creating a desktop shortcut for the hibernate command to make it easy to run the command. That post can be found here. But since then I have been playing with desktop shortcuts some more and so in this post I go into some detail and outline how to create a shortcut icon with content menus too.
The basic concepts with examples…
The basic concept is to create the shortcut as a text file with the “.desktop” file extenstion and in it we define what we want to launch together with some other basic details about the application or script/command, such as icon, name, and whether it needs to run in the terminal etc. Below is an example “.desktop” file, saved to my desktop as “HibernateNow.desktop”. It is running a bash script via the terminal (/hibernatescript.sh).
[Desktop Entry] Type=Application Terminal=true Name=HibernateNow Icon=utilities-terminal Exec=gnome-terminal -e "bash -c './hibernatescript.sh;$SHELL'" Categories=Application;
From ubuntu 20.04 onwards you may need to right click on the file after creation and choose “Allow Launching”, which is a one time operation (see screenshot).
But lets say that we want to create a shortcut for an application instead of a script. Its the same concept except the Exec path would be the file path to the executable application instead of a script and it would be “terminal=false” as we don’t need to launch the command in the terminal.
In the below example we are launching the Visual Studio Code application which is executed from /usr/share/code/code.
Just save this file on your desktop with a name “.desktop” extension, eg. VSCode.desktop.
[Desktop Entry] Type=Application Terminal=false Name=VSCode Icon=utilities-terminal Exec=/usr/share/code/code Categories=Application;
In a nutshell that’s it for creating application shortcuts in Ubuntu (and many other Linux distros as this is a shared standard). Just create the file and amend the Exec path to be the application you want to launch, then change the name and optionally the icon etc. The category property is for you to optionally choose which group of application types it sits with in the menus.
Taking it further…
But what else can we do with these “.desktop” files? The specification for the files can be found here. As well as the Type=Application option there is also Type=Link for web/document links that can be used with a URL property like this….
[Desktop Entry] Encoding=UTF-8 Name=Google Type=Link URL=http://www.google.co.uk/ Icon=text-html
But this doesnt work for me in Ubuntu 20.10 and it seems this has been removed from Gnome as per the discussion on this GitHub issue. If you want to add a web URL then its possible to use the Type=Application and point to your browser of choice (e.g. Firefox) and pass the URL as a parameter as per below:
[Desktop Entry] Type=Application Terminal=false Name=Google Icon=text-html Exec=firefox -new-window www.google.co.uk Categories=Application;
Notice that the Icon property has been updated to be Icon=text-html so show its a web link. The icons spec can be found here . A good place to find new icons is to poke around in the “.Desktop” files already on your system. On Ubuntu checkout here: /usr/share/applications for a list of your shared system applications shortcuts or /home/.local/share/applications for your users application shortcuts list. If you add your custom “.desktop” file to one of the above file paths then you can see them in your applications menu, and then you can go further by adding it to your Favourites list (right click icon > Add to Favorties), thus making it appear in your launcher (depending on your distro’s launcher toolbar settings).
Adding Context Menu Actions to the shortcuts…
Check out this example below for a shortcut named Test that launches an application (VS Code in this case). Notice the Actions property and associated “Desktop Action” entry
which provides an additional menu item for action within the shortcut (in this case it uses Firefox to navigate to Google). If we create the textfile with the below contents and call it “test.Desktop”, then add it to the /home/.local/share/applications folder, it will appear in the Applications list and also now has a right click context menu which includes “Navigate to Google” (as per the screenshot).
[Desktop Entry] Type=Application Terminal=false Name=Test-Shortcut Icon=com.visualstudio.code Exec=/usr/share/code/code Categories=Application; Actions=shortcut-action-here; [Desktop Action shortcut-action-here] Name=Navigate to Google Exec=firefox -new-window www.Google.co.uk Icon=com.visualstudio.code
And if I add it to my Dock Toolbar then I get the context menu there too.
A Useful GitHub Shortcut Example…
A better fleshed out and usable example is below where the shortcut is to GitHub (via firefox) and there are multiple actions for the context menu options for viewing Profile, Issues and Pull Requests.
[Desktop Entry] Type=Application Terminal=false Name=GitHub Icon=/usr/local/share/github.png Exec=firefox www.github.com Categories=Application; Actions=Profile;Issues;Pull-Requests [Desktop Action Profile] Name=Profile Exec=firefox https://github.com/richhewlett Icon=/home/Desktop/github.png [Desktop Action Issues] Name=Issues Exec=firefox https://github.com/issues Icon=/home/Desktop/github.png [Desktop Action Pull-Requests] Name=Pull Requests Exec=firefox https://github.com/pulls Icon=/home/Desktop/github.png
So as you can see there is quite a lot of useful functionality in the desktop shortcuts on Linux.