How to pass a variable value from one AutoIt script script to the next

5.4k Views Asked by At

I am running an AutoIt script and that script calls another AutoIt file. How do I pass a variable value from my first script to the next?

2

There are 2 best solutions below

0
On

You need to learn the scope concept of a variable (Dim, Global & Local variables).

From AutoItHelp

The difference between Dim, Local and Global is the scope in which they are created:

  • Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!) ;
  • Global = Forces creation of the variable in the Global scope ;
  • Local = Forces creation of the variable in the Local/Function scope.

Examples with two files: main.au3 and constantes.au3.

Content of constants.au3

#include-once

; Declaration of global variables
Global $name_application = "Foo"
Global $year = 2014

Content of main.au3

#include <constants.au3>

Func _foo()
    ConsoleWrite("In function _foo() name_application is available and it's equals = "&$name_application&@CRLF)
    Local $year2= 2014
EndFunc

ConsoleWrite("In main.au3 global variables are available"&@CRLF)
ConsoleWrite("For example, name_application = "&$name_application&@CRLF)
ConsoleWrite("But the local variable year2 isn't available here")

More information is here: http://www.autoitscript.fr/autoit3/docs/keywords/Dim.htm

0
On

Use command line interface in order to communicate between two files. File 2 must be compiled.

File1.exe:

$ThisIsVariableFromFIle1 = "This is some text."
Run("File2.exe " & $ThisIsVariableFromFIle1)

File2.exe:

MsgBox(0,"This is the whole commandline I got", $CmdLineRaw)
MsgBox(0,"This is part one", $CmdLine[1]); This
MsgBox(0,"This is part two", $CmdLine[2]); is
MsgBox(0,"This is part three", $CmdLine[3]); some