Hashbang for Gnome .desktop 文件

发布于 2024-11-07 05:37:35 字数 512 浏览 3 评论 0原文

我希望能够在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心是晴朗的。 2024-11-14 05:37:35

明确地说,Ignacio 在此处是正确的,.desktop 文件不应直接执行。这是可能的(正如您所发现的),但不明智。

另一方面,不要使用 xdg-open。如果有正确关联的 mime 类型,它可能会碰巧起作用,但这并不可靠。

您应该使用gtk-launch。它的用法如下:

gtk-launch APPLICATION [URI...]
gtk-launch app-name.desktop
gtk-launch app-name

这是 man 条目:

姓名

 gtk-launch - 启动应用程序

概要

 gtk-launch [应用程序] [URI...]

描述

 gtk-launch 使用给定名称启动应用程序。这
   应用程序在默认情况下以正确的启动通知启动
   显示,除非另有说明。

   gtk-launch 至少需要一个参数,即应用程序的名称
   发射。该名称应与应用程序桌面文件名匹配,如下所示
   驻留在 /usr/share/application 中,带或不带“.desktop”
   后缀。

   如果调用多个参数,则除了
   应用程序名称被视为 URI 位置并传递为
   启动的应用程序的参数。

请注意,gtk-launch 需要安装.desktop 文件(即位于/usr/share/applications 或< em>$HOME/.local/share/applications)。

因此,为了解决这个问题,我们可以使用一个 hackish 的小 bash 函数,在启动之前临时安装所需的 .desktop 文件。安装 .desktop 文件的“正确”方法是通过 desktop-file-install 但我将忽略它。

launch(){
    (
    # where you want to install the launcher to
    appdir=$HOME/.local/share/applications

    # the template used to install the launcher
    template=launcher-XXXXXX.desktop

    # ensure $1 has a .desktop extension, exists, is a normal file, is readable, has nonzero size
    # optionally use desktop-file-validate for stricter checking
    # if ! desktop-file-validate "$1" 2>/dev/null; then
    if [[ ! ( $1 = *.desktop && -f $1 && -r $1 && -s $1 ) ]]; then
        echo "ERROR: you have not supplied valid .desktop file" >&2
        exit 1
    fi

    # ensure the temporary launcher is deleted upon exit
    trap 'rm "$launcherfile" 2>/dev/null' EXIT

    launcherfile=$(mktemp -p "$appdir" "$template")
    launchername=${launcherfile##*/}

    if cp "$1" "$launcherfile" 2>/dev/null; then
        gtk-launch "$launchername" "${@:2}"
    else
        echo "ERROR: failed to copy launcher to applications directory" >&2
        exit 1
    fi

    exit 0
    )
}

您可以像这样使用它(如果需要,还可以传递其他参数或 URI):

launch ./path/to/shortcut.desktop

或者,我在此处编写了一个答案 概述了启动 .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:

gtk-launch APPLICATION [URI...]
gtk-launch app-name.desktop
gtk-launch app-name

Here is the man entry:

NAME

   gtk-launch - Launch an application

SYNOPSIS

   gtk-launch [APPLICATION] [URI...]

DESCRIPTION

   gtk-launch launches an application using the given name. The
   application is started with proper startup notification on a default
   display, unless specified otherwise.

   gtk-launch takes at least one argument, the name of the application to
   launch. The name should match application desktop file name, as
   residing in /usr/share/application, with or without the '.desktop'
   suffix.

   If called with more than one argument, the rest of them besides the
   application name are considered URI locations and are passed as
   arguments to the launched application.

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 via desktop-file-install but I'm going to ignore that.

launch(){
    (
    # where you want to install the launcher to
    appdir=$HOME/.local/share/applications

    # the template used to install the launcher
    template=launcher-XXXXXX.desktop

    # ensure $1 has a .desktop extension, exists, is a normal file, is readable, has nonzero size
    # optionally use desktop-file-validate for stricter checking
    # if ! desktop-file-validate "$1" 2>/dev/null; then
    if [[ ! ( $1 = *.desktop && -f $1 && -r $1 && -s $1 ) ]]; then
        echo "ERROR: you have not supplied valid .desktop file" >&2
        exit 1
    fi

    # ensure the temporary launcher is deleted upon exit
    trap 'rm "$launcherfile" 2>/dev/null' EXIT

    launcherfile=$(mktemp -p "$appdir" "$template")
    launchername=${launcherfile##*/}

    if cp "$1" "$launcherfile" 2>/dev/null; then
        gtk-launch "$launchername" "${@:2}"
    else
        echo "ERROR: failed to copy launcher to applications directory" >&2
        exit 1
    fi

    exit 0
    )
}

You can use it like so (and also pass along additional arguments or URIs if you want):

launch ./path/to/shortcut.desktop

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.

因为看清所以看轻 2024-11-14 05:37:35

您始终可以使用 xdg-open 作为您的 shebang,如下所示:

#!/usr/bin/env xdg-open

这不会造成任何麻烦,因为 # 也在 .desktop 中启动注释文件。

You can always use xdg-open for your shebang, as in:

#!/usr/bin/env xdg-open

This won't cause any trouble because # starts comments also in .desktop files.

空城之時有危險 2024-11-14 05:37:35

没有一个; .desktop 文件不打算被执行。而是运行 Exec 键中给出的可执行文件。

There isn't one; .desktop files aren't intended to be executed. Run the executable given in the Exec key instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文