Alexis Posted May 31, 2021 Report Share Posted May 31, 2021 Is it possible to create a variable from a prompt? What i'm trying to do is, when I run the Macro I get prompted for a string, let's say I type in "chrome", then the variable %chrome% gets created and contains the window title of the topmost window. Something like this: <VARIABLE SET STRING Option="\x01" Destination="%prompt%" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0"/> <VARIABLE SET STRING Option="\x05" Destination="%%prompt%%"/> When I tried to use the viariable %%prompt%% (which would be %chrome% in this example) I get the variable %%prompt%% is the wrong type error. So, is it even possible that MEX is able to create variables that haven't been specified beforehand? Quote Link to comment Share on other sites More sharing options...
acantor Posted May 31, 2021 Report Share Posted May 31, 2021 You will need to define every variable you plan to input into the script. You can define dozens or hundreds of variables, if you want. All of those variables shouldn't impact performance significantly. This script accepts variables "a", "b", and "chrome" only: Variable Set String %a% to "" Variable Set String %b% to "" Variable Set String %chrome% to "" Variable Set String %VariableName%: Prompt If Variable %VariableName% Equals "a" OR If Variable %VariableName% Equals "b" OR If Variable %VariableName% Equals "chrome" Variable Set String %%VariableName%% to topmost window title Text Box Display: Values Else MessageBox: Can't use this as a variable! End If <VARIABLE SET STRING Option="\x00" Destination="%a%" NoEmbeddedVars="FALSE"/> <VARIABLE SET STRING Option="\x00" Destination="%b%" NoEmbeddedVars="FALSE"/> <VARIABLE SET STRING Option="\x00" Destination="%chrome%" NoEmbeddedVars="FALSE"/> <COMMENT/> <VARIABLE SET STRING Option="\x01" Destination="%VariableName%" Prompt="What is the name of the variable?" Mask="FALSE" OnTop="TRUE" Left="Center" Top="Center" Monitor="0"/> <COMMENT/> <IF VARIABLE Variable="%VariableName%" Condition="\x00" Value="a" IgnoreCase="TRUE"/> <OR/> <IF VARIABLE Variable="%VariableName%" Condition="\x00" Value="b" IgnoreCase="TRUE"/> <OR/> <IF VARIABLE Variable="%VariableName%" Condition="\x00" Value="chrome" IgnoreCase="TRUE"/> <VARIABLE SET STRING Option="\x05" Destination="%%VariableName%%"/> <TEXT BOX DISPLAY Title="Values" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\lang4105\\f0\\fs20 VariableName = %VariableName%\r\n\\par \r\n\\par %a%\r\n\\par %b%\r\n\\par %chrome%\\lang1033\\f1\\fs14 \r\n\\par }\r\n" Left="821" Top="Center" Width="871" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/> <ELSE/> <MESSAGEBOX Caption="Can't use this as a variable!" Message="You must specify \"a,\" \"b,\" or \"chrome,\" not \"%VariableName%\" " Icon="4"/> <END IF/> Quote Link to comment Share on other sites More sharing options...
Alexis Posted June 7, 2021 Author Report Share Posted June 7, 2021 Thanks for the answer and the example. Much appreciated, though I was hoping that MEX could create Variables "on the fly". Quote Link to comment Share on other sites More sharing options...
acantor Posted June 7, 2021 Report Share Posted June 7, 2021 Although I don't know a straightforward way to create Macro Express variables on-the-fly, that doesn't mean it's impossible. Many years ago, I developed a Macro Express script that generated other Macro Express scripts. I wonder whether the approach could be applied to the problem of generating variables. The script worked by sending sequences of keystrokes and mouse clicks to the Macro Express user interface. After a lot of tweaking, the script was nearly 100% reliable, although while running, the script felt brittle. Quote Link to comment Share on other sites More sharing options...
rberq Posted June 7, 2021 Report Share Posted June 7, 2021 2 hours ago, acantor said: Although I don't know a straightforward way to create Macro Express variables on-the-fly, that doesn't mean it's impossible. Many years ago, I developed a Macro Express script that generated other Macro Express scripts. I wonder whether the approach could be applied to the problem of generating variables. I was thinking of something similar. Maybe open the script in the Direct Editor, copy all to the clipboard, manipulate whatever needs to be manipulated to add a variable, then paste the modified content back into the Direct Editor, overlaying the original macro. Seems like a good way to get into trouble .... I have been trying to figure out why anyone would WANT to create variables on the fly. Perhaps to set up the "data" environment all at once, before starting to write the procedural logic for a macro. You could make a spreadsheet or text file with variable names and attributes, then read the file with a macro and make the variables all at once. Labor saver??? Or not? Quote Link to comment Share on other sites More sharing options...
acantor Posted June 7, 2021 Report Share Posted June 7, 2021 I agree. The potential savings of a macro that creates variables on the fly needs to be balanced against the time and effort needed to make it work. I can imagine spectacular failures along the way. Although it could be a fascinating project to test one's mettle, it's not something Macro Express was designed to do. But there is another possibility. Under "Preferences," "General" tab, "Dialogs." there is this option: "Prompt the user to create a variable that is not defined." When unchecked, the variables that one creates in the Script Editor are (sort of) generated on the fly. I disabled this setting ages ago, and haven't missed it. For general-purpose scripting, I find I rarely have reason to do anything in the "Variables" tab in the Script Editor. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.