Triggering a macro (with args) from command line?

I do a lot of bash command line scripting, and find myself wanting to trigger a KM macro from the command line. Is there a way to trigger a macro from bash?

An ideal command line tool might look like this

kmtrigger --variable varOne=value1 --variable varTwo=value2 macroName

(Yes: I know that plenty of command line things can be done from KM; I’m looking to do some KM things from the command line. And yes: I know I could use something like osascript to run an AppleScript blob which in turn kicks KM, but that’s a level of indirection I’d like to avoid, even ignoring my long-term allergy to AppleScript.)

Thanks,
Dan

Hey Dan,

You’re pretty much stuck with osascript.

The only exception I can think of using open with a KM-URL, and I’m not sure you can wrangle parameters outside the URL.

-Chris

Thanks, Chris. AppleScript it is.

Dan

Chris and @dtgriscom,

Yep, you can pass a parameter in the KM Public URL:

http://127.0.0.1:4490/action.html?macro=<YourMacroUUID>&value=<YourParameter>

where

  • <YourMacroUUID> is the UUID of the macro to execute
  • <YourParameter> is just that, but MUST be URL encoded.

In the Macro that is executed, you can get the parameter using the %TriggerValue% token.

EDIT: I forgot to add the trigger for the macro to be executed:

Communicating between applications is pretty much always AppleScript under the hood anyway. You can use URLs, and there are some other ways, but generally you use AppleScripts (or AppleEvents anyway) for communicating.

Here is a perl script that handles command like this:

kmtrigger.pl var=value "My Var=My Value" "The Macro?Trigger Value"

And sets variables or triggers macros.

kmtrigger.pl (1.2 KB)

1 Like

or JavaScript for Automation (JXA).

My head is hurting enough working out all the nuances of JXA, without even thinking about perl.

Yes, technically it is OSA over AppleEvents, which can be done via AppleScript or JXA (or raw AppleEvents).

@peternlewis This made my day, and it was a pleasant surprise that the Perl code worked great on Ventura nearly 6 years later. Well done!

2 Likes