Ficheiros de escritorio: poñer o seu aplicativo nos menús do escritorio

To run applications from GNOME, users click on icons on their desktops or they select the applications which they want to run from a menu. Therefore, the first step in integrating an existing program with GNOME is to register it with the set of applications that users can run.

Unlike in Windows or MacOS, in GNOME the users menus are automatically constructed from the list of registered applications. Each published application specifies a set of categories to which it belongs, and the systems menu configuration sorts and arranges them. This mechanism follows the freedesktop.org desktop entry and menu standards.

Though common in other desktops, creating your own application-specific submenu is not recommended. Instead, provide one menu item for each application you ship. Extra items such as help files, READMEs or links to your web site should be embedded into the application itself.

In GNOME and other freedesktop.org-compliant desktops, an application gets registered into the desktop's menus through a desktop entry, which is a text file with .desktop extension. This desktop file contains a listing of the configurations for your application. The desktop takes the information in this file and uses it to:

  • poña o aplicativo no Menú principal.
  • liste o aplicativo no diálogo Executar aplicativo....
  • cree os iniciadores axeitados no menú ou no escritorio
  • asocie o nome e a descrición do aplicativo.
  • use a icona axeitada.
  • recoǹeza o tipo MIME que admite para abir ficheiros.

To add a menu entry for your application, create a desktop file. It should have a unique filename, and there are no length limits so avoid abbreviations and feel free to include brand names. However, don't put spaces or international characters in the file name. For instance, "foocorp-painter-pro.desktop" would be a good filename to choose but "fcpp.desktop" would be a bad name, as would "FooCorp Painter Pro.desktop". The file should be UTF-8 encoded, and should resemble the following template:

	[Desktop Entry]
	Encoding=UTF-8
	Name=FooCorp Painter Pro
	Exec=foocorp-painter-pro
	Icon=foocorp-painter-pro
	Type=Application
	Categories=Application;
	

These desktop files contain metadata about your application, and play a central role in integrating the program with the GNOME and other standards compliant desktops. The template presented here is the most basic possible. The file can be linguistically translated so your applications name can appear in the user's native language.

Place this file in the /usr/share/applications directory so that it is accessible by everyone, or in ~/.local/share/applications if you only wish to make it accessible to a single user. Which is used should depend on whether your application is installed systemwide or into a user's home directory. GNOME monitors these directories for changes, so simply copying the file to the right location is enough to register it with the desktop. 1

Each working desktop file needs to follow the same format. A minimal example of a desktop file is shown in Exemplo 1-1. The file is split into sections, each starting with the section descriptor in square brackets. In this example, only one section is shown as that is the essential section to integrating your application to the desktop. Within each section, the part of each line before the equal sign is the key while the second half is the value. An explanation of each line is shown in Táboa 1-1.

Other than the first line identifying the desktop file, the order of the lines is not important. In Exemplo 1-1, the line Type=Application could be the second row, the fifth row, or the last row and the result would be the same.

However, the keys are case sensitive. Type=Application is not the same as type=Application or TYPE=Application.

Exemplo 1-1Ficheiro de escritorio de exemplo
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Nome de exemplo de aplicativo
Comment=O mesmo aplicativo
Exec=application
Icon=application.png
Terminal=false
Táboa 1-1Explicacións liña a liña
Liña Descrición
[Desktop Entry] A primeira liña de todo ficheiro de escritorio e a sección de cabeceira para identificar o bloque de pares de chaves asociados co escritorio. Necesario para que o escritorio recoñeza o ficheiro correctamente.
Type=Application Dílle ao escritorio que este ficheiro de escritorio pertence a un aplicativo. Outros valores válidos para esta chave son Ligazón e Cartafol.
Encoding=UTF-8 Describe a codificación das entradas neste ficheiro de escritorio.
Name=Nome de exemplo de aplicativo Dalle o nome ao seu aplicativo para o menú principal e calquera iniciador.
Comment=Un aplicativo de exemplo Describe o aplicativo. Usando un consello.
Exec=application A orde que inicia o aplicativo desde consola. Pode ter argumentos.
Icon=application.png O nome da icona asociada con este aplicativo.
Terminal=false Describe se o aplicativo se debería executar nun terminal.

1.1.1. Iniciar o seu aplicativo

Se o seu aplicativo pode levar argumentos desde a liña de ordes, pode detallalo usando os campos mostrados na Táboa 1-2

Táboa 1-2Variábeis de execución
Engadir… Acepta…
%f un só nome de ficheiro.
%F múltiples nomes de ficheiro.
%u un só URL.
%U múltiples URL.
%d un só cartafol. Usado xunto con %f para localizar un ficheiro.
%D múltiples cartafoles. Usado xunto con %F para localizar ficheiros.
%n un só nome de ficheiro sen ruta.
%N múltiples ficheiros sen rutas.
%k un URI ou nome de ficheiro local para a localización do ficheiro de escritorio.
%v o nome do dispositivo de entrada.

1.1.2. Idiomas estranxeiros

Para crear nomes e comentarios localizados débese engadir liñas adicionais para cada local necesaria. Por exemplo, para engadir a versión en galego do comentario engada a seguinte liña

Comment[gl]=Exemplo do nome do programa

Non existen límites no número de traducións para o ficheiro.

Xa que manter unha longa lista de traducións nun ficheiro é moi ineficaz, unha mellor forma de crear estas traducións é usar o paquete intltook. Para obter máis información consulte as páxinas do manual de intltool-extract e intltool-merge.

1.1.3. Referencias

Especificación de entradas de escritorio; Especificacións para crear un ficheiro de escritorio.

1

Note that the ~/.local/share/applications location is not monitored by versions of GNOME prior to version 2.10 or on Fedora Core Linux, prior to version 2.8. These versions of GNOME follow the now-deprecated vfolder standard, and so desktop files must be installed to ~/.gnome2/vfolders/applications. This location is not supported by GNOME 2.8 on Fedora Core nor on upstream GNOME 2.10 so for maximum compatibility with deployed desktops, put the file in both locations.

Note that the KDE Desktop requires one to run kbuildsycoca to force a refresh of the menus.