Hashbang for Gnome .desktop 文件
我希望能够在 .desktop
文件顶部添加 #!
注释,这样如果它具有执行权限并被执行,它实际上会跑步。但是,我不知道 .desktop
文件的解释器是什么,所以我不知道要在 hashbang 中写入哪个 /usr/bin/
文件。有什么想法吗?
编辑:
到目前为止,我已经制作了一个小型 bash 脚本 execdesktop
,它可以执行桌面文件:
`sed -nr 's/Exec=(.*)$/\\1/p' $1`
如果我将以下内容添加到我的 .desktop< /code> files:
#!/usr/bin/execdesktop
然后运行正常。此方法有效,但我不想使用它,因为它需要安装 execdesktop。
I'd like to be able to add a #!
comment at the top of my .desktop
file so that if it has execute permissions and is executed, it'll actually run. However, I don't know what the interpreter for .desktop
files is, so I don't know which /usr/bin/
file to write in the hashbang. Any ideas?
Edit:
So far, I've made a small bash script, execdesktop
, that can execute desktop files:
`sed -nr 's/Exec=(.*)$/\\1/p' $1`
If I then add the following to my .desktop
files:
#!/usr/bin/execdesktop
Then it runs fine. This method works, but I'd prefer not to have to use it since it requires the installation of execdesktop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
明确地说,Ignacio 在此处是正确的,.desktop 文件不应直接执行。这是可能的(正如您所发现的),但不明智。
另一方面,不要使用 xdg-open。如果有正确关联的 mime 类型,它可能会碰巧起作用,但这并不可靠。
您应该使用
gtk-launch
。它的用法如下:这是 man 条目:
请注意,
gtk-launch
需要安装.desktop 文件(即位于/usr/share/applications 或< em>$HOME/.local/share/applications)。因此,为了解决这个问题,我们可以使用一个 hackish 的小 bash 函数,在启动之前临时安装所需的 .desktop 文件。安装 .desktop 文件的“正确”方法是通过
desktop-file-install
但我将忽略它。您可以像这样使用它(如果需要,还可以传递其他参数或 URI):
或者,我在此处编写了一个答案 概述了启动 .desktop 文件的所有方法。它提供了一些 gtk-launch 的替代方案,可能会有所帮助。
Just to be explicit, Ignacio is correct here in that .desktop files should not be directly executed. It is possible (as you discovered), but unwise.
On another note, do not use
xdg-open
. It might just happen to work if there is a correctly associated mime-type, but this is not reliable.You should use
gtk-launch
. It is used as follows:Here is the man entry:
Please note that
gtk-launch
requires the .desktop file to be installed (i.e. located in /usr/share/applications or $HOME/.local/share/applications).So to get around this, we can use a hackish little bash function that temporarily installs the desired
.desktop
file before launching it. The "correct" way to install a .desktop file is viadesktop-file-install
but I'm going to ignore that.You can use it like so (and also pass along additional arguments or URIs if you want):
Alternatively, I wrote an answer here that outlines all the ways to launch .desktop files. It offers some alternatives to
gtk-launch
that may help.您始终可以使用
xdg-open
作为您的 shebang,如下所示:这不会造成任何麻烦,因为
#
也在.desktop
中启动注释文件。You can always use
xdg-open
for your shebang, as in:This won't cause any trouble because
#
starts comments also in.desktop
files.没有一个; .desktop 文件不打算被执行。而是运行
Exec
键中给出的可执行文件。There isn't one; .desktop files aren't intended to be executed. Run the executable given in the
Exec
key instead.