翻译状态:本文是 Desktop entries翻译。上次翻译日期:2022-11-01。如果英文版本有所更改,则您可以帮助同步翻译。

Tango-preferences-desktop-locale.png本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队Tango-preferences-desktop-locale.png

附注: 请持续完成/改进翻译(在 Talk:Desktop entries (简体中文)# 中讨论)

XDG 桌面配置项规范定义了一个标准,用于在实现了XDG 桌面菜单规范的桌面环境里将应用程序集成到应用程序菜单中。(译注:下文中“XDG 桌面配置项”简称为“桌面项”)

基础

每个桌面项必须具有一个Type关键字和一个Name关键字。可以定义桌面项在应用程序菜单中的外观(非必须)。

桌面项可用的有效类型有三种:

Application
定义如何启动应用程序以及它支持的 MIME 类型(由 XDG MIME Applications 使用)。 使用XDG Autostart应用程序项可以通过将它们放在特定目录中以实现自动启动。应用程序项使用.desktop扩展名。 请参阅 #应用程序项
Link
定义URL的快捷方式。链接项使用.desktop扩展名。
Directory
定义应用程序菜单中子菜单的外观。目录项使用.directory扩展名。

以下部分将大致解释这些是如何创建和验证的。

应用程序项

应用程序的桌面项或.desktop文件通常是元信息资源和应用程序快捷方式的组合。对于系统范围内安装的应用程序,这些文件通常位于 /usr/share/applications//usr/local/share/applications/。而~/.local/share/applications/用于用户特定的应用程序。后者(用户条目)优先于前者(系统条目)。

文件示例

下面是一个带有附加注释的结构示例。 这个例子只是为了给人一个快速的印象,并没有展示如何使用所有可能的关键字。完整的关键字清单可在 freedesktop 规范 中找到。

[Desktop Entry]

# type关键字如上所述
Type=Application

# 本文件所遵循的桌面项规范版本
Version=1.0

# 应用程序的名称
Name=jMemorize

# 显示为工具提示的注释
Comment=Flash card based learning tool

# 可执行文件所在的目录
Path=/opt/jmemorise

# 可执行文件,可以带参
Exec=jmemorize

# 图标名称
Icon=jmemorize

# 应用程序是否需要运行在终端中
Terminal=false

# 本桌面项将显示在哪些分类中
Categories=Education;Languages;Java;
注意: 只有TypeName是必须的。

关键字赋值

所有可用的关键字都可以在freedesktop网站上找到。

例如,Type关键字定义了三种类型的桌面项:应用程序(type 1)、链接(type 2)和目录(type 3)。

  • Version 关键字不代表应用程序的版本,而是代表该文件符合的桌面项规范的版本。
  • NameGenericNameComment 通常包含它们组合形式的冗余值,例如:
Name=Pidgin Internet Messenger
GenericName=Internet Messenger

或者

Name=NoteCase notes manager
Comment=Notes Manager

应该避免这种情况,因为它只会让用户感到困惑。Name关键字应仅包含名称,或者可能是缩写/首字母缩略词(如果可用)。

  • GenericName 应该说明您通常所说的应用程序,该应用程序执行此特定应用程序提供的功能(即 Firefox 是“Web 浏览器”)。
  • Comment 旨在包含任何有用的附加信息。

验证

随着时间的推移,某些关键字已被弃用,您可能需要使用 desktop-file-validate(1) 来验证您的桌面项,它是 desktop-file-utils 软件包的一部分。要验证,请运行:

$ desktop-file-validate <your desktop file>

这将为您提供非常详细且有用的警告和错误消息。

安装

使用 desktop-file-install(1) 将桌面文件安装到目标目录。例如:

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

刷新桌面项数据库

要使~/.local/share/applications中定义的桌面项生效,请运行以下命令:

$ update-desktop-database ~/.local/share/applications
提示: 添加 -v(详细)参数以显示可能的桌面输入错误。

图标

参阅图标主题规范

通用图标格式

以下是图标常用图像格式的概述。

支持 freedesktop.org 标准指定的图标图像格式
扩展名 全称和/或说明 图像类型 是否容器型 是否支持
.png 便携式网络图形 位图
.svg(z) 可缩放矢量图形 矢量图 是(可选)
.xpm X PixMap 位图 是(不推荐)
.gif 图像互换格式 位图
.ico Windows图标格式 位图
.icns Apple图标格式 位图

图标格式转换

Merge-arrows-2.pngThis article or section is a candidate for merging with ImageMagick#Usage.Merge-arrows-2.png

Notes: 应归属该文。 (Discuss in Talk:Desktop entries (简体中文))

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 <icon name>.gif <icon name>.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 <icon name>-<number>.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 <icon name>.ico

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

$ icns2png -x <icon name>.icns

获取图标

附带“.desktop”文件的软件包肯定包含一个或一组图标。有时开发人员虽然没有创建“.desktop”文件,但可能会附带图标。所以一个好的开始是在源包中寻找图标。

您可以首先使用 find 过滤扩展名,然后使用 grep 进一步过滤某些常用关联词,如包名、“icon”、“logo”等,源码包中可能有很多图片。

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

如果应用程序的开发人员没有在他们的源包中包含图标,那么下一步就是在他们的网站上进行搜索。

一些项目,例如 tvbrowserAUR 有一个 艺术品/徽标页面,可以在其中找到其他图标。如果一个项目是多平台的,可能会出现这样的情况,即使 linux/unix 包没有附带图标,Windows 包也可能提供图标。如果项目使用 版本控制系统,如 CVS/SVN/等。并且您对它有一些经验,您也可以考虑浏览它以获取图标。

如果一切都失败了,该项目可能还没有图标/徽标。

图标文件路径

freedesktop.org 标准 规定了程序应以何种顺序和目录查找图标:

  1. $HOME/.icons(为了向后兼容)
  2. $XDG_DATA_DIRS/icons
  3. /usr/share/pixmaps

工具

arronax

Arronax 是一个图形程序,用于创建和修改应用程序和位置的桌面项。

alacarte

alacarte 是一个使用 freedesktop.org 菜单规范的 GNOME 图形菜单编辑器。它还支持覆盖桌面项。

gendesk

gendesk 最初是一个 Arch Linux 特定的工具,用于通过直接从 PKGBUILD 文件中获取所需信息来生成 .desktop 文件。 现在它是一个接受命令行参数的通用工具。

可以从 这个网站 自动下载图标(如果网站可用的话)。(图标的来源是可配置的)。

用法

  • gendesk 添加到 makedepends
  • 启动 prepare() 功能:
gendesk --pkgname "$pkgname" --pkgdesc "$pkgdesc"
  • 或者,如果已经提供了一个图标(例如,$pkgname.png),可以指定 -n 参数限定不下载图标或使用默认图标。例如:
gendesk -n --pkgname "$pkgname" --pkgdesc "$pkgdesc"
  • $srcdir/$pkgname.desktop 将被创建并安装在 package() 函数中:
install -Dm644 "$pkgname.desktop" "$pkgdir/usr/share/applications/$pkgname.desktop"
  • 图标可以这样安装:
install -Dm644 "$pkgname.png" "$pkgdir/usr/share/pixmaps/$pkgname.png"
  • --name='程序名称' 这个参数指定程序在菜单中的名称。
  • --exec='/opt/some_app/elf --some-arg --other-arg' 这种形式设置可执行文件字段。

lsdesktopf

lsdesktopfAUR可列出可用的“.desktop”文件或搜索它们的内容。

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

它还可以执行 MIME 类型相关的搜索。参阅XDG MIME Applications#lsdesktopf

fbrokendesktop

fbrokendesktopAUR Bash 脚本用于检测指向不存在路径的无效Exec值。没有任何参数,它使用 DskPath 数组中的预设目录。它仅显示完整路径和文件名丢失的无效“.desktop”。

例如

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

提示与技巧

从终端运行一个桌面项

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

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

编辑 .desktop 文件

For system-wide .desktop files (e.g. those installed from a package), first copy the relevant .desktop file (e.g. from /usr/share/applications/) to $XDG_DATA_HOME/applications/ (e.g. ~/.local/share/applications/). This prevents your changes from being overwritten when the package gets updated during system upgrades. The local user-specific .desktop files should automatically take precedence over the system-wide files. Now you can modify the local user-specific .desktop file as needed.

注意: If you have configured this application to autostart, there may already be a copy of the system desktop entry in $XDG_CONFIG_HOME/autostart, as per XDG Autostart. If you want to modify the behavior only while autostarting, proceed to edit this file. Otherwise, if you want to modify the behavior in all circumstances, you should:
  • Move the desktop file from $XDG_CONFIG_HOME/autostart/ to $XDG_DATA_HOME/applications.
  • Using ls -l, make sure that this desktop entry is a regular file you can edit, and not a link to the system location. If it is a link, replace it with a copy of the original as described above.
  • Make a symbolic link to the user application directory (default XDG dirs substituted for convenience):
ln -s ~/.local/share/applications/desktop_entry ~/.config/autostart/

Now, the file in your application launcher will stay the same as the one that is autostarted.

编辑环境变量

To set environment variables, in the .desktop file, edit the Exec= line to first use the env(1) command to set your variables. For example, with the original line commented out:

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

编辑命令行参数

To change or add the command line arguments, edit the Exec= line to append the desired options. As an example, with the original line commented out:

~/.local/share/applications/steam.desktop
...
# Exec=/usr/bin/steam-runtime %U
Exec=/usr/bin/steam-runtime -no-browser %U
...

隐藏桌面项

The visibility of the desktop entry can be controlled in multiple ways. See the Desktop Entry Specification for more information. Add one of the following lines to your .desktop file:

  • All desktop environments, choose one (or both) of the following:
    • Add the line NoDisplay=true for applications that you do not want displayed in the menus.
    • Add the line Hidden=true for applications that you consider deleted and do not want displayed in the menus.
  • Specified desktop environments, choose one of the following where desktop_names is a semicolon-delimited list of desktop environments (e.g. GNOME, GNOME;Xfce;KDE;):
    • Add the line NotShowIn=desktop_names to hide the entry only in the specified desktop environments.
    • Add the line OnlyShowIn=desktop_names to show the entry only in the specified desktop environments.

参阅