How Do I Create a Macro That Can Be Called From Another Macro as Function

I want to create a macros A that opens a webpage in chrome (open new tab, enter url, press enter, wait till finish loading). In macros B I want to use the macros A but with the url provided by the macros B.

Macros B, for example:

some script
set url=google.com
call macrosB(url)
some script

Is there a way to do something like that?

Thank you

1 Like

Sure. Macro A would be what we usually call a Sub-Macro (it's just a term we use, but most people know what it means).

First of all, variables are global. so any variable you set in Macro B will be available in Macro A.

But many of us who do this regularly use a different method. When you use the "Execute Macro" action, click the "gear" icon and you'll find an option called "with parameter". Put the parameter you want to pass here:

Macro A can refer to the passed parameter via %TriggerValue%, like this:

2 Likes

Thank you Dan!

1 Like

Hey Dan, just a question. But does this essentially mean that you can only pass in one parameter/argument into a macro?

Or with the JSON upgrade, is it now possible to pass in a map?

I'm not Dan, I'm not even mini-Dan, but there is a page in the wiki that I've been reading lately that lets use separate strings as separate parameters into a macro:

https://wiki.keyboardmaestro.com/token/TriggerValue

...which says:

You can easily access the individual parts using the token array notation, eg %TriggerValue[3]% will be the third comma-separated value, and %TriggerValue[3];% will be the third semicolon-separated value, and %TriggerValue[3] % (note the space between the ] and the % ) will be the third space-separated value (8.0.4+).

I've never tried passing a dictionary as a parameter (which is what I think you mean by "map".) Interesting idea. So I think we can all agree that a JSON is just a string so we know can pass one (if it's a constant) to a macro like this:

And that might be good enough for your purposes.

But a deeper question is whether we can pass a JSON to a macro as variable name rather than a constant. I couldn't see how to do it (maybe someone else can) but I was able to trick it by doing this:

And that worked, as long as the dictionary that I wanted to pass as a parameter was a dictionary called "a" inside a dictionary called "MyDictionary." That opens up interesting new possibilities for me! I never knew you could do this! This is huge! Thanks for making me think about this.

The only thing is that this is still passing the information by value, not by name, so the macro can't modify the data in the dictionary. However this could be fixed by passing the name of the dictionary and its sub-dictionary as additional parameters, like this:

I think I could get that to work.

Passing a variable "by name" might also interest you and was discussed two years ago here:

1 Like

Cool, thank you!