Create a PATH Environment Variable for Keyboard Maestro and Add /usr/local/bin to the Default Path


#1

Hey Folks,

Did you know that Keyboard Maestro by default only sees a very few of the Unix executables on your system?

So under normal circumstances you may have to call an exe by its full path in an Execute a Shell Script action like this:

/usr/local/bin/AudioSwitcher -a

That’s cumbersome and hard to read.

It’s much nice to be able to write (and read):

AudioSwitcher -a

Fortunately Peter has allowed us to set Keyboard Maestro’s shell environment variables.

See the Execute a Shell Script section of the wiki for more information.

The appended macro will add the /usr/local/bin/ directory to Keyboard Maestro’s PATH variable, and it will be stored in ENV_PATH. You can see this in Keyboard Maestro’s preferences’ variable panel.

If you already have customize your ENV_PATH variable the macro will exit and leave it alone.

If you use the MacPorts package manager for Unix executables you might want to use this command, instead of the one in the macro. It will add the MacPorts directories and the /usr/local/bin/ directory.

echo opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;

Since this alters your Keyboard Maestro environment I’ve left off a trigger. The easiest way to run it in Keyboard Maestro 8 is with Cmd-Shift-R in the editor.

-Chris


Create a PATH Environment Variable in Keyboard Maestro and Add -usr-local-bin to the Default Path.kmmacros (6.8 KB)


Keyboard Maestro “Convert PDF Files into Text Files in the Front Finder Window” Macro
Error when running terminal command through Applescript or KM
Switch Audio Source
Backup Keyboard Maestro -- Export ALL Macros, Clipboards and Variables
Execute Shell Script with Path of Finder not working
Merging PDF Documents
#2

Hey Folks,

If somehow you manage to screw things up you can just delete the variable ENV_PATH from Keyboard Maestro’s preferences’ variable panel and start over.

-Chris


#3

Hey Folks,

You can use the above macro by @ccstone if you prefer, but rather than going to the trouble of downloading, importing, and changing it, I find it simplest to just make the change manually.

The KM Wiki provides excellent instructions here:

How To Set a Path in Shell Scripts – KM Wiki

It is easy to check for and add a KM Variable in the Variable tab of the KM Editor Preferences:

  1. Press ⌘, to open Preferences
  2. Click on the Plus button to add a Variable
    . image
  3. Enter the Variable Name. “ENV_PATH” in this case:
    .
    image
    .
    • IF the variable already exists, its value is shown in the Value panel.
      Just click there to change the value.
    • IF not, press RETURN, then enter the value.

So, choose whichever method works best for you: macro or manual.
For me, manual works best because I rarely need to change the “ENV_PATH” variable.