13

I want to set the 'Path' variable in a .desktop file relative to the location where the desktop file is located and I can't manage to do that.

When I don't declare it or set it to blank it defaults to my home folder no matter where I run it from; and it doesn't accept values relative to my current location within the file system.

coversnail
  • 6,336
  • 14
  • 40
  • 70
kicsyromy
  • 740
  • 1
  • 5
  • 19
  • I've had this problem. I've found workarounds, although sometimes radically complicated depending upon what you are doing. Are you using a work around at this time? If not, I may be able to help with that. I have not found xdg docs that explain much on this. – bambuntu Apr 06 '12 at 20:42
  • OP are you still looking for an answer? If so, you may need to repost your question as this is marked for closure. Regards, – Ringtail Apr 07 '12 at 23:15

5 Answers5

18

You can kludge around this by using an in-line bash mini-script on your Exec. This will add the .desktop file's path to PATH prior to running your command.

Exec=bash -c "export PATH=$PATH:`dirname %k`; your_command"

%k will be substituted by the path of the desktop file itself. The dirname command chops off the filename part, leaving only the directory. Once PATH is set like this, your_command can be invoked without a full path.

AsukaMinato
  • 199
  • 1
  • 5
roadmr
  • 33,892
  • 9
  • 80
  • 93
4

You cannot set the CWD inside a .desktop file. If you want an application to have a specific CWD, you will need to write a simple wrapper script for the application, that looks something like this:

#!/bin/sh

(cd /where/you/want/it/to/be && exec your_program)

You can replace your_program there with $@ and run the script with your_program as an argument, like run-in-dir.sh your_program. This way you can use the same script to wrap any program you want to start in that directory.

AsukaMinato
  • 199
  • 1
  • 5
dobey
  • 40,344
  • 5
  • 56
  • 98
  • This is incorrect. The Path key in the .desktop file sets the working directory for the executed program. But this is not added to the $PATH variable so the full path of the application to be executed must still be provided in the Exec key. – Kevin Whitefoot Sep 03 '21 at 15:30
3

I used this:

Exec=bash -c 'cd $(dirname %k) && ./SCRIPT_NAME'

The %k is the full name of the .desktop file including its path. It's then used by dirname to get a location and change directory to that location. Finally, now that it's in the right place, it finds the script and runs it.

Artur Meinild
  • 21,605
  • 21
  • 56
  • 89
Marian
  • 131
  • 1
1

This worked for me Ubuntu 14.04:

Exec=bash -c "cd %k && ./app.run"

Latest spec says that %k points to the location of the desktop file:

%k - The location of the desktop file as either a URI (if for example gotten from the vfolder system) or a local filename or empty if no location is known.

nexayq
  • 133
  • 1
  • 11
1

For directory names with spaces in the name, this finally worked for me:

Exec=/bin/bash -c 'cd "$(dirname "$0")"; wine game.exe -windowed' %k

Credit to https://unix.stackexchange.com/a/144428/61349

ThorSummoner
  • 3,129
  • 3
  • 20
  • 31