Execute a Macro with the Macro Name as a Variable

Is there anyway to use the Execute Macro action but have a variable for the name of the macro to be run?

I want to set up an array and if a condition exists run a macro with that name. For example:
myArray(Apple, Banana, Cherry).

I want to test if files with those names exist. If not I want to run a macro with that name. Here is the pseudo code:
For i = 0 to Ubound(myArray)
fileName = "~Documents" & myArray(i) & ".txt"
If file fileName does not exist then
Execute Macro myArray(i)
End if

This may give you some idea.

Once you have passed the macro name (in my example, "Display Text") to the variable, then you can do:

tell application "Keyboard Maestro Engine"
do script macroName

Here is the code in the example:

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set macroName to getvariable "Local__macroName" instance kmInst
	do script macroName
end tell

This is to get Local or Instance variables. To get global variable value, use the method given on the wiki page.


That same script will work with Global variables.

1 Like