How to Create Custom Desktop Files For Launchers on Linux

Objective

Create a custom desktop file to launch a program graphically.

Distributions

This will work on all Linux distributions.

Requirements

A working Linux install with a graphical desktop

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

Have you ever installed a program from an outside source and wished that it had a convenient entry in your desktop environment’s menu? More often than not, Linux applications installed through any channel other than a software repository don’t include a proper desktop entry. Don’t worry, though. They’re actually quite simple to create for yourself.

The entries that you see in your desktop environment’s program menu come from simple plain text configuration files with the .desktop extension. These files all follow a very basic pattern, and they’re not too long to write in just a few minutes.

Create The File

The .desktop entries for your user account are stored at ~/.local/share/applications. cd into that directory, and have a look around. You’ll probably see a few files already in there.

Create a new file for your program. Name it something like program-name.desktop. This guide is going to use the Tor Browser as an example. Feel free to follow along with that. Open your new file in the text editor of your choosing.

On the first line of the file, begin the block for a desktop entry. It should look like this:

[Desktop Entry]


Basic Info

Now, you can start adding some basic information to your file. Begin by assigning a name to your program.

Name=Tor Browser

Every X window has it’s own class for identification purposes. Give your entry a unique class.

StartupWMClass=Tor Browser

When you mouse over launchers in most desktops, a small dialog box will appear with a short description of the program. Create that entry next.

Comment=Browse With Tor

Most desktops also support generic names for programs. These usually just say what the program is. In this case:

GenericName=Web Browser

Execution

Every desktop file needs a command to execute. Otherwise, what good really is it? The command should really just point to the executable file that you want to run with the launcher and include any options that you want to pass when it’s run. Think of it like you’re running the program from the linux command line. Essentially, that’s what you’re doing every time you click on your launcher.

Exec=/home/user/tor-browser_en-US/Browser/start-tor-browser

If you were to copy that command into the terminal and run it, the browser would open. That’s exactly the behavior that you’re looking for.

Icons

Launchers aren’t much good without an icon to click. You can use just about any image as an icon, but smaller PNGs with a transparent background work best. Your image can be stored just about anywhere. You only need to include the path to it for your entry.

Icon=/home/user/tor-browser_en-US/Browser/browser/icons/mozicon128.png

The Tor Browser comes with an icon buried deep in its directories. That’s the actual path to it.



Type And Catagories

The type and categories values tell any menus where to place your launcher. The type tells the desktop environment if the desktop entry is for an application, link, or directory. The categories value lets it know which categories to place your launcher under.

Type=Application
Categories=Network;WebBrowser;

For a full list of categories that desktop environments recognize, check the offical specification.

MIME Types

MIME types tell the desktop environment which files to associate with the application. These are organized by category, then by extension. You can have as many as you like. Check out VLC. The list is rather long.

MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/vnd.mozilla.xul+xml; application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;

If you’re unsure of what to include, take a look at a similar application. This example was copied directly from Firefox.

Keywords

Keywords aren’t that important. They’re additional metadata that a desktop environment or its utilities can use to search for your entry. They sort of extend the Name and GenericName values, giving you more words to associate with your application.

Keywords=Tor;Private;Onion;Browse;Internet;
Tor Browser Desktop Entry

Tor Browser Desktop Entry

Closing Thoughts

You are now fully prepared to write your own desktop entries for any application you can think of. These are also a great option for custom scripts and Wine games. Projects downloaded from Github also rarely come with .desktop files. It’s fairly simple to set one of these up for any one of those scenarios.



Comments and Discussions
Linux Forum