桌面项文件:将您的应用程序放在桌面菜单中。
要从 GNOME 中运行应用程序,用户需要点击他们桌面上的图标或者从菜单中选择。因此,将现有程序集成到 GNOME 的第一步就是将其 注册 为一组用户可用的程序。
与 Windows 和 MacOS 不同,GNOME 中,用户菜单从注册了的应用程序列表自动组织。每个发布的应用程序指定其属于的一组类别,系统菜单配置对其进行排序和排列。此机制遵守 freedesktop.org 桌面想和菜单标准。
虽然在其它桌面环境中很普遍,但是不推荐创建自己应用程序的子菜单。而是为您安装的每个程序提供一个菜单项。额外的项,比如帮助文件、README 或者到您网站的链接应该嵌入到应用程序自身中。
在 GNOME 和其它兼容 freedesktop.org 的桌面环境中,应用程序通过 桌面项 注册到桌面菜单,桌面项是一个文本文件,使用 .desktop 作为扩展名。此桌面项文件包含了您应用程序的一系列配置。桌面从此文件中提取信息并将其用于:
- 在 中放置应用程序。
- 在 运行应用程序... 对话框中列出应用程序
- 在菜单或桌面上创建适当的启动器。
- 组合应用程序的名称和描述
- 使用合适的图标。
- 识别它支持的 MIME 类型以便打开。
要为您的程序添加菜单项,请创建一个桌面项文件。它应该有一个独特的文件名,并且没有长度限制,所以请避免缩写,可以随便添加商标。然而,不要在文件名中使用空格和国际化字符。例如,"foocorp-painter-pro.desktop" 是一个比 "fcpp.desktop" 好的名称,"FooCorp Painter Pro.desktop" 也不是一个不好的文件名。文件应该是 UTF-8 编码的,并且应该遵守如下模板:
[Desktop Entry] Encoding=UTF-8 Name=FooCorp Painter Pro Exec=foocorp-painter-pro Icon=foocorp-painter-pro Type=Application Categories=Application;
这些桌面项文件包含应用程序的元数据,在将程序和 GNOME 及其它兼容于标准的桌面环境集成中,起核心作用。此处给出的模板是最基本的。此文件可以包含翻译,以使用用户的本地语言显示程序名。
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
每个可以工作的桌面项文件需要遵守同样的格式。在 例 1-1 中给出了一个很小的桌面项文件示例。此文件被分为几个节,每部分以方括号扩起来的节描述开始。这个例子中,只有一个节,这是一个将桌面和程序集成的关键节。在每个节中,每行中等号之前的部分是 键,而另一部分是 值。在 表 1-1 中,有对于每一行的解释。
除了标识桌面项文件的第一行,行的顺序并不重要。在 例 1-1 中,Type=Application 行是第二行,第五行或者是最后一行都无所谓。
然而,键是区分大小写的。Type=Application 与 type=Application 或 TYPE=Application 并不相同。
[Desktop Entry] Type=Application Encoding=UTF-8 Name=Sample Application Name Comment=A sample application Exec=application Icon=application.png Terminal=false
行 | 解释 |
---|---|
[Desktop Entry] | 每个桌面项文件的第一行和每节的开头,标识与桌面集成的键值对块,是桌面正确识别文件所必须的。 |
Type=Application | Tells the desktop that this desktop file pertains to an application. Other valid values for this key are Link and Directory. |
Encoding=UTF-8 | 描述此桌面文件中的编码 |
Name=Sample Application Name | 主菜单和任何启动器中您的应用程序的名称。 |
Comment=A sample application | 描述这个应用程序。使用的是工具提示。 |
Exec=application | 从 shell 中启动此应用程序的命令,可以有参数。 |
Icon=application.png | 与此应用程序关联的图标名。 |
Terminal=false | 说明程序是否在终端中运行。 |
1.1.2. 其它语言
要创建一个本地化的名称和注释,需要为每个区域添加行。例如,要添加中国大陆简体中文版的注释,添加如下行:
Comment[sv]=Exempelprogramnamn
在文件中,没有翻译行的数目限制。
因为在一个文件中维护一个很长的翻译列表是很笨重的,一种更好的创建这些翻译的方法是使用 intltool 包。查看 intltool-extract 和 intltool-merge 手册页获取更多信息。
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.