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: http://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal

From the thread:

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

#!/usr/bin/python

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__":
    main(*sys.argv)

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

[Desktop Entry]
Version=1.0
Name=run-desktop
Exec=run-desktop %U
MimeType=application/x-desktop
Terminal=false
Type=Application

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

[Default Applications]
....
application/x-desktop=run-desktop.desktop

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.