November 5, 2014

Running .desktop files from command line

I have been mildly annoyed from time to time that I did not know of any way to start a .desktop file from command line. After all, I can do so just fine from nautilus so there had to be a way to do it from a terminal too, right?

Turns out xdg-open can’t do it since .desktop files are a subclass of text files and as such the associated program is of course a text editor.

After searching through the net a bit I found a rather neat solution using GIO from python, courtesy of this thread:

From the thread:

A small python script that interfaces with GIO and asks it to do it’s magic:


from gi.repository import Gio
import sys 

def main(myname, desktop, *uris):
    launcher = Gio.DesktopAppInfo.new_from_filename(desktop)
    launcher.launch_uris(uris, None)

if __name__ == "__main__":

Couple that with a .desktop file to run the python script:

[Desktop Entry]
Exec=run-desktop %U

And then set that to be the default application for desktop files within xdg, like so:

[Default Applications]

Result: pure magic, and now xdg-open .desktop interprets and launches the .desktop file just as nautilus would. Splendid!

© Sebastian Hörberg 2018

Powered by Hugo & Kiss.