How can I create a .desktop file [Desktop Entry] that executes a command in the current directory?

95 Views Asked by At

I want to create a .desktop file that runs a specific command in the directory where the file is. This is what I have:

[Desktop Entry]
Type=Application
Name=MyApp
Exec=bash -c 'cd $0 && node index.js; read' $PWD
Icon=obconf
Terminal=true
Categories=Utility

Basically, I want to execute node index.js in the directory where the .desktop file is. As it is now, the pwd is always the home of the user (even if I'm deeper in the hierarchy). How can I make it so the command is launched in the directory where the .desktop file is ?

This is on a Raspberry Pi 5, Debian (bookworm).

Thank you.

1

There are 1 best solutions below

0
djip.co On BEST ANSWER

As Barmar mentionned in the comments, the GUI does not change the current working directory. However, I just found in this nice guide that you can retrieve the path to the .desktop files by using %k. Hence, this works:

[Desktop Entry]
Type=Application
Name=MyApp
Exec=bash -c 'cd %k && node index.js; read'
Icon=obconf
Terminal=true
Categories=Utility

This basically tells Node.js to launch the index.js file in the directory where the .desktop file is located.

Thanks Barmar for your help.

Cheers!