Desktop entries (简体中文) - ArchWiki

# Desktop entries (简体中文)

Related articles

翻译状态： 本文是英文页面 Desktop_entries 的翻译，最后翻译时间：2016-07-07，点击这里可以查看翻译后英文页面的改动。

自由桌面社区的桌面配置项规范为应用程序和桌面环境的整合提供了一个标准方法。桌面配置项是描述如何启动程序，如何处理数据的配置文件，它们还会和菜单规范同步作用，定义一个程序在菜单中的显示图标。

大部分桌面配置项是 `.desktop` 和 `.directory` 文件。本文将概述如何创建一个合规可用的桌面配置项，主要面向软件包发布者和维护者，但对软件开发者及其他人也会是有用的。

桌面配置项大致分为三类：

应用程序
  指向某个应用程序的快捷方式
链接
  指向某个网址的链接
目录
  某个菜单项元数据的容器

下列章节概述如何创建它们并使其生效。

`.desktop` 文件中还定义了数据文件的 MIME 类型关联。Default applications (简体中文) 介绍了它们的配置方法。

## 应用程序配置项

应用程序配置项，即 `.desktop` 文件是原信息资源和应用程序快捷图标的集合。系统程序的配置项通常位于 `/usr/share/applications` 或 `/usr/local/share/applications`目录，单用户安装的程序位于 `~/.local/share/applications` 目录，优先使用用户的配置项。

### 范例文件

下面是一个示例结构和注释，这个示例文件没有使用所有的键值，freedesktop.org 规范包含了完整的键值列表.

    [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.
    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;

### 关键字定义

全部有效的桌面配置项可参阅freedesktop.org网站。 举例：`Type`关键字（类型）定义了三类桌面项：应用程序（Application (type 1)），链接（Link (type 2)）和目录（Directory (type 3)）。

- `Version` （版本）关键字不是指应用程序的版本，而是本文件所遵循的桌面配置项规范的版本。

- `Name`（名称）、`GenericName`（通称）和`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 usefull additional information.

#### 过时的内容

There are quite some keys that have become deprecated over time as the standard has matured. The best/simplest way is to use the tool `desktop-file-validate` which is part of the package desktop-file-utils. To validate, run

    $ desktop-file-validate <your desktop file>

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

## 图标

参考 图标主题标准.

### 通用图像格式

用于图标的图像文件格式简述如下：

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 | Portable Network Graphics | Raster | No | Yes
.svg(z) | Scalable Vector Graphics | Vector | No | Yes (optional)
.xpm | X PixMap | Raster | No | Yes (不推荐)
.gif | Graphics Interchange Format | Raster | No | No
.ico | MS Windows Icon Format | Raster | Yes | No
.icns | Apple Icon Image | Raster | Yes | No

### 图标格式转换

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 libicnsAUR):

    $ icns2png -x <icon name>.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. tvbrowser 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 simple have no icon/logo yet.

## 工具

### 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 can easily be changed in the future).

#### 用法

- 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 --with-ponies'` for setting the exec field.

- See the gendesk project for more information.

### 在*.desktop文件目錄或搜索

該lsdesktopfAUR腳本列出可用*.desktop文件或在自己的搜索內容。它的主要目的是讓在可用程序控制台的簡要概述他們的命令行，並在`*.desktop`文件設置的類別。

例子

    # lsdesktopf
    # lsdesktopf --less
    # lsdesktopf --less gtk zh_TW,zh_CN,en_GB

List MIME-types or parts of MIME-types found in .desktop, and which desktop files shares same MIME-type, output is equal as in mimeapps.list for section `[Added Associations]`.

    # lsdesktopf --gm
    # lsdesktopf --gm [options]

欲了解更多的選擇使用 `lsdesktopf --help`.

### fbrokendesktop

The fbrokendesktopAUR bash script using command "which" to detect broken Exec that points to not existing path. Without any parameters it uses preset folders in "DskPath" array. It shows only broken *.desktop with full path and filename that is missing.

Examples

    # fbrokendesktop
    # fbrokendesktop /usr
    # fbrokendesktop /usr/share/apps/kdm/sessions/icewm.desktop

## 提示与技巧

### 隐藏桌面配置项

首先，将桌面配置项复制到 `~/.local/share/applications`，避免修改被后续升级覆盖。

要在所有环境隐藏，在桌面配置项中加入 `NoDisplay=true`. 要在某个环境中隐藏，在桌面配置项中加入 `NotShowIn=desktop-name`

desktop-name 可以是 GNOME, Xfce, KDE 等，指定多个桌面时用分号隔开。

### 自动启动

兼容 XDG 的桌面环境例如s GNOME 或 KDE 会自动, 启动如下位置的 *.desktop 文件:

- 系统目录: `$XDG_CONFIG_DIRS/autostart/` (默认是`/etc/xdg/autostart/`)

  - GNOME 会启动 `/usr/share/gnome/autostart/` 中的程序

- 用户目录 `$XDG_CONFIG_HOME/autostart/` (默认是`~/.config/autostart/`)

将 `*.desktop` 文件复制到用户的 `~/.config/autostart/` 目录可以覆盖系统设置。

参考桌面程序自启动规范.

Note: 此方法仅适用于兼容 XDG 标准的桌面，如果桌面环境不支持此标准，可以使用 dapperAUR, dex 或 fbautostartAUR 实现 XDG 自启动。先用桌面已有的方式自启动它们，这些工具启动后会安装 xdg 方式启动其它程序。

### 修改环境变量

在 `Exec` 命令中可以使用 `env` 修改环境变量:

    ~/.local/share/applications/abiword.desktop

    Exec=env LANG=he_IL.UTF-8 abiword %U

## 参阅

- DeveloperWiki:Removal of desktop files
- desktop wikipedia article
- information for developers

Categories:

- 简体中文
- Package development (简体中文)