Do you think that creating .desktop files is difficult on Linux? We propose a couple of solutions

Create .desktop files on Linux

“Go back to Windows”, the person who introduced me to Linux told me when he saw that some things were choking me and he wanted to do it like in the Microsoft system. He also told me that "Linux is not Windows", and that is the case for better or for worse. The downside is that there are things that are very simple on other systems and not so simple on those based on Linux, such as create .desktop files, also known as shortcuts.

But you have to differentiate between types of shortcuts, because not all are the same. Right now, I can think of at least two types of shortcuts: some are symlinks or symbolic links, which are a very small file that we can execute to access the original that is linked to; others are .desktop files, which are a type of direct link which contains information such as the name of an application and its version, and are the files that are responsible for launching many applications under Linux.

Creating .desktop files with a UI app

Just like we explain Here at LXA years ago, creating .desktop files on Linux isn't that overly complicated. In addition, the text provided serves as a a template that you only have to edit for each application. But things can be further simplified.

Available on Flathub, there is an app called Desktop File Creator. basically it is an interface in which we will tell her the name, the path to the executable, if it will be executed in the terminal or not... and she will take care of doing the rest for us. But if we want something even simpler, we can create ourselves a script/mini-app to do all this from the terminal (seen in the header image). The code in Python would be the following:

#!/usr/bin/env python3 from io import open import os file_name = input("Name the .desktop file: ") version = input("Application version: ") app_name = input("Application name: ") app_comment = input("Application comment: ") executable = input("Path to executable: ") icon = input("Path to app icon: ") terminal = input("Will it run in terminal ? (True for yes, False for no): ") tipo_app = input("Type of application (put Application if you have doubts): ") categories = input("Categories in which this application falls: ") folder = input( "Folder where the executable is: ") def createDesktop(): note = open(file_name + ".desktop", "w") entry_text = ('[Desktop Entry]') version_text = ('\nVersion=' + version ) app_name_text = ('\nName=' + app_name) comment_text = ('\nComment=' + app_comment) executable_text = ('\nExec=' + executable) icon_text = ('\nIcon=' + icon) terminal_text = ('\ nTerminal=' + terminal) text_tipoapp = ('\nType=' + type_app) text_categorias = ('\nCategories=' + categories) text_startupNotifyApp = ('\nStartupNotify=false') text_path = ('\nPath=' + folder) text = (input_text + version_text + application_name_text + comment_text + executable_text + icon_text + terminal_text + app_type_text + categories_text + startupNotifyApp_text + path_text) note.write(text) note.close() appName = app_name + ".desktop" os.system('chmod + x ' + appName + '\nmv ' + appName + ' ~/.local/share/applications') print("Successfully created .desktop file. It's in ~/.local/share/applications/ and should also appear in the app drawer.") createDesktop()

explaining the code

From the above:

  • The first line is what is known as a “Shebang” and indicates what the script has to be opened with (it may be different on some Linux distributions). It shouldn't be necessary if we navigate to the path where we save the .py file and launch it with "python file_name.py«, but yes if we want to tackle a little more as we will explain later.
  • The second and third lines import what is necessary to create the file, since it is necessary to be able to write to the hard drive.
  • Variables are then created that will later be used to write the content to the .desktop file.
  • In the createDesktop() function, it first opens a file, then adds the shortcut information parameters, then creates the .desktop file, gives it execute permissions, and moves it to the ~/.local/share/ folder. applications. And that would be all.

I would say that it couldn't be easier, but it would be easier if there was a way to create them with a context menu of Nautilus, Dolphin or the file manager that we are using. The problem is that on Linux this doesn't work like that. In .desktop files you can also add other information, such as translations and other ways to open applications (such as incognito mode in a browser), so creating these types of shortcuts is not so straightforward, worth the redundancy.

And I already forgot, if we want to be able to launch the previous script from any terminal window, we must move the .py file we created to the /bin folder. Don't let anyone who doesn't know what he's doing get used to it, because the executables go in that folder and you have to be careful what you touch. In any case, there are always options. You just have to know where to look. Furthermore, creating and sharing is also a common thing in Linux.


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.