User Tools

Site Tools


action:Execute_a_Shell_Script

**This is an old revision of the document!**

Execute a Shell Script

The Execute a Shell Script action executes a specified shell script using /bin/sh, either from a file or from the script embedded in the action.

Important:

  • It is best to always specify the language using the #! at the front of the script.
  • The script is executed with the sh shell, so if you want to use another language, or use any special kinds of shell-specific syntax, then you should specify the language.
  • For more details and examples, see How to Use Shebang at Top of Shell Script

To execute the script from an external file, choose the “Execute Script File” in the choices for script location.

You can choose what input (stdin) to send to the script (v8+) as well as where the output should go.

If the script fails, the action will fail (v9.0+), potentially aborting the macro.

Keyboard Maestro sets the environment variables for the script to include all your variables, using a prefix of KMVAR_ and your variable name with spaces changed in to underscores (_). For example, your Keyboard Maestro “File Name” variable will be available as the environment variable KMVAR_File_Name.

Keyboard Maestro also provides (v10.0+) a number of environment variables that contain information about the executing macro. These include:

environment variablemeaning
KMINFO_MacroNameThe name of the parent executing macro
KMINFO_MacroUUIDThe UUID of the parent executing macro
KMINFO_MacroGroupNameThe name of the macro group containing the parent executing macro
KMINFO_MacroGroupUUIDThe UUID of the macro group containing the parent executing macro
KMINFO_ThisMacroNameThe name of the executing macro
KMINFO_ThisMacroUUIDThe UUID of the executing macro
KMINFO_ThisMacroGroupNameThe name of the macro group containing the executing macro
KMINFO_ThisMacroGroupUUIDThe UUID of the macro group containing the executing macro
KMINFO_TriggerTimeThe unixtime the parent macro started executing
KMINFO_TriggerBaseThe type of the trigger that started the parent macro
KMINFO_TriggerThe trigger description of the trigger that started the parent macro
KMINFO_TriggerValuethe value associated with how the parent macro was triggered
KMINFO_ActionResultThe success or failure of the immediate past action
KMINFO_LastWindowIDThe window ID of the most recently displayed Keyboard Maestro Engine window
KMINFO_PromptWithListTextThe text typed in the most recently executed Prompt With List action
KMINFO_PasteByNameTextThe text typed in the most recently executed Paste by Name action

Using Keyboard Maestro Variables

Most Languages like bash, perl, ruby

In your script, use this format $KMVAR_VARIABLE_NAME where VARIABLE_NAME is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (_).

For example: File Name would be formatted as $KMVAR_File_Name.

If your variable already has underscores, you can use it as is.

Keep in mind that $KMVAR_VARIABLE_NAME is a shell environment variable, and this format is directly accessible to bash, sh, zsh, etc. Other languages may need a different method to read the shell environment variables.

Note that you can only read these environment variables. You cannot write to them (or more accurately, you can write to them but that will not change the Keyboard Maestro variables that they were created from).

Perl

snippet.perl
my $fileName = $ENV{KMVAR_File_Name};

Python

snippet.python
#!/usr/bin/env python
 
import os
print os.environ['KMVAR_File_Name']

See the Forum section below for more Python examples.

awk

Using shell variables in awk is also different than in most other shell languages. You need to pass the shell variable as parameter to the -v flag.

Some examples:

snippet.bash
awk -v awkVariable="$KMVAR_File_Name" 'BEGIN {print "The value of VAR is: " awkVariable}'

Passing Paths in Variables

If you are passing a file/folder path in a Keyboard Maestro Variable, then it is best to put the Variable reference in double quotes so that spaces in the path will work, like this:

snippet.bash
cat "$KMVAR_File_Path"

Consider These Guidelines

  • This must be a full path. If the path starts with a tilde (~), then you must first convert it to a full path using the Filter, Expand tilde (~) paths action before the Execute Shell Script action.
  • Like all Bash variables, the Keyboard Maestro Variable must not be between single quotes (') in order for the variable to be expanded to / replaced by its value. So, for example, this will not work:
snippet.bash
# 🚫 Does NOT Work
echo 'The variable is "$KMVAR_File_Name"'

because although the Variable is between double-quotes, it is in a string that is between single-quotes.

  • So, to handle this case, you can use something like this:
snippet.bash
# ✅ This WORKS
echo 'The variable is "'"$KMVAR_File_Name"'"'

One key Bash feature that makes this work is that when two quoted strings are adjacent, they will be concatenated. So after the Variable is expanded, the command string might look like this:

snippet.bash
echo 'The variable is "Test.txt"'

UTF-8 and Non-ASCII Characters

If you are dealing with non-ASCII characters, you probably want to set the LC_ALL environment variable to UTF8, which you can do by setting the Keyboard Maestro variable ENV_LC_ALL to “en_US.UTF-8”.

By default (v9.0+), if you have not set these environment variables they will be set to UTF-8 for you.

Action Gear

The action (gear) ⚙ menu includes the following options:

  • Trim Results — removes white space from the start and end of the scripts results.
  • Include Errors — include stdout and stderr results from the script.

Output of Results

The results of a shell script can be:

  • Ignored.
  • Displayed in a floating window.
  • Displayed briefly in a Notification.
  • Displayed large across the screen.
  • Typed in to the current selection.
  • Pasted in to the current selection.
  • Saved to a variable.
  • Appended to a variable (v9.0+).
  • Saved to the system or a Named Clipboard.
  • Asynchronously ignored — the action runs while the macro continues on.

If you want to return multiple values from a script, then you can either use AppleScript (via osascript) to set Keyboard Maestro variables.

Examples

For example, you could have a shell script date display briefly in the Notification Center every hour, or use a hot key to type the results directly into your text editor.

You can also use the clipboard by piping from pbpaste and to pbcopy.

Quoting Strings

Proper quoting of strings in a Shell Script is often a challenge. In order to understand it, you have to understand what happens at each level of the processing.

Thankfully, in an Execute Script action, Keyboard Maestro itself does not do any processing, so that is one less place for confusion.

With shell commands, the important thing to understand is that, with only a few exceptional programs, it is not the command that does the processing, it is the shell (bash usually, although there are many shells).

So for example with Keyboard Maestro variable set to "Selection Style" -int 0 and en Execute Shell Script command of:

snippet.bash
defaults write /Users/me/Library/Application\ Support/Witch/Settings $KMVAR_witchPref

It is the bash tool that sees the string and it processes it in to an array of strings which it passes to the defaults tool. This is important - the defaults command receives not a single string, but an array of strings, in this case the array will be:

  • defaults
  • write
  • /Users/me/Library/Application Support/Witch/Settings
  • "Selection
  • Style"
  • -int
  • 0

It is bash that has processed the variable substitution for $KMVAR_witchPref, split the line in to seven parts, processed the backslash in “Application\ Support” and then executed the defaults tool with the seven parts as arguments (the command itself is the 0th argument, normally mostly ignored by tools).

So the problem is that the word-breaking, variable substitution, de-quoting and de-backslash is all happening at as the line is processed by bash, after which you are left with quotes from within variable substitution, but they no longer have any meaning, they are just characters.

So variables that are meant to be a single parameter should be surrounded by double quotes in the line, but not contain quotes in the variable, and generally you do not want to pass multiple parameters within a single parameter. So instead, perhaps set one variable to the setting name, and one variable to the type and one variable to the value, and then use a command like this:

snippet.bash
defaults write "/Users/me/Library/Application Support/Witch/Settings" "$KMVAR_Setting" -"$KMVAR_Type" "$KMVAR_Value"

Now a further complication happens when you want to send a string containing double quotes and variables to a parameter. For example, perhaps you want a command like this:

snippet.bash
emacsclient -e '(w3m-browse-url "$KMVAR_SafariURL")'

This will not work because bash only expands variables within double-quoted strings. Within single quoted strings, they remain as the text $KMVAR_SafariURL (unchanged). And so emactsclient will receive the array:

  • emacsclient
  • -e
  • (w3m-browse-url "$KMVAR_SafariURL")

But it will not expand the variable (that is the shell’s job).

To work around this you need to use either all double quotes (and backslash any double quotes within them) such as:

snippet.bash
emacsclient -e "(w3m-browse-url \"$KMVAR_SafariURL\")"

or use a combination of strings, some double quoted and some single quoted:

snippet.bash
emacsclient -e '(w3m-browse-url "'"$KMVAR_SafariURL"'")'

Note that that parameter is now made up of three stings combined into one parameter:

  • (w3m-browse-url " - single quoted string
  • the value of $KMVAR_SafariURL - double quoted string
  • ") - single quoted string

For the reverse problem, where you have a variable that contains multiple parameters, note that you cannot use quotes within a variable - by the time the shell is expanding variables, it has already processed quotes and will not do so again so quotes will just be regular characters passed to the target command and almost certainly result in errors. If possible, use seperate variables for seperate parameters, but if you cannot do that (for example you have a list of paths), store them as seperate lines in a variable (without any quoting or backslashes) and use a command like:

snippet.bash
echo "$KMVAR_files" | tr '\r\n' '\0' | xargs -0 ls -l

The tr command will replace \r or \n line endings with a nul character, and the xargs -0 command will read that, split the arguments at the nul character, and pass them to the specified command (in this case ls -l. Note that xargs has a limit to the number of arguments it will pass, so for large numbers of arguments it may run the command multiple times with subsets of the arguments - read the xargs man page for more details.

Path in Shell Scripts

In essence, the default path in a Keyboard Maestro Execute Shell Script is the base path for the system:

/usr/bin:/bin:/usr/sbin:/sbin

That is the script will search for tools in the /usr/bin directory, then in the /bin directory, then /usr/sbin and finally /sbin. Only tools installed by the system will be in these directories - any tools you have installed will almost certainly be elsewhere and so not found by default because:

  • Shell scripts are executed in non-interactive mode (see the INVOCATION section of the sh man page), the only path preset by default is the system path.
  • Thus, your tools or files may not be automatically accessible.
  • The $PATH environment variable you may have set in Terminal is not used.
  • None of your profile scripts (like ~/.profile) will be executed.
  • Custom environment variable settings will not be applied, including any settings for tool-specific environment variables like PERL5LIB.

Generally, the best thing to do is to use full paths when referring to tools, such as /usr/local/bin/perl, however some tools may access sub-tools that they cannot find without the PATH (or other environment variables) being set.

If you need access to tools (executables) or files that you installed (not part of the system installation), you will need to do one of the following:

  • Use an explicit, full path to the tool or file. See Passing Paths in Variables in the above section. or
  • Set the $PATH environment variable within each Execute Shell Script action. or
  • Set ENV_PATH Keyboard Maestro variable prior to the Execute Shell Script Action.

Any Keyboard Maestro variable that starts with ENV_ will automatically be set as the as the corresponding environment variable (without the usual KMVAR_ prefix). So if you set your path in the ENV_PATH Keyboard Maestro variable, the PATH environment variable will be set from it.

You can set this Variable manually in the Variable Preferences Panel in Keyboard Maestro. Once the ENV_PATH variable is set, it will remain in your Keyboard Maestro variable until/unless you delete it, and remain available to every Execute a Shell Script action that you might use in the future. So you don’t need to set it in every Macro.

If your tool requires other environment variables to be set you can set them as well by creating an appropriate Keyboard Maestro variable with ENV_ at the front.

For example, if you want to use Perl with a custom library search path in the Execute a Shell Script action, you can also create a Keyboard Maestro variable ENV_PERL5LIB that will set the PERL5LIB environment variable.

Working Directory

The working directory for any executed scripts will be set from the Keyboard Maestro ENV_PWD directory (v10.0+), or default to the root directory (/).

Execute Script From Other Apps

When you execute a shell script from other apps, like Terminal.app, the shell does not have access to the Keyboard Maestro Engine environment, and thus it does not know anything about Keyboard Maestro Variables.

In order to access Keyboard Maestro Variables in these scripts, you must use a tool like osascript.

Here is a simple example to get a variable:

osascript -e 'tell application "Keyboard Maestro Engine" to getvariable "My KM Var"'

⚠️ Note that you do not replace spaces with underscores in your variable name in this case, since you are ultimately using AppleScript in the Shell Script.

To set a variable (and create if needed) you can use this:

osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "My KM Var" to "Some new value"'

Here is a more comprehensive example, providing error handling:

read -r -d '' theAppleScript <<'EOF'
   tell application "Keyboard Maestro Engine"
      set kmVarName to "My KM Var"
      tell variable kmVarName
         if it exists then
            return its value
         else
            return "Error → Keyboard Maestro variable '" & kmVarName & "' does not exist!"
         end if
      end tell
   end tell
EOF

osascript -e "$theAppleScript";

See Also

Actions

Forum

action/Execute_a_Shell_Script.1634543700.txt.gz · Last modified: 2021/10/18 03:55 by peternlewis