'Execute Macro' by Name in a Variable

A few of my macros conditionally execute other macros based on the current state of a variable like 'ThingToDo' below. Up until now, I've been adding a new IF ELSE for each macro possibility with an 'Execute Macro' for each 'ThingToDo':

The problem is that I need to manually select the macro that I want to run for every case. This means if I add a new case then I need to update the macro to accommodate the new 'ThingToDo'.

How can I execute a macro explicitly named in a variable?

NOTE: I realize that if the macro name changes it would not run, but this is a naming convention that I'll be enforcing strictly.

1 Like

Instead of the built-in Execute a Macro action, you can run a macro via AppleScript. As an example, this macro:


tell application "Keyboard Maestro Engine"
	set MacroToRun to getvariable "ThingToDo"
	do script MacroToRun
end tell

executes this macro:


which produces this result:


With this method, you could set the variable naming the macro-to-be-run however you like, then either replace your current "IF ELSE" based macro with one consisting of this single Execute an AppleScript action and run it as a sub-routine with an Execute Macro action, or just add the AppleScript action in an appropriate location for each macro that sets the variable it uses.


I'd like to do the same thing, but calling a dynamically named subroutine with results being passed in (e.g., var1, var2) and back (varResult). I assume "do script " works the same, but how do I specify the parameter passing in Applescript? Thanks!

Since you re talking about subroutines, not macros, that's an interesting question. I recommend that you post this question in its owns separate thread. I look forward to finding the answer with you (if there is one.)

Will do!

Question move to this thread: