5

I want to run .desktop files from the terminal and dmenu. From the terminal, it should ideally be run with ./app.desktop or /path/to/app.desktop, and app.desktop would be put in a $PATH directory. I'd rather not have to use an external command like gtk-launch app.desktop.

Additionally, I've found that gtk-launch doesn't always work to launch the application with gtk-launch app.desktop, and even when it does work, putting #!/path/to/gtk-launch at the top of the .desktop returns the error gtk-launch: no such application ./app.desktop when I run ./app.desktop with it executable.

I'm using bspwm and generally prefer launching things from the terminal or with dmenu_run so I don't have a desktop from which I can click on the .desktop files to launch them.

How can I make the .desktop files executable?

4 Answers 4

5

As https://askubuntu.com/a/239883/1002900 and https://gitlab.gnome.org/GNOME/glib/-/issues/54#note_1755632 explain:

gio launch '/path/file.desktop'
2

Provided that they are executable (chmod u+x /path/to/app.desktop) you can add this shebang at the top:

#!/usr/bin/env xdg-open

And from then on you can call your application with:

/path/to/app.desktop

For example, this .desktop file will start xeyes.

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Xeyes
Exec=xeyes
Terminal=false
Type=Application
3
  • 7
    When I use xdg-open app.desktop, it doesn't run the Exec= line but instead runs $EDITOR app.desktop which opens the .desktop file for editing in nvim for me without perl-file-mimeinfo installed (I'm on Manjaro). With perl-file-mimeinfo, it opens the path/to/app,desktop in my browser, which then prompts me to download the file. How should I configure xdg-open to correctly open the application? Commented Dec 30, 2020 at 2:15
  • Can you post the app.desktop file? Because I tried this and it works flawlessly. There might be an issue with the .desktop file itself. I added a minimum example. Commented Dec 30, 2020 at 3:05
  • 1
    I can only confirm what ChocolateOverflow wrote: xdg-open does not "launch" files of content-type: application/x-desktop but tries to edit them. For confirmation I tested with the example from this answer.
    – village
    Commented Jun 15, 2023 at 9:01
1

If you have gtk-launch installed, you can run:

gtk-launch example.desktop
0

You could try parsing the file, and executing whatever the Exec= line says should be executed. This should do it:

$(awk '/^Exec=/{sub(/^Exec=/, ""); print}' file.desktop)

Whatever was in file.desktop will launch.

You could also expand this and make a script that takes an argument APP and runs that command on the file.desktop file(s) that have "Name=APP" in it(them).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .