0

I appreciate that this is a similar question to others, but I am unable to ask any questions as I don't have 50 reputation. I have tried to follow Desktop shortcut for terminal command

Ubuntu 20.04.4 LTS does not allow me to right click on Desktop to create a new file (only New Folder). I ran sudo gedit from terminal, and created the following;

[Desktop Entry]
Name=Jupyter Notebook
Exec=jupyter notebook
Terminal=true
Type=Application
Icon=/usr/share/icons/HighContrast/32x32/devices/pda.png

I saved the file to Desktop, and it appears with a cog icon and my filename > jupyter.desktop

I then change the permissions from terminal to executable, chmod +x (later to +rwx as it doesn't work)

If I attempt to click on the file, all it does is open it in gedit.

If I go to terminal and try to run

./jupyter.desktop
./jupyter.desktop: line 1: [Desktop: command not found
./jupyter.desktop: line 2: Notebook: command not found
./jupyter.desktop: line 3: notebook: command not found

NB I have added 'quotes' to lines 2 and 3 (Name='Launcher Name' and Exec='exec param'), but I still get line 1 error, so I don't know if it correct to add the quotes or not!

I have been referring to this as a Shortcut (yup, I come from MS), but think it is more correct to say Launcher?? Out of interest, is the .desktop mandatory, or could I have no extension, or even .lnk?

I would very much appreciate some help. Thank you

>>>> EDIT after the very useful information from @vanadium >>>

I do not think it is possible to create a .desktop file in a subdirectory of XDG_DATA_DIRS, at least, my attempts to 'get a result' in the Application Explorer failed. It is however noted that the applications shown appear to be cached, as I believe I had experiences that were not immediate. It also appears that the .desktop files are somehow validated, as I get the impression that invalid information will stop the application being shown.

You will have guessed that I am trying to create a Jupyter Notebook shortcut. The reason for doing this was to specify my working directory. During my journey, I found that it appears my installation of notebook actually created a .desktop within ~/.local/share/applications. I had attempted to modify Exec=/home/myhome/my_working_directory/jupyter-notebook %f but this failed, leading me to the conclusion regarding validation.

On further investigation, I found I could use --notebook-dir, and so my final solution is to modify ~/.local/share/applications/jupyter-notebook.desktop

[Desktop Entry]
Name=Jupyter Notebook
Comment=Run Jupyter Notebook
Exec=jupyter-notebook --notebook-dir="/home/myhome/my_working_directory" %f
Terminal=true
Type=Application
Icon=notebook
StartupNotify=true
MimeType=application/x-ipynb+json;
Categories=Development;Education;
Keywords=python;

Hopefully the question, and great information from @vanadium, will be useful to others in the future (it is a shame some old questions cannot be deprecated, as they may steer others down the wrong path ...pun almost intended)

Further EDIT, good references >>>> https://www.howtogeek.com/445303/how-to-create-desktop-shortcuts-on-ubuntu/

https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html

1
  • Strange, my initial question was badly formatted. I knew so, so I added a comment to say that lines had Carriage Return that were being stripped out. The weird thing being, the comment was removed, and the offending lines formatted correctly .... I needed to indent by four spaces (as shown in the Code editing tip). No idea how this could happen automatically(???)
    – Chrizk
    Mar 11, 2022 at 16:16

1 Answer 1

1

"How to create a desktop file" is answered simply as: the way you do. With a text editor. There are no graphical tools installed by default anymore that can do this. However, you can still install "Alacarte" or "Menulibre" to edit menu entries, which in fact automatically creates a .desktop launcher in your ~/.local/share/applications directory.

"How to use a desktop file" is another aspect. .desktop files are not designed to be run. Their primary purpose is to populate your application menu, and, when launched from a menu, inform your desktop about how to launch a program, what icon to use, open it in a terminal or not, etc.

Place your .desktop file in either ~/.local/share/applications or in an applications directory under one of the directories indicated in the XDG_DATA_DIRS variable to have the program appear in the menu. When placed under ~/.local/share/applications, the launcher is visible to you as user only. When placed in a system directory, i.e., /usr/local/share/applications, de launcher will be visible to all users.

Placing applications on the desktop is not anymore quite supported in the standard Ubuntu desktop. The idea is that you place your most frequently applications on the Dock, which is always visible. Your desktop, in contrast, is covered most of the time - at least if you are doing something.

Still, the Ubuntu desktop supports them. However, before they work as you expect, two requirements must be met:

  1. You have to set the "executable" bit, which in this context is used to indicate the desktop environment that that launcher is "trusted". Right-click the launcher, select "Properties" and check "Execute" on the "Permissions" tab.
  2. The first time you try to launch it, the "Untrusted application launcher" dialog appears. Confirm once more you trust the launcher by clicking "Trust and Launch".

After that, your desktop launcher will work as you expect.

2
  • thank you for the really quick reply and detail. One question regarding the applications directory, can I place the file within a subdirectory of any listed (none appear to have a file in the directory) ~/Desktop$ echo $XDG_DATA_DIRS /usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop Would you mind telling me how I can get the carriage returns to show when I provide command line examples? >>>> ignore that, just noticed that it has auto-formatted, despite the preview showing the CR missing :)
    – Chrizk
    Mar 11, 2022 at 13:31
  • Yes, "applications" is a subdirectory of any listed, and if it does not exist, you can create it (e.g by default there is no /usr/local/share/applications (I believe))
    – vanadium
    Mar 11, 2022 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .