Execute a Subroutine From JavaScript Passing Parameters

Hi!
I have a subroutine that accepts three parameters, and I need to execute it from outside KM.
I know that I can execute a macro with a single parameter like this

var app = Application('Keyboard Maestro Engine')
app.doScript("45FDB98C-69C7-4820-AC02-C44A3762E750", { withParameter: "Whatever" });

but I couldn't figure out a way to do the same with multiple parameters, if there's a way at all.

Thanks,
Francesco

You can only send one parameter, so munge your 3 into 1 then either a) decompose that back to 3 in the macro or b) have a "decomposer" macro that you target with the app.doScript which splits out the 3 parameters then executes the macro proper.

(You could also set 3 global vars and have your macro check "if params empty then use globals" but... yuck!)

You can pass the parameter in the form of a JSON string, and parse that to a JS object with three keys inside the macro.

(() => {
    "use strict";

    function myFunction(a, b, c) {
        return c ? (
            [a, b]
        ) : [b, a];
    }

    const jsonString = JSON.stringify({
        "alpha": 1,
        "beta": "someString",
        "gamma": false
    });

    const parsed = JSON.parse(jsonString);

    const args = [parsed.alpha, parsed.beta, parsed.gamma];

    return myFunction(...args);

})();
3 Likes

Thanks Nige_S,
that was my backup plan :wink:
It's a bummer though, because accepting more than one parameter from outside would be much more handy.
Hope that can change in the future.

Thanks,
Francesco

I like this! Thank you!

Francesco

1 Like