1.Desktop ?Entry文件
在windows操作系統里,打開程序可以通過點桌面上的快捷方式文件打開。在Linux系統上,程序的打開快捷方式文件是由Desktop Entry文件來描述的,它描述了程序的啟動配置信息。Desktop Entry文件標準是由FreeDesktop.org(http://freedesktop.org/wiki/)制定的,目前最新的版本是Version 1.2alpha。我們用1.0就好了。Desktop Entry文件以".desktop"為后綴名。這些Desktop ?Entry文件通常會放在/usr/share/applications/或/opt/gnome/share/applications/等目錄下,但其實放哪里都可以。用戶打開應用程序瀏覽器后會看見很多應用程序快捷方式,每個快捷方式都和一個Desktop ?Entry文件相對應。點擊相應Desktop ?Entry文件同樣可以啟動相對應的應用程序。一個DesktopEntry文件的樣子大概如下:
[Desktop Entry]
Version = 1.0
Encoding = UTF-8
Name = Quick Start Tour
GenericName = User Tutorial
Comment = 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
2.Desktop ?Entry文件結構
Desktop Entry文件以字符串"[Desktop Entry]"開始。Desktop Entry文件的內容是由若干“key=value”的鍵值對組成的。key分為必選和可選兩種:必選的必須在.desktop文件中被定義;而可選則不必。下面是關于一些重要關鍵字的說明:
Version:可選,指定了當前Desktop Entry文件所遵循的Desktop Entry文件標準版本。
Name:必選,指定了快捷方式顯示的名稱。
Icon:[可選]指定義了快捷方式所使用的圖標。
Icon如果指定的圖標是以絕對路徑的格式給出,那么所指定圖標文件將被使用;如果只給出了圖標的名稱,那么Linux系統將使用"IconTheme Specification",也就說從系統指定的主題下去找這個圖標,這些主題的圖標放在/usr/share/icons的對應的主題下,如果系統沒有指定主題,那么就會加載hicolor主題下的圖標。原文是這樣說的:
The ?name of the theme that this theme inherits from. If an icon name is not found in the current theme, it is searched for in the inherited theme (and recursively in all the inherited themes).If no theme is specified implementations are required to add the"hicolor" theme to the inheritance tree. An implementation may optionally add other default themes in between the last specified theme and the hicolor theme.
參考:https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-0.9.html
GenericName:[可選]指定了應用程序的通用名稱。如果程序名系:谷歌瀏覽器,那么這個通用的名稱可以系:瀏覽器。有些分類的意思。
Comment:[可選]對當前快捷方式對應的程序的簡單描述。
Type:[必選]指定了Desktop Entry文件的類型。數值可以是"Application"或"Link"。"Type= Application"表示當前Desktop Entry文件指向了一個應用程序;而"Type = Link"表示當前Desktop Entry文件指向了一個URL。
Exec:[可選]只有在"Type"類型是"Application"時才有意義。定義了啟動指定應用程序所要執行的命令,在此命令是可以帶參數的。所執行的命令在shell中輸入并按回車鍵同樣可以啟動指定應用程序。
URL:[可選]只有在"Type"類型是"Link"時才有意義。"URL"的數值定義了該Desktop
Entry文件指向的URL。
StartupNotify:[可選]值只能是true或false,只有在"Type"類型是"Application"時才有意義。用于跟蹤程序的啟動。參考:https://www.freedesktop.org/wiki/Specifications/startup-notification-spec/
Terminal:[可選]數值也是布爾值,只有在"Type"類型是"Application"時才有意義。指出將要運行的程序是否需要在終端窗口中運行
Categories:[可選]只有在"Type"類型是"Application"時才有意義。指出了將要運行的程序在菜單中顯示的類別。參考:https://specifications.freedesktop.org/menu-spec/menu-spec-1.0.html
在關鍵字后加上字符串"[LOCALE]"就可以對該關鍵字進行特定的本地化定義,也就是說根據系統所選的語言去選用相應的鍵值。"LOCALE"的合法取值為:
LOCALE= lang_COUNTRY.ENCODING@MODIFIER
在此,域"_COUNTRY",".ENCODING"和"@MODIFIER"是可以被忽略的。當指定Desktop
Entry文件被解析時,解析器應當根據當前POSIX
locale來正確獲取本地化的關鍵字數值。如前就分別定義了在"cs"和"hu"語言環境下關鍵字"Name","Comment"和"GenericName"的不同值。
下載創建幾個小實例:
如在桌面創建打開百度的快捷方式,我準備了一個baidu.png圖標:
[Desktop
Entry]
Version=1.0
Name=百度一下
Icon=/home/won/Icons/baidu.png
Type=Link
把這個文件件放在桌面,一點擊,就打開百度了。
再來一個打開androidstudio IDE的:
[Desktop
Entry]
Version=1.0
Name=Android
Studio
Icon=/home/won/Icons/studio.png
Type=Application
Exec=/home/won/Software/android-studio/bin/studio.sh
效果如下: