How to Access an Environment Variable in a .Desktop File's Exec Line

How do I access an environment variable in a .desktop file's exec line?

By default environment variables do not seem to be resolved by all implementations, however you can instead exec sh, which will resolve the passed environment variable. Note that the desktop spec also requires you to escape the = and $ character with a backslash. So you want:

Exec=sh -c "myprogram --folder\=\$HOME/.special"

For the full list of characters that need escaping, see the specification

What's the environment variable for the path to the desktop?

I found that the best solution is to use a vbscript together with the batch file.

Here is the batch file:

@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

Here is findDesktop.vbs file:

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)

There may be other solutions but I personally find this one less hackish.

I tested this on an English PC and also a French PC - it seems to work (Windows XP).

How to get Desktop location?

You can use os.environ["HOMEPATH"] to get the path. Right now it's literally trying to find %HOMEPATH%/Desktop without substituting the actual path.

Maybe something like:

shutil.copy(txtName, os.path.join(os.environ["HOMEPATH"], "Desktop"))

Linux desktop file Exec doesn't used the user session

Thanks anyone to have participate to this question.

I have solved this issue by implemented use of pkexec instead of gksudo.
pkexec seems to reuse the current user environment then I don't have this issue anymore.

Thanks.

can someone help me get python to run my scripts in CMD in windows 10 without having to cd to exact path each time.?

You should be able to do this as long as the script is in the module search path, for example, the PYTHONPATH environment variable. You should then be able to run the script like this:

python -m script

How to prefix file with custom commands when debugging in PyCharm?

I was able to do this by installing the envfile plugin in PyCharm. This plugin can read in a .env file when starting a process. Basically I did the following:

  1. Create a script that generates a .env file, envfile.env and name the script generate.sh
  2. This generate.sh script is a shell script that basically does: aws-vault exec $AWS_PROFILE -- env | grep AWS_ > envfile.env, so all the aws creds are in the envfile.env. Possibly add other environment variables if you need so.
  3. Execute command above at least once.
  4. Install the envfile plugin in pycharm.
  5. In the Run configuration, a new tab appears with 'EnvFile'. In this tab, enable the EnvFile. Add the generated envfile.env (see previous).
  6. Do Tool / External Tools and create an external tool for the generate.sh. This way you can execute the script from PyCharm.
  7. Again in the Run configuration add a Before Launch that executes the External Tool generate.sh.

Warning, the temporary aws-creds are in the plaintext envfile.env.

Atom opens a new file called ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT=false

I am going to make several assumptions. You are running on Ubuntu and you've installed Atom via Snap.

If these assumptions are correct the cause of the issue is a misconfigured application menu item from the Snap package author.

To fix it you just need to run this command:

sudo sed -i 's/Exec=env BAMF_DESKTOP_FILE_HINT=\/var\/lib\/snapd\/desktop\/applications\/atom_atom.desktop \/snap\/bin\/atom ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT=false \/usr\/bin\/atom %F/Exec=env BAMF_DESKTOP_FILE_HINT=\/var\/lib\/snapd\/desktop\/applications\/atom_atom.desktop ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT=false \/snap\/bin\/atom %F/' /var/lib/snapd/desktop/applications/atom_atom.desktop

TL;DR {

Here is a detailed explanation of what's causing the issue and what the command above does. It may be useful if the file has been changed since the answer was written.

The actual cause for the bug is that this menu item file:

/var/lib/snapd/desktop/applications/atom_atom.desktop

It has a typo in it and what should be environment variables are set after calling
the atom executable, resulting in Atom treating it as arguments in the form of
files that it should open.

#                                                                                     ▼ Executable   ▼ Not an environment variable                   ▼ Not an executable
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/atom_atom.desktop /snap/bin/atom ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT=false /usr/bin/atom %F

It should instead be:

#        ▼ Environment variable                                                       ▼ Environment variable                          ▼ Executable
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/atom_atom.desktop ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT=false /snap/bin/atom %F

The solution above uses sed to search and replace the file and fix the issue.

}

NOTE: The command will work until the Snap author updates the menu item file (.desktop) when hopefully the issue would have been resolved.



Related Topics



Leave a reply



Submit