Execute a Script in Terminal

terminal
shellscript

#1

This action is similar to the default “Execute a Shell Script” action. The main difference is that the script will be executed in the Terminal application.

Example:

##When would you use this?

  • Whenever you need the visual output of the Terminal
  • For interactive scripts
  • When the script demands a password, which you don’t want to send through KM

Installation

Drop the zip archive on the Keyboard Maestro application dock icon. (Wiki: Plug-in Actions)

Execute a Script in Terminal.zip (11.6 KB)

##Options

###New Terminal window

Executes the script in a new Terminal window. If unchecked the script will be executed in an already open (non-busy) Terminal window.

Bring Terminal to foreground

When executing the script the Terminal application will become frontmost.

Other

You can also execute script files. You must format the path as you would do in the Terminal, that is, quoting the path or escaping any spaces.

The action will not capture any script output.


How Do I Execute a Shell Script in Interactive mode?
Copy password from pass (The Standard Unix Password Manager)
#2

Update (1.0.1)

If the “New Terminal window” option is not selected:

The action now also checks for “[Process completed]” windows and will treat them as busy windows. (As before, if all open windows are busy the script will be executed in a new Terminal window, independently of if the option is selected.)

Execute a Script in Terminal 1.0.1.zip (12.0 KB)


To install the new version delete the old plugin from ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions/, then drag the zip file to the KM Editor icon in the dock.

You may have to reload KM as described here.


How to install Plug-In Actions in Keyboard Maestro 5.3.2?
#3

Thank you!

Can you include stored KM variables in the script box for what you’re passing off to Terminal to execute?


#4

No, the script will presumably inherit the environment of the Terminal and the Terminal window, not the AppleScript, so it will not have the Keyboard Maestro variables.

If you wish to use the Keyboard Maestro variables, you will have to read them from the Keyboard Maestro Engine using an AppleScript (via osascript) within the script.

Or failing that, pass the variables by some other mechanism (eg, the clipboard or a file).


#5

OK, here is a new version:

Execute a Script in Terminal-v200.zip (15.6 KB)

You can paste your KM variables to the second text field (“KM variables”). To use them in your script call them with $km1, $km2, etc. in the order they are listed in the Variables field.

Example:

…produces this output:



Treat this as a beta version! I haven’t done much testing.

Before using it with file-system-changing commands like rm or mv please test it with something harmless like echo or ls !

Notes:

  • If you are using the action without any variables you can leave the explanatory text in the field. It will be ignored.
  • Empty lines in the Variables field or trailing newlines will be ignored.
  • Empty or undefined variables will not be ignored, they will become empty strings in the shell script.
  • If you prefer to see an alert if a variable is empty/undefined, this can easily be implemented. But I think in general it is good to have the possibility to run the action also with intentionally empty variables.

How Do I Execute a Shell Script in Interactive mode?
How to install Plug-In Actions in Keyboard Maestro 5.3.2?
#6

[2017-12-06] Update 2.1.0 for KM8+

  • Now supports also local and instance variables as introduced with KM8
  • Download is a zipped, code-signed DMG, containing further installation hints
  • BTW, the new variable mechanism, as introduced here, seems to work fine. So, I would consider it Safe for Work. I think.

Execute a Script in Terminal-210.dmg.zip (100.3 KB)

@peternlewis:

Any chance to get this action (or preferably someting better) into a future release of KM? I’m seeing myself using this action many times.


How to create new folder with a custom name?
#7

I will add it to the list (well, it is probably already there). I doubt it would happen any time soon.


How to create new folder with a custom name?