Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashbang for Gnome .desktop files

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.

like image 358
Reinderien Avatar asked May 16 '11 16:05

Reinderien


People also ask

Where do I put .desktop files?

Desktop entries for applications, or . desktop files, are generally a combination of meta information resources and a shortcut of an application. These files usually reside in /usr/share/applications/ or /usr/local/share/applications/ for applications installed system-wide, or ~/.

How do I run a .desktop file?

desktop files are shortcuts that allow application's settings to be customized. For instance, I have lots of them in my /usr/share/applications/ folder. If I open that folder in nautilus , I can run these applications just by double clicking its associated file, e.g. double-clicking firefox. desktop runs Firefox.

Where do I put .desktop files in Ubuntu?

Alternatively, you can place your . desktop file at /usr/share/applications/ or at ~/. local/share/applications/. After moving your file there, search for it in the Dash (Windows key -> type the name of the application) and drag and drop it to the Unity Launcher.


1 Answers

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.

like image 65
Six Avatar answered Sep 24 '22 11:09

Six