Per-application environment variables on Mac OS X - macos

There are several different ways documented of setting environment variables either system wide or per user, see this question.
Are there any options for setting environment variables on a per-application basis?
E.g. Is there a way to use something like ~/.MacOSX/environment.plist or launchd.conf but have some variables only inherited by specific apps?
The only way I can think of is to modify each applications Info.plist, but this involves changing an applications bundle which doesn't seem like a terribly good idea.

Typically this is done by wrapping the desired app in a small shell script or automator action that sets the environment variable and then launches the app. For example, I have an automator "application" that has the following rule:
Run Shell Script:
NSZombiesEnabled=YES open /Applications/MyApp.app
When I want to launch it this way, I just run the automator action.

Related

How to make PowerShell recognize changes in environment variables?

When I change any environment variable in Windows using GUI (System Properties/Environment Variables/System Variables) changes are reflected immediately in cmd terminal, but PowerShell shows an old value.
Here cmd recognized that I changed JAVA_HOME value from C:\Users\nikif\.jdks\openjdk-19.0.1 to C:\Users\nikif\.jdks\corretto-16.0.2
PowerShell still shows the previous value - C:\Users\nikif\.jdks\openjdk-19.0.1
Is there any command that will make PowerShell recognize the environment variable change made in GUI?
Or maybe there is a better way to change the environment variable from PowerShell?
Thank you.
This all should really be a comment (I tried above), but attempting to explain why the question, as it stands, is too vage requires just more explaination.
Processes get their environment when started by the OS. They basically get a copy the environment as it was at that point in time. When you start a process you can (using the respective APIs) pass additional or altered environment variables.
During the existance (runtime) of a process you cannot change the environment.
Shells are a common exception, as they provide specific syntax that does that (set NAME=VALUE in CMD.EXE and $env:NAME="VALUE" in PowerShell, for example). An application itself could also do such stuff from its code using respective APIs. But in general, there is no way to change variables from the outside.
One process will never see the changes done to the environment in a different process. For example, if you have to separate CMD.EXE sessions running and change a variable in one of them, the other will not know. The environment is private to each process.
A potential exception is the (global/system) environment variables you can set using the Computer/Properties/Environment setting (applet). The system will send a WM_SETTINGCHANGE window message indicating the that environment has changed. Applications can register to this message and act accordingly.
However, neither PowerShell nor CMD.EXE do seem to listen to this message (it would require a (hidden) Window anyway and both are console applications). And frankly, it would be not good if they did. Consider CMD.EXE just execuing a batch file and a variable (say PATH) changes underneath - security issues and general havoc all over.

Adding custom commands to terminal along side application

I am working on an application where we are using xtermjs and node-pty inside of an electron application. We are adding a terminal to our application and would like to add some custom commands that are used in the terminal that are related to our application.
What are some options for adding these commands?
We want them installed with the application.
They don't have to be useable inside an 'external' terminal, but it is ok if they are. By external, i mean your normal terminal. Not our xterm & node-pty implementation.
And we want them to behave the same as other normal unix commands. Where you can pipe with other commands && them together and stuff.
I have played around with intercepting commands between xterm and node-pty and that was a disaster. I am now considering, just writing bash scripts for the commands and having the installer manage putting them where they need to be so they can be used.
Just wondering what my options are, thanks.
You can simply put all your executables in a directory that you add to your PATH when you invoke the shell in your terminal emulator.
The commands will be available to the user like any others in any construct that accepts commands, regardless of the user's shell or shell version (i.e. it'll work equally well in bash, zsh and fish).
If you need the commands to coordinate with your terminal emulator (e.g. if you want to process the command in JS in your Node.js process), you can arrange that via a second environment variable containing e.g. a host/port to connect to.

Electron get or set Environment variables in Windows?

I know that from JavaScript it is not possible to get or set Windows Environment Variables. I have deployed a Electron app as a executable (as suggested here), is it possible now to get or modify a Windows Enviroment Variable. If it is can someone point me in the right direction ?
You can use NodeJs to read the enviroment variables via process.env you can read all methods and possabilities on the process documentation.
Example:
console.log(process.env.PATH)
Writing environment variables in a running process is always temporary. You can set environment variables by calling/spawning system commands/tools like setx under windows.
Persisting them does not change the running environment in the process for that you have to restart the process. So you need both, set the variable as described above and in addition persist it with system tools/commands.

Regularly change environment variables

I'm a developer and I work on some applications which need different environments.
One of my problems is related to the OS environment variables.
On Windows 7, do you know if there's a way to easily/regularly change the environment variables ?
Of course, I could change it manually but I'm looking for an easier solution.
Changes should be "permanent" till my next "task switch".
Create a set of batch files that set the variables the way you need them, then run the appropriate one for your task.
The setx command line tool will set them permanently.
http://technet.microsoft.com/en-us/library/cc755104(v=ws.10).aspx
I think Win+Break is a real time saver in getting to any System Config screen.
Further more, you can import/export system/user environment variables from the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
Keep in mind that changing these will in not alter your running processes' environments. My procedure for this:
Killing explorer.exe from the Task Manager, and then
Run-ning a new explorer.exe (from the File menu in that same Task Manager)
will usually do what you want. Note that this 'soft' reload does not include long-running background processes.
Bonus Tip
use Process Explorer to view exactly what environment a running process is using:

OS X: Attach to a running process and read its environment variables

Is it possible (and if so, how?) to read a running process's environment variables. To be more specific, I am concerned with environment variables set by the process itself (using setenv() or similar), not the general user session environment variables set when the process is launched.
I figured that DTrace would be the tool for this job, but the only solution I've found online (from this mailing list thread) appears to be specific to Solaris because the script fails to execute on my Mac.
Perhaps Instruments exposes this functionality somehow and I just haven't found it yet?
I have also tried Tasks Explorer, which is a nice little application, and it shows process info including environment variables, but it does not show any environment variables set by the process itself, which is what I'm after.
Any help would be much appreciated!
I suppose you could try next way:
Inject you dylib into running process.
To read environment variables from injected dylib.
Via IPC (RPC, Shared Memory, etc.) send Environment variable into you application.
By the way, I have a plan to add this type of environment variables exploring into my Tasks Explorer.
Next links will help you with injection:
https://github.com/comex/inject_and_interpose
SIMBL/Bundle/dylib injection into Dock.app

Resources