Linux桌面快捷方式 Desktop Entry 详解

Desktop Entry 文件以".desktop"为后缀名。以 Linux GNOME 桌面系统为例,用户打开应用程序浏览器后(见图1)会看见很多应用程序快捷方式。事实上,每个应用程序快捷方式都和一个Desktop Entry 文件相对应。这些 Desktop Entry 文件通常被存放在 /usr/share/applications/ /opt/gnome/share/applications/ 等目录下。从文件浏览器进入这些目录,点击相应的Desktop Entry 文件同样可以启动相对应的应用程序。

假设当前"/usr/share/applications/"目录下有一文件"cbt.desktop",用任意文件编辑软件(如 vi 或 gedit)打开"cbt.desktop",将得到如下内容:


清单1 "cbt.desktop"文件内容
[Desktop Entry] Version = 1.0 Encoding = UTF-8 Name = Quick Start Tour GenericName = User Tutorial Comment = Computer Based Training tutorial to \ guide and help you learn how to use the Desktop Exec = gnome-open /usr/share/doc/manual/sled-gnome-cbt_en/index.html Icon = cbt StartupNotify = true Terminal = false Type = Application Categories = GNOME;Application;Documentation; OnlyShowIn = GNOME; X-SuSE-translate = true Name[cs] = Rychlá prohlídka systému Comment[cs] = V?ukov? program seznamující u?ivatele se základy pracovního prost?edí GenericName[cs] = U?ivatelsk? tutoriál Name[hu] = Rendszerbemutató Comment[hu] = A munkaállomés használatát bemutató segédlet GenericName[hu] = Felhasználói segédlet

本文将在下一节中结合上述"cbt.desktop"文件内容重点解析 Desktop Entry 的文件结构。读者可以从中深入领会上述各条语句的具体含义。

3.Desktop Entry 文件结构

Desktop Entry 文件通常以字符串"[Desktop Entry]"开始。由清单 1 可以得知,Desktop Entry 文件的内容是由若干{关键字,数值}配对的 Entry 组成的。例如,"Version"就是一个关键字,关键字"Version"对应的数值是"1.0"。Desktop Entry 文件标准定义了一系列标准关键字。标准关键字分为必选和可选两种:必选标准关键字必须在 .desktop 文件中被定义;而可选关键字则不必。以下是对重点关键字的解析。

  • 关键字"Version":[可选] 该数值指定了当前 Desktop Entry 文件所遵循的 Desktop Entry 文件标准版本。
  • 关键字"Encoding":[1.0 版本不推荐使用] 该数值指定了当前 Desktop Entry 文件中特定字符串所使用的编码方式。尽管Desktop Entry 文件标准 1.0 不再推荐使用该关键字,但由于历史原因该关键字仍然广泛出现在现有的 Desktop Entry 文件中。
  • 关键字"Name":[必选]
    该数值指定了相关应用程序的名称。比如在清单1中关键字"Name"的数值是"Quick Start Tour"。打开文件浏览器,进入"/usr/share/applications"目录,就可以看见"cbt.desktop"文件所定义的快捷方式 的显示样式,如图2所示。其中,快捷方式的显示名称由关键字"Name"的数值所决定,快捷方式所使用的图标由下文中将要介绍的关键字"Icon"的数值 来决定。当然,这些定义在应用程序浏览器中同样适用,请参考图3。 

    图2 "cbt.desktop"文件在文件浏览器中的显示样式
     

  • 关键字"GenericName":[可选]
    该数值指定了相关应用程序的通用名称。比如在清单1中关键字"GenericName"的数值是"User Tutorial"。打开应用程序浏览器,就可以看见字符串"User Tutorial"被显示在图标的右侧,如图3所示:


    图3 "cbt.desktop"文件在应用程序浏览器中的显示样式
     

  • 关键字"Comment":[可选]
    该数值是对当前Desktop Entry的简单描述。
  • 关键字"Type":[必选]
    关键字"Type"定义了Desktop Entry文件的类型。常见的"Type"数值是"Application"和"Link"。"Type = Application"表示当前Desktop Entry文件指向了一个应用程序;而"Type = Link"表示当前Desktop Entry文件指向了一个URL (Uniform Resource Locator)。
  • 关键字"Exec":[可选]
    关键字"Exec"只有在"Type"类型是"Application"时才有意义。"Exec"的数值定义了启动指定应用程序所要执行的命令,在此命令 是可以带参数的。在本例中,关键字"Exec"的数值是字符串"gnome-open /usr/share/doc/manual/sled-gnome-cbt_en/index.html"。在shell中输入该字符串并按回车键同样 可以启动指定应用程序。
  • 关键字"URL":[可选]
    关键字"URL"只有在"Type"类型是"Link"时才有意义。"URL"的数值定义了该Desktop Entry文件指向的URL。例如:

    清单2 "Type = Link"类型Desktop Entry文件示例 

    Type = Link URL = http://www.ibm.com/developerworks


    双击含有上述内容的Desktop Entry文件将启动web浏览器,并打开指定网页"http://www.ibm.com/developerworks",运行结果请参考图4。
  • 关键字"Icon":[可选]
    该数值定义了当前Desktop Entry文件在应用程序浏览器或是在文件浏览器中所显示的图标。如果关键字"Icon"的数值是以绝对路径的格式给出,那么其数值所指定图标文件将被使 用;反之,Linux系统将使用"Icon Theme Specification"[2]在系统指定图标目录下定位所需要使用的图标文件。比如在本例中关键字"Icon"的数值是"cbt",它实际对应着系 统指定图标目录下的图片文件"cbt.png" 。该图片作为图标的显示效果如图2,图3所示。
  • 关键字"StartupNotify":[可选]
    关键字"StartupNotify"的数值是布尔值(true 或是 false)。该关键字只有在"Type"类型是"Application"时才有意义。其数值的含义由规范"Startup Notification Protocol Specifications"[3]定义,在此不再详述。
  • 关键字"Terminal":[可选]
    和"StartupNotify"一样,关键字"StartupNotify"的数值也是布尔值,并且该关键字只有在"Type"类型 是"Application"时才有意义。其数值指出了相关应用程序(即关键字"Exec"的数值)是否需要在终端窗口中运行。本文将在下一节中给出关键 字"Terminal"的具体使用方法。
  • 关键字"Categories":[可选]
    关键字"Categories"只有在"Type"类型是"Application"时才有意义。"Categories"的数值指出了相关应用程序在菜单中显示的类别。具体菜单分类由规范"Desktop Specification Menu"具体定义[4]。
  • 关键字"OnlyShowIn"和"NotShowIn":[可选]
    这两个关键字分别定义了当前Desktop Entry是否在特定Linux 桌面系统(例如:Linux GNOME 或 Linux KDE)下显示(由"OnlyShowIn"定义),或不显示(由"NotShowIn"定义)。具体定义请参考"Desktop Specification Menu"[4]。
  • 关键字"X-SuSE-translate":[SUSE Linux特有]
    关键字"X-SuSE-translate"是SUSE Linux(http://www.novell.com/linux/) 特有的。"X-SuSE-translate"符合SUSE RPM Package风格。"X-SuSE-translate"数值表示是否要对关键字"Name"和"GenericName"进行翻译。详情请参 考"SUSE Package Conventions"[5]。
  • 本地化关键字"[LOCALE]"
    根据"Desktop Entry Specification"规范[1],在关键字后加上字符串"[LOCALE]"就可以对该关键字进行特定的本地化定义。"LOCALE"的合法取值为:

    LOCALE= lang_COUNTRY.ENCODING@MODIFIER


    在此,域"_COUNTRY",".ENCODING"和"@MODIFIER"是可以被忽略的。当指定Desktop Entry文件被解析时,解析器应当根据当前POSIX locale来正确获取本地化的关键字数值。例如清单1就分别定义了在"cs"和"hu"语言环境下关键字"Name","Comment" 和"GenericName"的不同数值。
  • 其余关键字
    除了上述在清单1中出现的关键字外,"Desktop Entry Specification"还定义了"Hidden","TryExec","MimeType"等可选关键字。用户可以根据需要进行选取。
---- 以上内容来自:http://www.ibm.com/developerworks/cn/linux/l-cn-dtef/index.html?ca=drs-cn ------

----------以下内容来自:http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html  ---------


Table 2. Standard Keys
Key Description Value Type REQ? Type
Type This specification defines 3 types of desktop entries: Application (type 1), Link (type 2) and Directory (type 3). To allow the addition of new types in the future, implementations should ignore desktop entries with an unknown type. string YES  
Version Version of the Desktop Entry Specification that the desktop entry conforms with. Entries that confirm with this version of the specification should use 1.0. Note that the version field is not required to be present. string NO 1-3
Name Specific name of the application, for example "Mozilla". localestring YES 1-3
GenericName Generic name of the application, for example "Web Browser". localestring NO 1-3
NoDisplay NoDisplay means "this application exists, but don't display it in the menus". This can be useful to e.g. associate this application with MIME types, so that it gets launched from a file manager (or other apps), without having a menu entry for it (there are tons of good reasons for this, including e.g. the netscape -remote, or kfmclient openURL kind of stuff). boolean NO 1-3
Comment Tooltip for the entry, for example "View sites on the Internet". The value should not be redundant with the values of Name and GenericName. localestring NO 1-3
Icon Icon to display in file manager, menus, etc. If the name is an absolute path, the given file will be used. If the name is not an absolute path, the algorithm described in the Icon Theme Specification will be used to locate the icon. localestring NO 1-3
Hidden Hidden should have been called Deleted. It means the user deleted (at his level) something that was present (at an upper level, e.g. in the system dirs). It's strictly equivalent to the .desktop file not existing at all, as far as that user is concerned. This can also be used to "uninstall" existing files (e.g. due to a renaming) - by letting make install install a file with Hidden=true in it. boolean NO 1-3
OnlyShowIn, NotShowIn A list of strings identifying the environments that should display/not display a given desktop entry. Only one of these keys, either OnlyShowIn or NotShowIn, may appear in a group (for possible values see the Desktop Menu Specification). string(s) NO 1-3
TryExec Path to an executable file on disk used to determine if the program is actually installed. If the path is not an absolute path, the file is looked up in the $PATH environment variable. If the file is not present or if it is not executable, the entry may be ignored (not be used in menus, for example). string NO 1
Exec Program to execute, possibly with arguments. string YES 1
Path If entry is of type Application, the working directory to run the program in. string NO 1
Terminal Whether the program runs in a terminal window. boolean NO 1
MimeType The MIME type(s) supported by this application. string(s) NO 1
Categories Categories in which the entry should be shown in a menu (for possible values see the Desktop Menu Specification). string(s) NO 1
StartupNotify If true, it is KNOWN that the application will send a "remove" message when started with the DESKTOP_STARTUP_ID environment variable set. If false, it is KNOWN that the application does not work with startup notification at all (does not shown any window, breaks even when using StartupWMClass, etc.). If absent, a reasonable handling is up to implementations (assuming false, using StartupWMClass, etc.). (See the Startup Notification Protocol Specification for more details). boolean NO 1
StartupWMClass If specified, it is known that the application will map at least one window with the given string as its WM class or WM name hint (see the Startup Notification Protocol Specification for more details). string NO 1
URL If entry is Link type, the URL to access. string YES 2

The Exec key

The Exec key must contain a command line. A command line consists of an executable program optionally followed by one or more arguments. The executable program can either be specified with its full path or with the name of the executable only. If no full path is provided the executable is looked up in the $PATH environment variable used by the desktop environment. The name or path of the executable program may not contain the equal sign ("="). Arguments are separated by a space.

Arguments may be quoted in whole. If an argument contains a reserved character the argument must be quoted. The rules for quoting of arguments is also applicable to the executable name or path of the executable program as provided.

Quoting must be done by enclosing the argument between double quotes and escaping the double quote character, backtick character ("`"), dollar sign ("$") and backslash character ("\") by preceding it with an additional backslash character. Implementations must undo quoting before expanding field codes and before passing the argument to the executable program. Reserved characters are space (" "), tab, newline, double quote, single quote ("'"), backslash character ("\"), greater-than sign (">"), less-than sign ("<"), tilde ("~"), vertical bar ("|"), ampersand ("&"), semicolon (";"), dollar sign ("$"), asterisk ("*"), question mark ("?"), hash mark ("#"), parenthesis ("(") and (")") and backtick character ("`").

Note that the general escape rule for values of type string states that the backslash character can be escaped as ("\\") as well and that this escape rule is applied before the quoting rule. As such, to unambiguously represent a literal backslash character in a quoted argument in a desktop entry file requires the use of four successive backslash characters ("\\\\"). Likewise, a literal dollar sign in a quoted argument in a desktop entry file is unambiguously represented with ("\\$").

A number of special field codes have been defined which will be expanded by the file manager or program launcher when encountered in the command line. Field codes consist of the percentage character ("%") followed by an alpha character. Literal percentage characters must be escaped as %%. Deprecated field codes should be removed from the command line and ignored. Field codes are expanded only once, the string that is used to replace the field code should not be checked for field codes itself.

Command lines that contain a field code that is not listed in this specification are invalid and must not be processed, in particular implementations may not introduce support for field codes not listed in this specification. Extensions, if any, should be introduced by means of a new key.

Implementations must take care not to expand field codes into multiple arguments unless explicitly instructed by this specification. This means that name fields, filenames and other replacements that can contain spaces must be passed as a single argument to the executable program after expansion.

Although the Exec key is defined to have a value of the type string, which is limited to ASCII characters, field code expansion may introduce non-ASCII characters in arguments. Implementations must take care that all characters in arguments passed to the executable program are properly encoded according to the applicable locale setting.

Recognized field codes are as follows:

Code Description
%f A single file name, even if multiple files are selected. The system reading the desktop entry should recognize that the program in question cannot handle multiple file arguments, and it should should probably spawn and execute multiple copies of a program for each selected file if the program is not able to handle additional file arguments. If files are not on the local file system (i.e. are on HTTP or FTP locations), the files will be copied to the local file system and %f will be expanded to point at the temporary file. Used for programs that do not understand the URL syntax.
%F A list of files. Use for apps that can open several local files at once. Each file is passed as a separate argument to the executable program.
%u A single URL. Local files may either be passed as file: URLs or as file path.
%U A list of URLs. Each URL is passed as a separate argument to the executable program. Local files may either be passed as file: URLs or as file path.
%d Deprecated.
%D Deprecated.
%n Deprecated.
%N Deprecated.
%i The Icon key of the desktop entry expanded as two arguments, first --icon and then the value of the Icon key. Should not expand to any arguments if the Icon key is empty or missing.
%c The translated name of the application as listed in the appropriate Name key in the desktop entry.
%k The location of the desktop file as either a URI (if for example gotten from the vfolder system) or a local filename or empty if no location is known.
%v Deprecated.
%m Deprecated.

A command line may contain at most one %f, %u, %F or %U field code. If the application should not open any file the %f, %u, %F and %U field codes must be removed from the command line and ignored.

Field codes must not be used inside a quoted argument, the result of field code expansion inside a quoted argument is undefined. The %F and %U field codes may only be used as an argument on their own. 

你可能感兴趣的:(linux)