Desktop file-install

All systems curl cmd.cat/desktop-file-install.sh

Debian apt-get install desktop-file-utils

Ubuntu apt-get install desktop-file-utils

Alpine apk add desktop-file-utils

Arch Linux pacman -S desktop-file-utils

Kali Linux apt-get install desktop-file-utils

CentOS yum install desktop-file-utils

Fedora dnf install desktop-file-utils

OS X brew install desktop-file-utils

Raspbian apt-get install desktop-file-utils

Docker docker run cmd.cat/desktop-file-install desktop-file-install powered by Commando

Note: help and version output are generated by a naïve script which tries a few variants of --help, -h etc. to find the command's help and version info. Sometimes it gets lucky, sometimes it doesn't; if the output below looks wrong, it probably is.

Error on file "version": Key file contains line 'BZh91AY&SYKaiQ' which is not a key-value pair, group, or comment

Usage: desktop-file-install [OPTION...] [FILE...] Install desktop files. Help Options: -h, --help Show help options --help-all Show all help options --help-install Show desktop file installation options --help-edit Show desktop file edition options Application Options: --rebuild-mime-info-cache Rebuild the MIME types application database after processing desktop files

A .desktop file is simply a shortcut that is used to launch application in Linux. Without the .desktop file, your application won’t show up in the Applications menu and you can’t launch it with third-party launchers such as Synapse and Albert.

Most applications, when installed, will create the .desktop file automatically and place themselves in the Application menu for quick access. However, if you compile a program from source or download an app in archive format, this may not be the case and you may have to open the terminal to execute the binary every time you want to use it. Obviously, this can become a very tedious and troublesome step.

This article will describe how you can create a .desktop file for any application you use that you can launch from the Application menu.

How to create Desktop launchers

A “.desktop” file is basically a simple text file that holds information about a program. It is usually placed in “~/.local/share/applications” or “/usr/share/applications/” depending whether you want the launcher to be accessible only for local account or for everyone. If you navigate to either directory in your File manager, you will see quite a few “.desktop” files that correspond to the installed apps on your computer.

For demonstration purposes, I will create a .desktop file for Super Tux Kart, a kart racing game which I like to play sometimes. A version is available in the Ubuntu repos, but this is often behind the latest stable version.

The only way to get the latest and greatest release is by downloading a tar archive, extracting it and executing a file which will launch the game.

You can follow along with whatever program you want to create a launcher for and it should work the same way.

Note: The following steps assume you have the archive for the program you want to create a launcher for in your “Downloads” folder.

1. Navigate to the directory where the archive is stored, right-click it and select “Extract here”.

2. Once the extraction is complete, Change to the newly created folder and find the executable. One you find it, right-click it click “Run” to launch the program, just to make sure it is working.

3. In some cases, you won’t see the “Run” option in the menu. This is often because the executable is a text file. You can get around this by executing it via the terminal or, if you use GNOME, click the Files menu in the top bar, and select “Preferences”.

4. Select the “Behavior” tab and choose the “Run them” option under “Executable Text Files”. Now the “Run” option should appear when you right-click the executable text file.

5. If you’ve confirmed that the application works when you launch it, you can exit it. Then launch your text editor and paste the following into the empty text file:

You need to change the “Exec” field to the path to the executable and “Name” field to the name of the application. Most programs would provide an icon somewhere in the archive so don’t forget to include that as well. In my case, the launcher file for Super Tux Kart looks like this:

6. Save the file in “~/.local/share/applications” folder as “application-name.desktop”. The “.local” folder is a hidden folder in your Home directory and you will have enable “Show Hidden Files” mode to view it. If you want it to be globally accessible, run the following command in the terminal:

Of course, don’t forget to change the to the actual name of the .desktop file.

7. Once done, just open the Applications menu on your desktop, and the application will be right there to use.

The method described here should work on all mainstream Linux based operating systems. Here’s another screenshot showing Super Tux Kart in elementary OS’s application launcher [slingshot]

Feel free to leave a comment below if you found this tutorial helpful.

The XDG Desktop Entry specification defines a standard for applications to integrate into application menus of desktop environments implementing the XDG Desktop Menu specification.

Basics

Each desktop entry must have a Type and a Name key and can optionally define its appearance in the application menu.

The three available types are:

Application Defines how to launch an application and what MIME types it supports [used by XDG MIME Applications]. With XDG Autostart Application entries can be started automatically by placing them in specific directories. Application entries use the .desktop file extension. See #Application entry. Link Defines a shortcut to a URL. Link entries use the .desktop file extension. Directory Defines the appearance of a submenu in the application menu. Directory entries use the .directory file extension.

The following sections will roughly explain how these are created and validated.

Application entry

Desktop entries for applications, or .desktop files, are generally a combination of meta information resources and a shortcut of an application. These files usually reside in /usr/share/applications/ or /usr/local/share/applications/ for applications installed system-wide, or ~/.local/share/applications/ for user-specific applications. User entries take precedence over system entries.

File example

Following is an example of its structure with additional comments. The example is only meant to give a quick impression, and does not show how to utilize all possible entry keys. The complete list of keys can be found in the freedesktop specification.

[Desktop Entry] # The type as listed above Type=Application # The version of the desktop entry specification to which this file complies Version=1.0 # The name of the application Name=jMemorize # A comment which can/will be used as a tooltip Comment=Flash card based learning tool # The path to the folder in which the executable is run Path=/opt/jmemorise # The executable of the application, possibly with arguments. Exec=jmemorize # The name of the icon that will be used to display this entry Icon=jmemorize # Describes whether this application needs to be run in a terminal or not Terminal=false # Describes the categories in which this entry should be shown Categories=Education;Languages;Java;

Note: Only Type and Name are required.

Key definition

All recognized entries can be found on the freedesktop site. For example, the Type key defines three types of desktop entries: Application [type 1], Link [type 2] and Directory [type 3].

  • Version key does not stand for the version of the application, but for the version of the desktop entry specification to which this file complies.
  • Name, GenericName and Comment often contain redundant values in the form of combinations of them, like:
Name=Pidgin Internet Messenger GenericName=Internet Messenger

or

Name=NoteCase notes manager Comment=Notes Manager

This should be avoided, as it will only be confusing to users. The Name key should only contain the name, or maybe an abbreviation/acronym if available.

  • GenericName should state what you would generally call an application that does what this specific application offers [i.e. Firefox is a "Web Browser"].
  • Comment is intended to contain any useful additional information.

Validation

As some keys have become deprecated over time, you may want to validate your desktop entries using desktop-file-validate[1] which is part of the desktop-file-utils package. To validate, run:

$ desktop-file-validate

This will give you very verbose and useful warnings and error messages.

Installation

Use desktop-file-install[1] to install desktop file into target directory. For example:

$ desktop-file-install --dir=$HOME/.local/share/applications ~/app.desktop

Update database of desktop entries

To make desktop entries defined in ~/.local/share/applications work, run the following command:

$ update-desktop-database ~/.local/share/applications

Tip: Add the -v [verbose] argument to show possible desktop entry errors.

Icons

See also the Icon Theme Specification.

Common image formats

Here is a short overview of image formats commonly used for icons.

Support for image formats for icons as specified by the freedesktop.org standard. Extension Full Name and/or Description Graphics Type Container Format Supported .png .svg[z] .xpm .gif .ico .icns
Portable Network Graphics Raster No Yes
Scalable Vector Graphics Vector No Yes [optional]
X PixMap Raster No Yes [deprecated]
Graphics Interchange Format Raster No No
MS Windows Icon Format Raster Yes No
Apple Icon Image Raster Yes No

Converting icons

If you stumble across an icon which is in a format that is not supported by the freedesktop.org standard [like gif or ico], you can use the convert tool [which is part of the imagemagick package] to convert it to a supported/recommended format, e.g.:

$ convert .gif .png

If you convert from a container format like ico, you will get all images that were encapsulated in the ico file in the form -.png. If you want to know the size of the image, or the number of images in a container file like ico you can use the identify tool [also part of the imagemagick package]:

$ identify /usr/share/vlc/vlc48x48.ico /usr/share/vlc/vlc48x48.ico[0] ICO 32x32 32x32+0+0 8-bit DirectClass 84.3kb /usr/share/vlc/vlc48x48.ico[1] ICO 16x16 16x16+0+0 8-bit DirectClass 84.3kb /usr/share/vlc/vlc48x48.ico[2] ICO 128x128 128x128+0+0 8-bit DirectClass 84.3kb /usr/share/vlc/vlc48x48.ico[3] ICO 48x48 48x48+0+0 8-bit DirectClass 84.3kb /usr/share/vlc/vlc48x48.ico[4] ICO 32x32 32x32+0+0 8-bit DirectClass 84.3kb /usr/share/vlc/vlc48x48.ico[5] ICO 16x16 16x16+0+0 8-bit DirectClass 84.3kb

As you can see, the example ico file, although its name might suggest a single image of size 48x48, contains no less than 6 different sizes, of which one is even greater than 48x48, namely 128x128.

Alternatively, you can use icotool [from icoutils] to extract png images from ico container:

$ icotool -x .ico

For extracting images from .icns container, you can use icns2png [provided by libicns]:

$ icns2png -x .icns

Obtaining icons

Although packages that already ship with a .desktop file most certainly contain an icon or a set of icons, there is sometimes the case when a developer has not created a .desktop file, but may ship icons, nonetheless. So a good start is to look for icons in the source package. You can i.e. first filter for the extension with find and then use grep to filter further for certain buzzwords like the package name, "icon", "logo", etc, if there are quite a lot of images in the source package.

$ find /path/to/source/package -regex ".*\.\[svg\|png\|xpm\|gif\|ico\]$"

If the developers of an application do not include icons in their source packages, the next step would be to search on their web sites. Some projects, like i.e. tvbrowserAUR have an artwork/logo page where additional icons may be found. If a project is multi-platform, there may be the case that even if the linux/unix package does not come with an icon, the Windows package might provide one. If the project uses a Version control system like CVS/SVN/etc. and you have some experience with it, you also might consider browsing it for icons. If everything fails, the project might simply have no icon/logo yet.

Icon path

The freedesktop.org standard specifies in which order and directories programs should look for icons:

  1. $HOME/.icons [for backwards compatibility]
  2. $XDG_DATA_DIRS/icons
  3. /usr/share/pixmaps

gendesk

gendesk started as an Arch Linux-specific tool for generating .desktop files by fetching the needed information directly from PKGBUILD files. Now it is a general tool that takes command-line arguments.

Icons can be automatically downloaded from openiconlibrary, if available. [The source for icons is configurable].

How to use

  • Add gendesk to makedepends
  • Start the prepare[] function with:
gendesk --pkgname "$pkgname" --pkgdesc "$pkgdesc"
  • Alternatively, if an icon is already provided [$pkgname.png, for instance]. The -n flag is for not downloading an icon or using the default icon. Example:
gendesk -n --pkgname "$pkgname" --pkgdesc "$pkgdesc"
  • $srcdir/$pkgname.desktop will be created and can be installed in the package[] function with:
install -Dm644 "$pkgname.desktop" "$pkgdir/usr/share/applications/$pkgname.desktop"
  • The icon can be installed with:
install -Dm644 "$pkgname.png" "$pkgdir/usr/share/pixmaps/$pkgname.png"
  • Use --name='Program Name' for choosing a name for the menu entry.
  • Use --exec='/opt/some_app/elf --some-arg --other-arg' for setting the exec field.
  • See the gendesk project for more information.

lsdesktopf

lsdesktopfAUR can list available .desktop files or search their contents.

$ lsdesktopf $ lsdesktopf --list $ lsdesktopf --list gtk zh_TW,zh_CN,en_GB

It can also perform MIME-type-related searches. See XDG MIME Applications#lsdesktopf.

fbrokendesktop

The fbrokendesktopAUR Bash script detects broken Exec values pointing to non-existent paths. Without any arguments it uses preset directories in the DskPath array. It shows only broken .desktop with full path and filename that is missing.

Examples

$ fbrokendesktop $ fbrokendesktop /usr $ fbrokendesktop /usr/share/xsessions/icewm.desktop

Tips and tricks

Run a desktop file from a terminal

If gtk3 is installed, run gtk-launch application.desktop.

Or install the dex package and run dex /path/to/application.desktop.

Hide desktop entries

Firstly, copy the desktop entry file in question to ~/.local/share/applications to avoid your changes being overwritten on system upgrades. The visibility of the desktop entry can be controlled in multiple ways:

  • Add the NoDisplay=true line to the desktop entry file to hide the entry in all desktop environments.
  • Add the NotShowIn=desktop_names line to hide the entry in specific desktop environments.
  • Conversely, add the OnlyShowIn=desktop_names line to show the entry in specific desktop environments.

desktop_names is a semicolon-delimited list of desktop environments [e.g. GNOME, GNOME;Xfce;KDE;]. See "OnlyShowIn, NotShowIn" in the desktop entry specification for more information.

Modify environment variables

To set environment variables, copy the .desktop file from /usr/share/applications/ to $XDG_DATA_HOME/ [~/.local/share/applications/] and edit the Exec= command line by prepending env. For example:

~/.local/share/applications/abiword.desktop Exec=env LANG=he_IL.UTF-8 abiword %U

See also

  • Wikipedia:.desktop
  • Information for developers

Video liên quan

Chủ Đề