Jump to content
Macro Express Forums

Variable set in one macro that is evaluated by another


Recommended Posts

I have not quite figured out how to script a macro that returns a value to another macro.

 

For example, here is a macro nicknamed "Black_Test" that tests if pixel colour = 0:

 

Get Pixel Color from Beneath the Mouse into %PixelColour%
If Variable %PixelColour% Equals "0"
 Variable Set Bool %IsBlackPixel% to "True"
Else
 Variable Set Bool %IsBlackPixel% to "False"
End If

 

Other macros call "Black_Test" to obtain the value of %IsBlackPixel%:

 

Macro Run: Black_Test
If %IsBlackPixel% = "True"
  // Do this
Else
  // Do that
End If

 

My assumption has been that %IsBlackPixel% should be defined as "Global" in "Black_Test." In the macros that run "Black_Test," there should be no need to make "IsBlackPixel" global. Is this correct? Probably not, because sometimes I can make it work, sometimes not.

 

I have tried adding commands to save and restore variables (see below), but it's still not reliable:

 

Get Pixel Color from Beneath the Mouse into %PixelColour%
If Variable %PixelColour% Equals "0"
 Variable Set Bool %IsBlackPixel% to "True"
Else
 Variable Set Bool %IsBlackPixel% to "False"
End If
Variable Save: Save Boolean Variables // COMMAND ADDED

 

Macro Run: Black_Test
Variable Restore: Restore Boolean Variables  // COMMAND ADDED
If %IsBlackPixel% = "True"
  // Do this
Else
  // Do that
End If

 

I must be missing something. I look forward to learning a solution on this seemingly simple yet perplexing problem! Or a better approach.

Link to comment
Share on other sites

The problem here is that MEP has various bugs to do with not defining variables properly; I've reported several (e.g. Run Macro in variable fails to recognize any user-defined variables), but none of the problems has been fixed, and I haven't received any feedback from Insight for quite a long time.

 

Your problem is to do with boolean variables. Take a look at your variables in any macro that calls Black_Test, and you'll see that the variable %IsBlackPixel% is defined as a text variable. You'll have to change that manually, after which your logic should work.

 

Text and Integer variables don't usually have these problems, but I'm not guaranteeing that!

Link to comment
Share on other sites

Text and Integer variables don't usually have these problems, but I'm not guaranteeing that!

 

Thank you, Paul, I will check. But your suggestion suggests another solution. If Boolean variables are buggy, maybe I should use Text variables. Why not set values of text variable to either True or False? In other words, create ersatz Boolean variables using Text variables.

Link to comment
Share on other sites

IMHO one should never use variable save or restore.

 

Click here to see my write-up about the nature of global and local variables. Personally when I create a new macro I make all my variables local by default to avoid possible conflicts later with called macros. This way when I need to use a subroutine macro I wrote months ago I don't have to worry about clashes.

Link to comment
Share on other sites

IMHO one should never use variable save or restore.

 

Hi Cory,

 

Is there a way to get around using "Save" and "Restore" in cases when a macro evaluates a condition, and another script makes decisions at a later time, when it is no longer possible to check the condition?

 

Which leads to a question.

 

If I save a MEP variable, and then shut down my computer, will MEP be able to restore the value after I reboot?

Link to comment
Share on other sites

IMHO one should never use variable save or restore.

For the sake of those who are relative newcomers to Macro Express, I think you should modify this statement to reflect the fact that use of the Variable Restore command is mandatory when you invoke a macro with MeProc.exe and wish to pass in parameters, as in Meproc /AMyMacro /VMyString:Now is the time. This runs a macro called MyMacro, placing "Now is the time" in a text variable called MyString.

Link to comment
Share on other sites

  • 2 weeks later...

Sorry I’ve been away. I was building a new server for myself as well as some other projects I needed ot get done ASAP.

 

I’ve mentioned this a time or two in other posts but it’s worth mentioning again. I use the registry to save variable values for later use. For instance I have a Fax Monitor macro and part of that is knowing who is the designated monitor at any point in time. I have a second macro that changes a registry value on the local machine which ‘enables’ the macro under this user’s profile. I simply save this in the HKCU hive of the registry and now when the primary macro runs it launches and checks to see if it should abort of continue. The next step illustrates another place to store variables and that’s in INI files. I have a third macro which simply presents a list of users and any user can select one of these to designate them as the current fax monitor. But a local registry would not be wise so instead I save it to an INI file, with dozens of other settings, on the file server. Now when the primary macro runs and it finds that it is enabled in the registry it looks to the INI file to see if it’s user name matches the designated fax monitor. Anyway that’s the way I like to do it.

Link to comment
Share on other sites

For the sake of those who are relative newcomers to Macro Express, I think you should modify this statement to reflect the fact that use of the Variable Restore command is mandatory when you invoke a macro with MeProc.exe and wish to pass in parameters, as in Meproc /AMyMacro /VMyString:Now is the time. This runs a macro called MyMacro, placing "Now is the time" in a text variable called MyString.

Well... I suppose you could but I never pass variables that way. I would again rather use the registry or INI to pass the value.

I know you love Variable Save/Restore Paul but it is buggy as hell and holds a whole host of horrors. I can’t recommend it to anyone. It’s only included in MEP for reverse compatibility and should only be used in that case. Also I don’t remember him mentioning anything about using MEProc.exe in his post so I don’t’ think it’s relevant.

Link to comment
Share on other sites

Hi Cory,

 

Is there a way to get around using "Save" and "Restore" in cases when a macro evaluates a condition, and another script makes decisions at a later time, when it is no longer possible to check the condition?

 

Which leads to a question.

 

If I save a MEP variable, and then shut down my computer, will MEP be able to restore the value after I reboot?

 

 

Old time programmer and brand new to MEP but I have a thought on this. I see MEP has the ability to read a file. To bridge a computer shutdown or even between two macros, one can use a file as depot for variables. As part of the shutdown of a macro it writes out values to a file, and on its startup it reads them in as the override of default values. This is basicially how the xxx.ini functionality of windows bascially works, no reason not to follow that paradigm. In the past I've used this for inter-process communcation (read parameter passing) from one process to another.

 

The only real issue in accomplishing this is what facilities are available in MEP for string parsing, but I will have to defer that answer to those who know MEP better. I imagine I'll be in a similar need in a short timeframe myself...

 

- Jleslie48

Link to comment
Share on other sites

I've no idea where you got this idea from! It's untrue, and you'll not find me using this anywhere except for macros called with MeProc plus parameters.

I just seems every time this comes up you mention save/restore. Sorry I must have missed his meniton of using MEProc.
Link to comment
Share on other sites

This discussion leads to an interesting question. When is it appropriate to use the Save/Restore for variables? I have used these commands successfully, and although they may be limited and/or buggy, they are, in certain circumstances, the path of least resistance.

 

Save/Restore works perfectly for me when I use Integer or Text variables. Generally, I am flagging whether a condition was met, and then checking the flag with another macro.

 

I wish I could use Boolean variables in this way, but apparently, they do not work when set in one macro, and read by another.

Link to comment
Share on other sites

This discussion leads to an interesting question. When is it appropriate to use the Save/Restore for variables? I have used these commands successfully, and although they may be limited and/or buggy, they are, in certain circumstances, the path of least resistance.

Never (except when using MeProc with parameters).

Save/Restore works perfectly for me when I use Integer or Text variables. Generally, I am flagging whether a condition was met, and then checking the flag with another macro.

 

I wish I could use Boolean variables in this way, but apparently, they do not work when set in one macro, and read by another.

Boolean variables work exactly in the same way as any other variable. The trick is specifically to create the boolean variable as Boolean in the macro that needs to access a boolean value.

For example, Mac1 runs Mac2, then checks if boolean bTrueFalse = True (Mac2 sets bTrueFalse to True or False). Create bTrueFalse as a boolean variable in Mac1, and you'll find this all works.

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.

Guest
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.

Loading...
×
×
  • Create New...