Apps launched by KM have a different environment than apps launched by the Finder

Another security install (I’m now on El Capitan 10.11.6) wiped my old
method for setting the PATH environment variable for apps launched in
the GUI. So I looked into a better method and came across Setting environment
variables in OS
X?
.

After issuing

sudo launchctl config user path <my-customised-path>

and rebooting I can launch apps like Emacs from the Finder, they have the
customised PATH and subsequently find all the programs in the added
directories. :slight_smile:

So far so good. Next time I start the same program via an Keyboard Maestro
short cut - and the app now has only the standard path as if it did
not recognise what launchd tells it.

Why is that? Does Keyboard Maestro not get launched via launchd? And
how can I set the PATH for all my apps whether they are started
through Finder or Keyboard Maestro?

Many, many tanks for you help!

Hey There,

How exactly are you launching Emacs from the Finder?

How exactly are you starting the program with Keyboard Maestro?

-Chris

In KM I use the Activate Application macro which I trigger via a key combo.

In the Finder I just double click the Emacs.app item.

Hey @halloleo,

Hmm…

Emacs is a Unix executable, so there wouldn’t be an app in the Finder.

Since you do have an app in the Finder you must have installed something.

What might that be?

-Chris

Hi Chris, I don’t use the OS Emacs, but Emacs installed via Homebrew which does provide an app.

My big question is how does the KM engine get launched. What environment does it inherit? (User, System or else)?

I actually found it out by trial and error. - If I set the PATH variable for the system domain as well (like this:

sudo launchctl config system path <my-customised-path>

), then Emacs receives my customised PATH when launched via a Keyboard Maestro trigger.

So, if I understand things correctly, this means that the KM engine is launched by a system process, not a user process. Is this something to be mentioned in the doco, @peternlewis?

Keyboard Maestro Engine is either launched by the Keyboard Maestro editor (when you launch it), or by whatever process the system uses to launch Startup Items (which I would have assumed would get the same environment as other applications launched by the Finder, but perhaps not).

The parent process of both the Keyboard Maestro Editor and the Keyboard Maestro Engine is launchd — the same as any other normally launched application on macOS.

Verify this by opening the Activity Monitor and looking at the process detail or the hierarchical view.

As far as I know path issues do NOT come into play with Keyboard Maestro EXCEPT when a command-shell is run by a KM action. When a command-shell is run KM does NOT pick up the system environment (by design). The user must manually pick up the environment by invoking a command similar to this:

source ~/.bashrc

You can also set up environment variables globally available to Keyboard Maestro-run command-shells in KM’s variable preferences:

Name:

ENV_PATH

Content:

/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The Execute a Shell Script page of the wiki is worth reading through.

But — I don’t think it will be relevant to the issue you’re having with Emacs.

-Chris

Well using the system domain (i.e. sudo launchctl config system path <my-customised-path>) works for me, so I guess I leave it at that - although I still don’t understand why sudo launchctl config user path <my-customised-path> is not “enough”…

Thanks @ccstone and @peternlewis for paying attention to this issue. :smiley: