@readwithai - X - blog - computer-assisted reading for more agency
This is a quick primer on desktop files for those with a command-line mentality - who are used to running commands from the command-line, adding things to their path, and creating shell scripts. Desktop files are how a number of GUI desktop environments like KDE plasma, Gnome Shell and Ubuntu Unity specify things that are run.
If you want a technical reference you might like to start with the arch wiki page on the topic . If you want a practical quickstart carry on reading.
What is is a desktop file and why should I care?
You don’t need to use desktop files - you could run everything you want from the terminal, but most people at least run their terminals from a GUI desktop environment, probably use a separate browser and a couple of applications, and a lot of desktop environments work with the common standard of .desktop files. In KDE desktop files are used for keyboard shortcuts, items in the launcher and applications that automatically start when you start the desktop environment. If you are going to use a desktop environment you might want to be able to make use of features that use desktop files. But if you are a command-line user your probably want to be able to edit and create these files yourself.
You can to some degree escape desktop files by using certain tiling or simpler window managers like i3 or awesome.
I am @readwithai. I make tools for reading and research using the note taking tool Obsidian. Interesting? Links at the end of the article
What are desktop files used for and why do they exist?
A desktop file specifies something that can be run. They are similar to an executable that you place on your path - but they provide additional information that desktop environments demand. They make things a bit more complicated than exuectable on the command-line in exchange for certain conveniences like icons, documentation, and localization - amongst other things.
You can think of desktop files as like a shell script for GUI desktops A few systems that use .desktop files are: the keyboard shortcut systems, the mime type/ file type system and program launcherps.
Where do desktop files live and what do they look like?
Desktop files are an INI/TOML-like file that provide a number of key value pairs. Like shell scripts and executables, there is a PATH for desktop entries that consists of ~/.local/share/applications and /usr/share/local/applications and /usr/share/applications.
You can explore some open applications files there to see how they work. Desktop files provide various bits of data including localisation of executables to difference file systems, a tooltip describing an executable called Comment, a name calld Name, an executable and arguments to run called Exec amongst other things.
Here is a minimal example of a desktop file:
# ~/.local/share/applications/simple.desktop
[Desktop Entry]
Name=A simple desktop entry
Exec=zenity --info ran desktop entry
If you create this desktop file then you will be able to run it from your desktop environments’s launcher menu. You actually don’t even need the Name entry in the name entry - if omitted the name default to simple.
Some nice things about desktop entries
Desktop entries are a bit of a pain compared to an executable on the path. But they provide some benefits - they work across different systems. The mime system is quite nice, because you can ask what things can open a file in a more data-driven way.
Mime-types and default applications
If you give the Exec parameter of the desktop file an argument using %f as well as one or more MimeTypes, then your desktop will know that your application can handle certain mime types. Your desktop environment can then do useful things like add the application to the “Open with…” menu.
Here is a simple example that when runs on PDFs or PNGs copies the file to a known location.
# /home/alex/.local/share/applications/copy-to-file.desktop
[Desktop Entry]
Exec=cp %f /tmp/file.pdf
MimeType=application/pdf;application/png
Once you create this file, you will notice a copy-to-file item in Open With menu on PDF files (though it should be noted that mime types are handled through other mechanisms as well).
You can query the default desktop file for a mime type with for example
xdg-mime query default application/pdf
In KDE, you can use can use ktraderclient5 to find all the apps that support a given mime type: ktraderclient5 --mimetype application/pdf --servicetype Application.
You could then set (careful!) the default mime handler to be this file with:
xdg-mime default copy-to-file.desktop application/pdf
Keyboard shortcuts
KDE seems to create a new desktop file every time you create a custom shortcut. So if you want to create shortcuts by hand understanding desktop files is helpful.
Autostart
KDE also uses desktop files for the applications that automatically start when KDE starts. These live in $HOME/autostart unfortunately gtk-launch, which can be used to run desktop files does not find these files. If you want to test out an autostart file you can use dex which runs a desktop file based on a path rather htan searching.
Running desktop files from the command-line
When creating desktop files it can be nice to run them in a repeatedly way and the command-line can help here. The best way I found to run desktop files is gtk-launch. This still works with kde. There are other tools like dex, but the nice thing about this is that it searches for the desktop file in different locations like launches rather than requiring a full path. gtk-launch vim.desktop launches vim from the command-line.
@readwithai here. If you are interested in reading, note taking or command-line backs follow me on X, my blog, or learn about Obsidian.
Finishing up
I hope this was useful. I created this because the world seemed to lack some practical documentation on desktop files (even though the reference documentation is pretty good).