List of available environment variables

Is there a place I can see all available “environment variables” and their values?

For example, I’d love to use the name of the computer or hard drive as a variable.

1 Like

Here are your specific requests.

scutil --get ComputerName

will print the computer name.

ls /Volumes

will give you the list of volumes.

Here's an example of a script I use that will only run on a machine with a specific serial number:

Hey Jack,

You can run from an Execute Shell Script Action:

env

Gets you some.

set

Gets you more.

F=~/"Desktop/KM_ENV_Dump_`date '+%Y.%m.%d.%H%M%S'`.txt"
set > $F;
open -e $F;

Dumps to a dated file in the Finder and opens it in TextEdit.

Some AppleScripts for your specific wants:

tell application "Finder"
  name of startup disk
end tell

computer name of (get system info)

boot volume of (get system info)<br>

Some of these types of things are available as KM tokens.

Keep in mind that KM’s 'Execute Shell Script` Action produces an environment that does not inherit any customizations you’ve made to your system-level-shell.


Best Regards,
Chris

2 Likes

Thanks, guys.

Some of these types of things are available as KM tokens.

Chris, your response above was exactly what I needed. I had not examined the tokens closely enough. I'm now using "Machine Unique ID" token to make macros run differently on my work mac.

^ I have been able to use the %MacName% as above, however would like to have this for some computers at home but not others…

Trying to find that ‘OR’ command within the text box however unsure of the command…

The Text:
%MacName%

contains Jane | Bob ?
contains “Jane” | “Bob” ?
contains Jane OR Bob ?

Use a regex match with Jane|Bob

Thanks Peter, I used contains and didn’t work until I had to use the match.

For completeness (and for later readers), there is also

JavaScript for Automation

ObjC.deepUnwrap($.NSProcessInfo.processInfo.environment)

AppleScript

use framework "Foundation"
use scripting additions

current application's NSProcessInfo's processInfo's environment as record

Tho as Chris points out above, Terminal.app environment settings are not automatically exported to the fresh shell inherited by a script process.

For explicit exports and customizations see:

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

1 Like