Introduction

Desktop files are text files, with .desktop extension, that launches applications. In this tutorial, I wish to explore different ways you can use desktop files to run range of applications.

Anatomy of a desktop file.

If you see the contents of files under /usr/share/applications, you might see something that looks like this.

[Desktop Entry] Name=Gcolor2 GenericName=Gcolor2 Comment=Simple GTK2 color selector and picker Exec=gcolor2 Icon=/usr/share/pixmaps/gcolor2/gcolor2.xpm Terminal=false Type=Application Categories=Application;Graphics;

Some of the interesting variables from this file are

Name: Name of the application as it appears in your launcher, menu etc

GenericName: Generic Name of the application, E.g Web browser

Exec: Name of the application/script to be executed

Icon: Image file, that appears as icon

Terminal: Should this application show the terminal (could be helpful for applications that logs output in a terminal)

Type: Application, Link and Directory are three types

Categories: Under which section on menu, should it appear

You can use only names for Icon or Exec, if those files are located at standard path, otherwise you will have to use full path.

If you want to create desktop files locally, create a directory where you will store these files. Lets call it myapplications inside your home directory as ~/myapplications. To make this directory recognizable by application menus or launchers like gnome-do, synapse, unity-dash etc, you will also have to link it to ~/.local/share/applications. Run following command to do so.

$ ln -s ~/myapplications ~/.local/share/applications/

Lets see different examples, where we can use the desktop files.

Note: Be sure to make the files executable, after its creation, using “chmod +x filename“.

1. Games

Lets say, I want to create a desktop file for a game called FTL, that runs only if I run the FTL script on the game’s own installation directory i.e I cannot run /path/to/ftl/FTL and execute the game, if I am in some other directory in a terminal. I will create a desktop file that looks like this

[Desktop Entry] Name=FTL Comment=Good Game Exec=/path/to/ftl/FTL Icon=/path/to/ftl/data/exe_icon.bmp Path=/path/to/ftl Terminal=false Type=Application Categories=Game;PuzzleGame;

Only interesting thing about above file is the Path variable, that specifies, in which directory should the command execute.

After you create these files, you can collect them just by copying them where ever you like. Just click the icon and the application should launch. You could also drag it to different launcher bars (as you can see in figure below for elementary), execute it from standard application launcher menu etc.

2. Web pages

If I wanted an icon that launches G+, I would create a simple desktop file, that looks like this.

[Desktop Entry] Name=G+ Comment=Google Plus Exec=chromium-browser 'https://plus.google.com/' Icon=/home/username/.icons/gplus.png

In this case, I downloaded a 256×256 image of googleplus from internet, and kept it inside .icons directory (you can keep it anywhere you like).

If you wanted to open up a collection of web pages, using a desktop file, you could easily do so using a script, and point Exec to that script. E.g If I wanted to run reddit.sh, Exec could look this this.

Exec=/home/username/bin/reddit.sh

And the script file (reddit.sh) would look like this

#!/bin/bash chromium-browser 'http://www.reddit.com/r/linux' 'http://www.reddit.com/r/linux_gaming'

3. Command line scripts

As an example let us assume you have dual graphics cards (Intel/AMD hybrid). To switch between the two, you would create a desktop file that looks like this.

[Desktop Entry] Name=Switch AMD Intel Comment=Switch between AMD and Intel Graphics card Exec=/home/username/bin/switchamdintel.pl Icon=/home/username/.icons/amdintel.jpg Terminal=false Type=Application Categories=System

Here, Exec points to a perl script (you can use bash or other scripts too). Content of that script looks like this.

#!/usr/bin/perl $checkGraphicsCommand = `aticonfig --pxl`; chomp $checkGraphicsCommand; if ($checkGraphicsCommand eq "PowerXpress: Integrated GPU is active (Power-Saving mode).") { # Activate the discrete GPU drivers system "pkexec aticonfig --px-dgpu && pkill gnome-session" } else { # Activate the integrated intel 3000HD drivers system "pkexec aticonfig --px-igpu && pkill gnome-session" }

The good thing is, I don’t have to open up AMD catalyst control center, or fire up a terminal and follow series of steps just to switch the graphics cards. I could simply use my regular application launcher (e.g application menu, gnome-do etc) and execute the script just as I would execute a GUI application.

Conclusion

As you can see, I mentioned a wide range of examples. Purpose of which is to encourage you to create your own launcher for applications you run. There are much more parameters and variables that can be used on a desktop file, which you can see if you explore files at /usr/share/applications. To cover even more use cases, see Desktop Entry Specification. The power of desktop files is limitless. E.g I could create launcher for all my nintento 64 roms (using mupen64plus). I could create desktop files to launch a script that mixes a series of commands and applications. I could create several versions of same application, depending upon different parameters. The point is, there is nothing wrong with using the terminal to execute commands, but sometimes, it is less intimidating and more enjoyable to press a button and see stuffs happen.

Cheers!!