0

i have this bash script: test.sh that i would like to be able to find in my apps and/or as a favorite in my dock.

since i've never done that i relied on: Execute sh script from *.desktop file?

i then created a file: test.desktop

[Desktop Entry]
Name=TEST
Comment=bash copy/resize
Exec=bash ~/mybash/test.sh
NoDisplay=False
Terminal=False
Type=Application
Icon=~/usr/share/pixmaps/copy-resize.png
Categories=Accessories;

set permission to execute, and created /usr/share/applications, /usr/share/pixmaps and /mybash in my home directory to start with. I could right click it and run as application, but it was not visible in settings>applications

dragging it to the desktop didn't help, so found: Cannot launch .desktop files (opens with Text Editor instead)

since i did not know how to find the /.local folder i decided to copy the file:

sudo cp ~/usr/share/applications/test.desktop ~/.local/share/applications

It now can be found under settings>applications alas it is listed without the icon but i cannot find it under apps. If i search in the app view (9 dots) i find the /.local/share/applications/test.desktop listed as a text file and right clicking does not give me the option to set it to favorites and left it opens in geany (as it was made with that app)

if i try in a terminal:

bash ~/.local/share/applications/test.desktop

it starts the sh script, output in terminal:

/home/maarten/.local/share/applications/test.desktop: 1: [Desktop: not found
/home/maarten/.local/share/applications/test.desktop: 3: Cam: not found
starting gui...

and that does it's job, in the dock I see the icon I choose and a dot that it's active and the gui opens. so except for the icon location in settings>applications this part works. But double clicking still leads to the text editor. If placed on the desktop i can set 'allow launching' changing the file icon to a text document, but then double click does nothing.

the big question is Who can fill me in on what i missed in making this work?

At this moment i've copied test.desktop to:

(home)/test.desktop, (with/without allow launching)
/Desktop/test.desktop, 
/.local/share/applications/test.desktop and 
/usr/share/applications/test.desktop. 

on all placed i can use a rightclick and choose to run as program, but nowhere does a double click lead to opening the file.

  • You said: "if i try in a terminal: bash ~/.local/share/applications/test.desktop, it starts the sh script and that does it's job. I do not believe that that worked with the file you provided. Please check and correct the information. – vanadium Apr 08 '22 at 14:22
  • i just tried again and it might not be perfect: – user74341 Apr 08 '22 at 14:57

2 Answers2

1

Quick-Fix

Try the steps below

  1. Change your .desktop file by replacing ~/some/path/file.ext to /home/<username>/some/path/file.ext

  2. You can also set Terminal=True to open a terminal when running the .desktop shortcut.

    Your .desktop file should look like this:

    [Desktop Entry]
    Name=TEST
    Comment=bash copy/resize
    Exec=bash /home/<your-username>/mybash/test.sh
    NoDisplay=False
    Terminal=True
    Type=Application
    Icon=/usr/local/share/pixmaps/copy-resize.png
    Categories=Accessories;
    

Expected Behavior w/ Screenshots

  • When viewing .desktop files in a File browser like Nautilus, the files will appear like typical text files or potentially with gear symbols on them enter image description here

  • When double-clicking on a .desktop file the default file action set for that mime-type in xdg-settings will be applied, in this case the .desktop file will open in your default text-editor enter image description here
      you can check your xdg-mime settings like this: enter image description here

    As you can see, my default is GNOME Builder too

 

  • I've been showing the above using the JetBrains CLion IDE .desktop file, now let me show you how to properly utilize the .desktop file.
    1. first access your application menu with Super/Win key on your keyboard, or by clicking on the 3x3 grid icon on your taskbar [note: this can differ depending on your desktop environment and your configuration] enter image description here

    2. start typing your application name; in your case TEST but in my example CLion enter image description here

Info

  1. it's already been pointed out that using the ~ shortcut doesn't work inside a Unity Launcher .desktop file

    which is why you need to specify the full path to your script and its icon file

  2. you should use either /home/<username>/.local/share/applications/ for just your user account to see the entry
    or /usr/local/share/applications/ for all users of the machine to see the entry

  3. you don't need a $HOME/usr/ folder, $HOME/.local/ is probably what you were thinking of

  4. refer here for more details about how to properly setup a .desktop launcher file

References

Unity Launcher Specification
Ubuntu-desktop command manual

wsmyth
  • 130
  • 2
  • 6
  • Thanks, that at least got rid of the '~' but still, a double click opens the file in the editor. only right click and 'run as program' or via terminal with bash – user74341 Apr 08 '22 at 16:47
  • yes thank you, after doing that, when placed on the desktop and 'allow launching' is activated it will double click, maybe you can add that to your answer – user74341 Apr 08 '22 at 16:51
  • if the program is active, i have the icon in my dock, but on the desktop it looks like a text file, do you know the solution for that one too? – user74341 Apr 08 '22 at 16:54
  • the double click opening in a text editor is standard expected behavior. these files are meant to show up in your Application menu when you click on your keyboards Super / Win key or when you search for an application in your Activities Overview – wsmyth Apr 08 '22 at 16:54
  • typically it showing as a text file means you need to set the .desktop file's executable bit using chmod +x filename.desktop. Or right-clicking with your mouse and setting permissions through the GUI. – wsmyth Apr 08 '22 at 16:55
  • thanks William, i grasp it much better now, will write it out for the next time this comes up....and changed the icon via the gui, so that's settled too :) – user74341 Apr 08 '22 at 16:58
  • You're welcome, @user74341. Not to force your hand but if you up-vote my answer that will help give me more reputation points and allow me to do more on this forum. If you don't think my answer deserves and up-vote, then don't feel obligated to provide one. – wsmyth Apr 08 '22 at 17:48
  • well, you went out of your way after i have it working, so i'll upvote you..the least i can do.....although not working as i expected, i was thinking it would end up in that 3x3 grid as a launchable application too, but there it are just editor launches again without the proper icon as i see in your screenprint. – user74341 Apr 08 '22 at 17:54
  • got it! thanks..all works as expected. – user74341 Apr 08 '22 at 18:11
  • yesterday there was a moment that i thought both my scripts were working, i have one that starts a gui and one that stayes in the terminal. since these changes i do not see the terminal window before the gui starts and the non-gui version does not open a terminal window. If i right click and choose 'run as program' it will however still open a terminal (with the errors seen in the first post) – user74341 Apr 09 '22 at 10:21
  • solved with adding gnome-terminal -e – user74341 Apr 09 '22 at 11:21
0

It works if the desktop file is located in the home folder. The options 'set to favorites' appears.

There is also useful application called 'menulibre'.

Mahler
  • 611
  • 8
  • 18
  • i copied test.desktop to the home folder, no option set to favorites appears. installed menulibre, but am unsure what to do with it. – user74341 Apr 08 '22 at 15:56
  • First sentence definitely is a wrong answer. Suggestion on second line might not work here if the desktop file is already valid to start with. Did you test anything? – vanadium Apr 08 '22 at 16:12
  • if i open a winndow and go to home i see desktop, downloads, etc at the end there is one file: test.desktop, if i right click that i see: open with, run as program, cut, copy, move to, copy to, move to trash, rename, compress, send to, star, properties...where can i find set to favorites? – user74341 Apr 08 '22 at 16:23
  • and i tested what you see at the beginning and some more, but i do not really know what i'm doing (wrong) so it's hard to come up with a test. – user74341 Apr 08 '22 at 16:26