desktop entry文件介绍
The XDG Desktop Entry specification defines a standard for applications to integrate into application menus of desktop environments implementing the XDG Desktop Menuspecification.
一个desktop entry文件代表一个桌面环境中的一个菜单项,它的种类可以是应用、子菜单和链接。
对于应用和链接,文件扩展名为*.desktop,对于子菜单,文件扩展名为*.directory,虽然directory意为目录,但是却是作为子菜单的扩展名。
一些背景介绍
XDG指的是X Development Group, 这是Freedesktop.org的旧称,关于freedesktop的一些规定可以在https://specifications.freedesktop.org上找到。
desktop文件位置
先来看一下XDG Base Directory Specification定义的一些环境变量:
$XDG_DATA_HOME- 用户数据存放的基准目录,如果没有设置,默认值为$HOME/.local/share$XDG_CONFIG_HOME- 用户配置存放的基准目录,默认为$HOME/.config$XDG_DATA_DIRS- 除了$XDG_DATA_HOME目录之外,一系列按优先级排序的基准目录,这些目录也是用于查找用户数据文件的。默认值为/usr/local/share:/usr/share$XDG_CONFIG_DIRS- 除了$XDG_CONFIG_HOME目录之外,一系列按优先级排序的基准目录,用于查找配置文件。默认值为/etc/xdg$XDG_CACHE_HOME- 用于存放用户的non-essential (cached) data。默认$HOME/.cache
一般来说,建议用户自己的文件放在~/.local/share和~/.config目录下。
desktop entry文件放在$XDG_DATA_HOME和$XDG_DATA_DIRS 下面的applications/ 或者desktop-directories/,分别对应上面说到的三种类型。
desktop文件格式
先上个例子:/usr/share/applications/gedit.desktop
[Desktop Entry]
Name=gedit
GenericName=Text Editor
Comment=Edit text files
Exec=gedit %U
Terminal=false
Type=Application
StartupNotify=true
Icon=accessories-text-editor
Categories=GNOME;GTK;Utility;TextEditor;
X-GNOME-DocPath=gedit/gedit.xml
X-GNOME-FullName=Text Editor
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=gedit
X-GNOME-Bugzilla-Component=general
X-GNOME-Bugzilla-Version=3.18.3
X-GNOME-Bugzilla-ExtraInfoScript=/usr/share/gedit/gedit-bugreport
Actions=new-window;new-document;
Keywords=Text;Editor;Plaintext;Write;
X-Ubuntu-Gettext-Domain=gedit
X-AppStream-Ignore=true
[Desktop Action new-window]
Name=Open a New Window
Exec=gedit --new-window
[Desktop Action new-document]
Name=Open a New Document
Exec=gedit --new-document
从上面的例子可以看到,除了几行用方括号括住以外,*.desktop/*.directory文件的内容大部分都是一些形如key=value的键值对,参考官方文档,下面列出几个常用的,
| Keyword | value |
|---|---|
| Type | 为Application, Link或Directory |
| Name | 程序名 |
| Comment | 注释 |
| Icon | 图标,参见Icon Theme Specification |
| Exec | 执行程序的命令,只有Application类有,具体请看文档 |
| Categories | entry 应用所属的种类,使用分号分隔 |
| URL | 用于Link类型的菜单项 |
注意上面的并不是所有的菜单项都有的,URL就只是Link类型特有,具体查看官方文档。
至于方括号之内的内容,叫做Group Header,常用的就是Desktop Entry和Desktop Action <动作名称>。试试在gedit的图标上右键,你看到了什么?是不是看到了两个选项:Open a new window和Open a new document。 在看看上面的desktop文件。我想你已经知道了,Desktop Action可用来添加操作,如果选择了Open a new window就执行gedit --new-window,如果选择了Open a new document就执行gedit --new-document,当然别忘了把这两个操作加入进去:Actions=new-window;new-document;。
自己动手写
一般来说,用户自定义的desktop文件建议都放在用户的~/.local/share里面。
编写url desktop entry文件示例
~/.local/share/applications/github.desktop文件内容如下
[Desktop Entry]
Type=Link
Name=github
URL=https://www.github.com
Icon=github
图片文件是~/.local/share/pixmaps/github.png,当然可以放在别的可以找到的地方,查看Icon Theme Specification
这样一个Link类型的desktop entry文件就创建好了,注意unity和gnome都不会在菜单中显示Link类型的菜单项,不过在文件管理器nautilus里面还是可以看到带有图标的链接的,点击也能打开浏览器跳转到github。
编写application desktop entry 文件示例
一个用于编辑~/note.txt的desktop entry
~/.local/share/applications/note.desktop
[Desktop Entry]
Type=Application
Name=Note
Icon=accessories-text-editor
Exec=bash -c "gedit ~/note.txt"
下面是两个示例的结果图,其中Note可以 在菜单上找到

参考链接
XDG Desktop Entry specification
本文介绍了XDG Desktop Entry规范,该规范定义了应用程序如何融入桌面环境的菜单。内容包括:desktop entry文件的用途(应用、子菜单和链接),文件扩展名,文件位置,文件格式,以及如何编写URL和Application类型的desktop entry文件示例。
462

被折叠的 条评论
为什么被折叠?



