Execute a Script in Terminal

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.

7 Likes

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.

2 Likes

Thank you!

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

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).

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.
4 Likes

[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.

1 Like

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

2 Likes

I am having some issues drag-dropping the zip into my Keyboard Maestro dock icon. When I try that, nothing happens. I do not see this plugin anywhere. I tried restarting the Keyboard Maestro app as well, but no luck. Is there something that I must do before drag-dropping?

Check if it gets expanded into ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions. That’s where it should be, unless @peternlewis changed something recently.

Just to be sure, restart also KM Engine. You can do this from the File menu within the KM app. (Last entry, “Quit Engine” / “Launch Engine”).

Check if it gets expanded into ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions .

My bad that I hadn't noted this earlier. I had a look there before posting, no, it is not visible on that path.

restart also KM Engine

This is something I tried as well, but no luck. This is actually a new Mac with a new installation of KM, and the old device works fine. I have sync enabled between the two, via Syncthing. Not sure if that might affect it.

Not sure either. But, since everything works on your other computer, this might be something.

I’m using KM’s own sync (Prefs > General) via iCloud. With this enabled, KM syncs a special “Keyboard Maestro Macros.sync” file. But as the name suggests, plugins may not be included with that sync either. I’m not sure about that; currently the plugins on my two Macs are in sync, but this can well be because they might have been installed on both machines. (Rarely that I install new plugins; I have only 4 in total, where two are obsolete…)

But: Are we speaking of a syncing issue, or did you have tried to install (zip-drop) the plugin on the other machine directly, and it didn’t work either?

The sync issue could be unrelated, as my understanding is the same as you -- third-party plugins cannot be synced.

Drag-dropping not working is the core issue. I want to try removing this plugin from working computer, and try adding it again, but I am a bit worried that I might lose it on that computer as well. :slightly_smiling_face: Will continue investigating, will followup if I know more.

Well, I think you can’t lose anything because instead of expanding the Zip by dropping it on the KM Dock icon, you can always expand it “manually” into the path. Or did you try that and the plugin was not recognized?

This is completely my bad, I was trying to import the .zip that I downloaded from this forum post.

But I must,

  • Unzip the archive first.
  • Run the .dmg
  • And then, import the archive it shows.

Doing so worked. :white_check_mark: :tada:

Great :slight_smile:

I guess, it was my bad too, since I should have checked the structure of my archive and tell you to drop the Zip from the Dmg and not the “outer” Zip. Sorry.

I think I enclosed the Dmg in a Zip because the forum doesn’t accept Dmg uploads (at least back then when I uploaded it).

PS:

I noticed that the only hint that you have to mount the Dmg first, is a tiny line in my 2.1-update post that can easily be missed.

So, for other users, here in bold:

Download version 2.1 of the plugin, unzip it, mount the Dmg and follow the instructions there.

2 Likes