Jump to content
Macro Express Forums

calling macros using "Run Macro" command

Recommended Posts

I've been trying to figure out ways to address a scenario which I've been unsuccessful in solving...

I would like to utilize the "Wait for this macro to terminate before proceeding" function in the Run Macro command coupled with the ability to share global variables between the main macro and the called macro.

I wonder if someone here has a solution or some good alternatives.


The scenario:

A main script will be initiated, but will call another within its sequence to complete;

there are over 100 macros which can be called;

which of those macros to call cannot be previously determined(can only be determined at run time).


To this point, I have been using short keys to call one of the macro's, as I've used short key association with keywords determined in the main script.

example: all 100+ macro's to be called have short key assignments like 001 thru 100+, the main macro gathers the data it needs from a source to determine which of those to call, and then use the Type command to initiate the short key for that macro.

This method works, however, the main macro runs thru before the called macro completes. I've solved this with a few Wait For type delays; but I'd like a more accurate way of determining when the called macro is completed prior to the main macro moving on and the ability to share variables.


I haven't found a way to use the Run Macro command to call one of those macro's by name because there is not a way to know what the name of the script would be until run time. More specifically, I haven't found a way to make an association with short key 001 and the name of the macro.


I noticed that the Run Macro command has a variable option for the Macro Nickname so I've thought about creating a list of the macro's within a sequence to assign a variable to; but I cannot locate a command which would allow this.


Does anyone know of a way to associate a macro name with a variable? Am I over thinking this? Run Macro in Variable?


Forgive me if the above is unclear. I can re-create it in another way if necessary.

Link to comment
Share on other sites

"Run Macro in Variable" looks like a good way to initiate the called macro. Sorry, I only have ME3 installed so I can't check, but does that command have a wait-for-completion option?


If not, you could use a global variable to signal completion:

(1) Main macro sets DoneProcessing variable to zero, then initiates called macro.

(2) Main macro loops (Repeat loop) checking DoneProcessing variable for value of one rather than zero.

(3) Called macro does its thing, sets DoneProcessing to value one, exits.

(4) Main macro sees DoneProcessing value of one, exits from Repeat loop and continues processing.


Obviously, you can name your global variable anything you want -- DoneProcessing is not a keyword within Macro Express as far as I know.

If you use this technique, be sure to put a brief delay within the Repeat loop so it doesn't hog all the processor time -- one-tenth second (100ms) usually works well. Also put some error logic within the Repeat loop -- for example, if it doesn't see the DoneProcessing value within 5 seconds (50 repeats with 100ms delays), then display an abort message and halt the main macro. This allows for a called macro that fails or "forgets" to set the DoneProcessing value.

Link to comment
Share on other sites

If a macro executes from an activation it is operating independently and the variable pool will not be shared. This feature only applies to the Macro Run command. As rberg described it gets ugly trying to do what you want. I would not recommend it.

Link to comment
Share on other sites

If I understand correctly, your macro could calculate the name of the macro to run, put it in a variable and then do a Macro Run using that variable. This works for me:

Variable Set String %MacroToRun% to "Test OSVersion Windows 10"
Macro Run: %MacroToRun%

Link to comment
Share on other sites

Thank you all for the replies. It seems that I may have muddied the problem a bit trying to explain it. I noticed that I had "Run Macro" as opposed to "Macro Run" as the command which I already knew I would like to use as it contains the characteristics I require. rberq's solution would work if global variables can be reliably passed between the macro's without using the Macro Run command. I've tried the Variable Save, Variable Restore, and the Clear Variables commands to pass variables but with varied results as disposing of the variables didn't occur reliably.


Samara's solution would not work since the macro to run is pre-assigned.


The problem is that I cannot call a macro by Name because I don't know which macro to call until run time.


Sounds more and more like I'll need to make a giant list of switch/case statements to be able to assign a variable to each of the macro's.


Thank you guys for your time.

Link to comment
Share on other sites

If the problem is passing information among the macros, and variables aren't quite working, here are a couple possibilities:


(1) Create registry key(s), write the data into the keys, read it back in other macros, delete the registry key when done.


(2) Maybe simpler: create a temporary text file, write data into it, read back in other macros, delete when done (actually, delete only when you need to rewrite it with new data -- who cares if an itty-bitty file stays there in a temp folder?). Using files sounds slow and inefficient, but it is plenty fast enough if your macros are assisting a manual process. You won't even have time to blink while the file is written/read.

Link to comment
Share on other sites

... the main macro gathers the data it needs from a source to determine which of those to call, and then ...

The problem is that I cannot call a macro by Name because I don't know which macro to call until run time

If your macro can determine "which of those to call" at "run time" then the technique I suggested should work.

If Variable %Data% Equals "Something1"
  Variable Set String %MacroToRun% to "Macro001"
End If
If Variable %Data% Equals "Something2"
  Variable Set String %MacroToRun% to "Macro002"
  If Variable %Data% Equals "Something3"
    Variable Set String %MacroToRun% to "Macro101"
  End If
End If
Macro Run: %MacroToRun%

However, rberg's suggestions about how to save variable data will also work. I would add that you can use .INI files as well. Depending on what type of information your variables contain, INI files may make the process a little easier.


This article suggests several ways to save and restore variables: How do Local and Global variables work and what is Variable Scope?

Link to comment
Share on other sites

I would replace the Macro Run commands by copy/pasting the body of the run macros into the calling macro. I only use Macro Run for building and testing. If you still want the shortkey activation availabe, don't delete the original macros.

Link to comment
Share on other sites

Gaaaaccckkkk!!! :o


It's really a pretty easy way, you just have to comment the code using the remarks command between sections, and it doesn't have to be done all at one time, the other solutions offered seem like they'll take just as much work plus the added bonus of debugging.


The end result is a single, easily portable macro, I always approach macro writing as if I was going to distribute it, guess I love self abuse! I'm a 3x user btw, can't really help with pro specific solutions.

Link to comment
Share on other sites

Ended up creating an additional macro to call which contains a series of switch/case statements to represent each of the macro's to call.

This way, I can take advantage of the built in sharing of variables across macro's, have no timing issues, and jump to the end past conditional statements which don't apply once a match is found so it's fairly efficient.

Case: 12345
  Macro Run: "macroName1"
End Case 
Case: 54321
  Macro Run: "macroName2"
End Case
End Switch

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...