Environment variables

Is there a list of environment variables that I can use with Keyboard Maestro with If Then Else Action?

What I would like to do is based on the following:

  1. I have a folder which I would like to open with a macro in Finder/Path Finder (which I can use the File, Folder or Application action).

  2. However, on one computer, because of the smallness of the internal disk I have to store this folder on a second disk, while on the second computer the internal disk is big enough to hold the folder.

  3. I wanted to use the If Then Else Action, so that condition tested was for the HOSTNAME of one computer, and if not false then the folder on that computer would be opened, and if I ran the macro on the second computer, the else part of the If Then Else Action would be run.

However, it seems that the HOSTNAME environment variable is not supported by Keyboard Maestro. Is this correct? If there is another way to do what what I need, I would be grateful for suggestions.

(I copied the HOSTNAME variable accurately)

1 Like

If you are running Yosemite, this macro should show the KM and Environment variables available (Environment variables are those not prefixed by "KMVAR")

(It should also show you your current system info strings)

Environment and KM Vars, System Info.kmmacros (19.3 KB)

// Clipboard listing of variables (Environment + KMVAR) 
// and system info
function run() {
  var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true && a),
    str;
  sa.setTheClipboardTo(
    str = JSON.stringify({
        attribs: sa.systemAttribute().reduce(
          function (dct, k) {
            dct[k] = a.systemAttribute(k);
            return dct;
          }, {}
        ),
        info: sa.systemInfo()
      },
      null, 2
    )
  );

  return str;
}
1 Like

Or a version which skips the KMVARs (you can view them anyway in KM Preferences) and shows the available Environment variables, with their values, and with the system info strings:

Environment Variables and System Info.kmmacros (19.4 KB)

osascript -l JavaScript <<JXA_END 2>/dev/null
// Clipboard listing of environment variables 
// and system info
function run() {
  var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true && a),
    str;

  sa.setTheClipboardTo(
    str = JSON.stringify({
        attribs: sa.systemAttribute().reduce(
          function (dct, k) {
            if (k.indexOf("KMVAR") !== 0) {
              dct[k] = a.systemAttribute(k);
            }
            return dct;
          }, {}
        ),
        info: sa.systemInfo()
      },
      null, 2
    )
  );
  return str;
}
JXA_END
1 Like

So using systemInfo() rather than systemAttributes(), and replacing the %MacName% token with the computer name you are looking for, you could branch on the active computer name like this:

Branch on computer name.kmmacros (20.8 KB)

Using this shell script in the test:

osascript -l JavaScript <<JXA_END 2>/dev/null
(function () {
  var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true && a);

  return sa.systemInfo().computerName === "$KMVAR_computerName";
})()
JXA_END
1 Like

But most simply of all : - )

Just enter a string like "Victor's Mac mini" into a test comparing it with the current value of the %MacName% text token:

1 Like

To list all the environment variables and their values, simply run the "env" command:

That will include all the KMVAR_ Keyboard Maestro variables, but you can ignore them and look at what remains.

As @ComplexPoint says, you can just use the %MacName% or %MacUUID% tokens.

3 Likes

ComplexPoint (and Peter),

Thank you for the replies. The last suggestion by ComplexPoint works fine.