What's the best way to send info to a macro from a shell script?

I would like to be able to send a piece of information (a single keyword) to a Keyboard Maestro macro from a shell script.

Sort of like an “argument” I guess.

What‘s the best way of doing this? The only way I could think of was to create a file with just the word in it and then have the macro start off by reading that file into a Keyboard Maestro variable, but I was wondering if there was a better way.

UPDATE: Oh! I think it might be "with parameter" but I'm still trying to figure out exactly how it might work.

You can set a KM variable from a shell script:

1 Like

Yes, it is the “with parameter” facility of the AppleScript. Of course, getting the parameter from your shell script into the AppleScript (which you execute via osascript) requires careful quoting, so it is not entirely without challenges - a good example would be useful.

Here is a bash script that will take its first argument as a macro UID (or macro name, but you'll need to quote it it it has spaces), and the remainder of the arguments (space separated) as the parameter:

#!/bin/bash

MACRO=$1
MACRO=${MACRO//\"/\\\"}
PARAMS="${*:2}"
PARAMS=${PARAMS//\"/\\\"}

osascript -e 'tell application "Keyboard Maestro Engine" to do script "'"$MACRO"'" with parameter "'"$PARAMS"'"'

For example, if you save that script as kmdo.sh

kmdo.sh 088520CE-CA97-4082-A073-8D54A08FABCD "wombat\" stew" 'is "nice"'

Cool! I have it easy: in my current usage, I will always be calling the same macro, and the argument / parameter would be one-word-no-spaces.

Unfortunately it seems a lot slower (I assume because if AppleScript) than the alternative (which isn't as nice as it would be with Keyboard Maestro’s help.

Thanks for the reply!