11

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

7

As this Ask Ubuntu SE answer and GitLab issue comment state:

#!/usr/bin/env sh
gio launch '/path/file.desktop'
1
  • 1
    Interestingly, this doesn't appear to always work: gio: Unable to load application information for ‘/home/RokeJulianLockhart/.local/share/applications/steam.desktop‘. Commented Dec 5, 2024 at 22:58
3

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
4
  • 10
    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
  • 4
    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. Commented Jun 15, 2023 at 9:01
  • @EduardoTrápani, I suggest that you state your environment, since my answer explains how your experience should be impossible. Commented Aug 28, 2024 at 16:47
2

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

gtk-launch example.desktop

gtk-launch will look for example.desktop in /usr/share/applications/, and ~/.local/share/applications/ and anywhere else where .desktop files are meant to be placed.

1
  • 1
    This isn't working for me, per "gtk-launch: no such application /home/RokeJulianLockhart/.local/share/applications/steam.desktop", despite file "$HOME/.local/share/applications/steam.desktop" returning "/home/RokeJulianLockhart/.local/share/applications/steam.desktop: Unicode text, UTF-8 text". Commented Dec 5, 2024 at 22:59
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.