Shell – How to run a .desktop file

shellxdg-open

I'm trying to write a script that launches the default application for a given mime type. For example, I would like to say my-script text/plain and have it open KWrite for me.

What I know is that you can use xdg-mime to query what is the default application for a given mime type

$ xdg-mime query default text/plain
org.kde.kwrite.desktop

However, I don't know what is the command I can use to launch KWrite given org.kde.kwrite.desktop. How can I do that? Is having the name of the desktop file enough or do I also need to find out where it is stored (/usr/share/applications, .local/share/applications, etc)?


By the way, I don't think I can solve my original problem using xdg-open because XDG open expects to receive a filename or URL as a parameter and I want to be able to launch my applications without needing to pass a filename. For example, I want to be able to open the text editor on a blank file buffer or open my web browser on its home page.

Best Answer

This answer over on askubuntu.com covers many different ways to solve the problem. The one that was the closes to doing what I wanted was the gtk-launch command:

gtk-launch org.kde.kwrite.desktop

One thing I like about gtk-launch is that it can find the appropriate desktop file even if you only give it the name.

Related Question