Declaring and setting global variables

I am having trouble using global variables. I have 2 scripts. In Script1, I declare and set the variable using:

global testVar
put "testing" into testVar
Script2

Then in Script2, I use the variable:

put testVar

(also tried “TypeText testVar”)

…but the literal string of “testVar” is entered.

If I move the put command that sets the value (put “testing” into testVar) into Script2, it still doesn’t work.

If I use (TypeText testVar) then it works. But then it’s not a global variable.

What am I missing here to be able to declare and set a global variable in one script and then use it in another?

You can declare the variable to be global in script 2 as well, or you can just refer to “global testvar” when you want to use it in the second script. Here is an example:

Script 1:

global somevar

put "me" into somevar

script2

Script 2:

global somevar

put "somevar is" && somevar

or just

put "somevar is" && global somevar

Cool. I see how that works. Thanks.

One difference though… I don’t want to add anything to the variable within Script2. I want the variable to be set and changed only in Script1. I want to do something like this:

Script1:

global somevar 

put "me" into somevar 
Script2
put "you" into somevar 
Script2

Script2:

global somevar
TypeText tab  (* get focus to text box *)
put somevar   (* enters the stored variable into the text box  *)
Click "SaveButton"

So, the first time Script2 is called, “me” is entered into the text box and saved. The second time Script2 is called, “you” is entered and saved.

I want to set the value of somevar in Script1, and Script2 will use that value when it executes. As somevar in Script1 changes, and when Script2 executes, it uses the new value. And so on. But that is the piece I can’t get to work.

The code you have should be doing what you describe. The only problem I see is that you are saying “put somevar” where you actually want to “typetext somevar” if you want EggPlant to type the value on the remote system. “Put” is equivalent to something like “writeln” or “print” and simply writes the value to the local output stream (in this case, the Run window). The global variable behavior is exactly what you describe.

Thanks. It’s working! As long as…

  1. If I make a modification to Script2, it has to be saved. Otherwise when I run Script1, Eggplant seems to be calling a cached version of Script2 and doesn’t use the modified script.

  2. The global variable has to be declared on both scripts (just as you and the Reference guides said) :slight_smile: . I wasn’t always doing this.

Open the Preferences, go to the Script panel, and change the save setting to “Save All Scripts” when you run a script.

Fantastic! Another issue solved. Much appreciated.