2

The situation:

I need to set some environment variable to be accessed within a programm. It works if I open a Terminal window (zsh shell), set the variable via export VARNAME=VARVALUE and then launch the programm from that terminal window (for example by open ~/Applications/myApp.app.

The problem

Now I want the environment variable to always be set when I run the application by just double-clicking it in the Applications folder. Therefor I created the file ~/.zshenv and wrote the export statement in there.

Now when I open some Terminal window, the desired environment variable is already set (can be shown by using env command) and when I launch the application from terminal everything is fine. But as soon as I launch the application by just clicking the .app file the variable isn't set in the programm context.

I can't find any information online as to which shell/process launches applications if you just click them this way. Does anyone have an idea on how to fix the issue?

Benjamin A
  • 29
  • 2
  • How did you ensure, that `zsh` is executed when you double click on the App icon? I would not expect this to be the case, although in theory, Apple could have decided to implement it in this way. – user1934428 Sep 23 '21 at 09:17
  • @user1934428 I didn't, but I don't really know about the inner workings of MacOS either. I just search for a way to have environment variabels set within my program context when launched from finder. – Benjamin A Sep 23 '21 at 11:31
  • With your approach, you have to redefine the command executed by the clicking on the icon, to run a zsh-script, which *you* have to provide, and which then starts the app. But if you want to see **every** app on your Mac your modified environment, this is not a sensible approach either. I think the settings should then go into [`/etc/launchd.conf`](https://stackoverflow.com/questions/135688/setting-environment-variables-on-os-x#588442). If this does not work, I suggest that you ask this at [Ask Different](https://apple.stackexchange.com/), where MacOS configuration is discussed. – user1934428 Sep 24 '21 at 06:27

1 Answers1

1

open may be a child of your shell process, but myApp.app is not. open essentially asks launchd to start a new process, so myApp.app inherits its environment from launchd, not your shell.

The actual executable is stored in a subdirectory of the bundle ./myApp.app; you may be able to execute it directly if you can locate it.

I am not sure what the best way to configure the environment of a process intended to be started via open or the Finder; someone else may be able to provide a better answer.

chepner
  • 497,756
  • 71
  • 530
  • 681