linux 桌面快捷方式[Desktop Entry]

本文详细介绍了DesktopEntry文件的格式和用途,包括其不同类型的定义、关键字段的解释及使用场景,如Version、Name、Exec等。

以eclipse为例,最简单的Desktop Entry:

[Desktop Entry]
Exec=/opt/eclipse/eclipse
Type=Application

一般内容:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Eclipse
Comment=Eclipse IDE
Exec=eclipse
Icon=/opt/eclipse/icon.xpm
Terminal=false
Type=Application
Categories=GNOME;Application;Development;
StartupNotify=true 


主要的参数及其作用如下表【来源:http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html


KeyDescriptionValue TypeREQ?Type
TypeThis 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.stringYES 
VersionVersion of the Desktop Entry Specification that the desktop entry conforms with. Entries that confirm with this version of the specification should use1.0. Note that the version field is not required to be present.stringNO1-3
NameSpecific name of the application, for example "Mozilla".localestringYES1-3
GenericNameGeneric name of the application, for example "Web Browser".localestringNO1-3
NoDisplayNoDisplay 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. thenetscape -remote, or kfmclient openURL kind of stuff).booleanNO1-3
CommentTooltip for the entry, for example "View sites on the Internet". The value should not be redundant with the values ofName and GenericName.localestringNO1-3
IconIcon 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 theIcon Theme Specification will be used to locate the icon.localestringNO1-3
HiddenHidden 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 lettingmake install install a file with Hidden=true in it.booleanNO1-3
OnlyShowIn, NotShowInA list of strings identifying the environments that should display/not display a given desktop entry. Only one of these keys, eitherOnlyShowIn or NotShowIn, may appear in a group (for possible values see theDesktop Menu Specification).string(s)NO1-3
TryExecPath 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).stringNO1
ExecProgram to execute, possibly with arguments. See the Exec key for details on how this key works.stringYES1
PathIf entry is of type Application, the working directory to run the program in.stringNO1
TerminalWhether the program runs in a terminal window.booleanNO1
ActionsIdentifiers for application actions. This can be used to tell the application to make a specific action, different from the default behavior. TheApplication actions section describes how actions work.string(s)NO1
MimeTypeThe MIME type(s) supported by this application.string(s)NO1
CategoriesCategories in which the entry should be shown in a menu (for possible values see theDesktop Menu Specification).string(s)NO1
KeywordsA list of strings which may be used in addition to other metadata to describe this entry. This can be useful e.g. to facilitate searching through entries. The values are not meant for display, and should not be redundant with the values ofName or GenericName.localestring(s)NO1
StartupNotifyIf 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 workwith startup notification at all (does not shown any window, breakseven when using StartupWMClass, etc.).If absent, a reasonable handling is up to implementations (assuming false,using StartupWMClass, etc.). (See theStartup Notification Protocol Specification for more details).booleanNO1
StartupWMClassIf specified, it is known that the application will map at least onewindow with the given string as its WM class or WM name hint (see theStartup Notification Protocol Specification for more details).stringNO1
URLIf entry is Link type, the URL to access.stringYES2

更详细的内容:http://blog.chinaunix.net/uid-20332519-id-3015914.html


<think>我们正在讨论Linux桌面快捷方式(.desktop文件)中的Path字段。根据用户的问题,我们需要解释Path字段的作用、使用方法和注意事项。 引用资料中并没有直接提到Path字段,因此我们将基于标准Desktop Entry规范来回答。 作用: - Path字段指定了应用程序启动时的工作目录。当通过快捷方式启动程序时,程序将在该目录下运行。这对于需要访问特定目录下资源(如配置文件、数据文件)的应用程序非常重要。 使用方法: - 在.desktop文件中,使用Path=/路径/的格式来设置。例如:Path=/home/user/myapp_data/ 注意事项: 1. 路径必须使用绝对路径(以/开头)。 2. 如果路径包含空格或特殊字符,不需要特别处理(因为.desktop文件是键值对,值部分直接写路径即可)。 3. 如果未设置Path字段,则默认使用当前工作目录(通常是用户的主目录或启动快捷方式时所在的目录)。 4. 如果路径不存在,程序可能无法正常启动,具体取决于应用程序是否依赖该工作目录。 示例: 假设有一个应用程序需要在其安装目录下读取配置文件,那么可以这样写: ``` [Desktop Entry] Type=Application Name=MyApp Exec=/opt/myapp/myapp_bin Icon=/opt/myapp/icon.png Path=/opt/myapp/ Terminal=false ``` 相关问题: 1. 如果Path字段指定的目录不存在会怎样? 2. 如何为不同的用户设置不同的工作目录(例如使用环境变量)? 3. 在.desktop文件中可以使用相对路径吗? 4. 如何测试Path字段是否按预期工作? 注意:在回答中,我们不需要引用之前的引用内容,因为那些内容与当前问题关联不大。我们主要根据标准规范来回答。 </think> ### Linux 桌面快捷方式中 `Path` 字段详解 #### 一、作用与意义 `Path` 字段在 `.desktop` 文件中定义了应用程序启动时的**工作目录**(Working Directory)[^1]。主要作用包括: 1. **资源定位** 当应用程序需要访问相对路径资源(如配置文件、数据文件)时,系统会以 `Path` 指定的目录为基准进行查找。 例如:若设置 `Path=/opt/myapp/`,则脚本中的 `./config.ini` 会指向 `/opt/myapp/config.ini`。 2. **环境准备** 为依赖特定目录环境的程序(如需加载本地库的 Java/Python 应用)提供正确的运行上下文。 3. **路径隔离** 避免因用户当前目录不同导致的文件访问冲突。 --- #### 二、使用方法 ##### 基础配置 ```ini [Desktop Entry] Type=Application Name=示例程序 Exec=./start.sh # 使用相对路径 Icon=app-icon Path=/opt/myapp/ # 指定工作目录 Terminal=false ``` - 此时执行 `Exec` 命令相当于在终端运行: $$ \text{cd /opt/myapp/ \&\& ./start.sh} $$ ##### 动态路径技巧 使用环境变量实现用户级定制: ```ini Path=$HOME/Documents/app_data/ ``` > 注:部分桌面环境支持 `$HOME` 等变量,需测试验证[^3]。 --- #### 三、注意事项 1. **路径有效性** - 目录必须真实存在,否则程序可能启动失败 - 推荐使用**绝对路径**,相对路径行为可能不一致 2. **权限控制** 若路径指向受限区域(如 `/usr/`),需确保: - 用户有目录读写权限 - 应用程序以正确权限运行(如通过 `pkexec` 提权) 3. **特殊字符处理** 路径含空格或特殊符号时需转义: ```ini Path=/path/with\ spaces/ # 使用反斜杠转义 ``` 4. **与 `Exec` 的协同** - `Exec` 中的绝对路径命令不受 `Path` 影响 - 若 `Exec` 使用相对路径(如 `./app`),则**必须**设置 `Path` --- #### 四、调试建议 1. **验证路径** 终端执行测试: ```bash desktop-file-validate your_app.desktop # 语法检查 test -d $(grep "Path" your_app.desktop | cut -d= -f2) && echo "Valid" # 路径存在性验证 ``` 2. **日志跟踪** 临时启用终端输出观察行为: ```ini Terminal=true # 启动时显示终端 ``` 观察报错如:`Error: Config file not found at ./config.ini` --- ### 示例场景 **Python 数据分析工具配置**: ```ini [Desktop Entry] Type=Application Name=数据处理器 Exec=python3 analysis.py # 依赖工作目录中的 analysis.py Icon=statistics Path=/home/user/data_tools/ # 确保 analysis.py 在此目录 Terminal=true Categories=Science; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值