Sign in to follow this  
Followers 0
Morthawt

Send $var data to a new script execution?

12 posts in this topic

Is it possible for me to send the contents of variables to a new script I will be running? Like with a cmd file, I can do "Start cmd.exe /c newbat.cmd" and the newbat.cmd can still echo var's that were set in my original bat file.

Is this possible? I made a test with

Global $hi = "Hello"
Run(@AutoItExe & ' /autoit3executescript test2.au3')
and one with
MsgBox(0,"Title","Hello" & $hi)
I want that second one to know var data from the first.

Is this possible with Autoit3?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Is it possible for me to send the contents of variables to a new script I will be running? Like with a cmd file, I can do "Start cmd.exe /c newbat.cmd" and the newbat.cmd can still echo var's that were set in my original bat file.

Is this possible? I made a test with

Global $hi = "Hello"
Run(@AutoItExe & ' /autoit3executescript test2.au3')
and one with
MsgBox(0,"Title","Hello" & $hi)
I want that second one to know var data from the first.

Is this possible with Autoit3?

Not transparently like you tried, but it's a nice idea.

Global $hi = "Hello"
Run(@AutoItExe & ' /autoit3executescript test2.au3 "' & $hi & '"')

MsgBox(0,"Title","Hello" & $CmdLine[1])

See also: http://www.autoitscript.com/autoit3/docs/intro/running.htm

Or what also comes very close:

Global $hi = "Hello"
#include "the other file.au3"

The other file.au3:

MsgBox(0,"Title","Hello" & $hi)
Edited by Manadar

Share this post


Link to post
Share on other sites

hi Morthawt,

Take a look at :unsure:

Hope this helps,

-smartee

Share this post


Link to post
Share on other sites

Or what also comes very close:

Global $hi = "Hello"
#include "the other file.au3"

The other file.au3:

MsgBox(0,"Title","Hello" & $hi)

This part looks the easiest, I just need to understand it a bit more. Can you explain what this does? I see the #include but I am not sure that that means for the functioning of the file. Basically I have the first script doing a bunch of things, but because Autoit3 is not able to do many things at once, I need to make a new script that will constantly keep checking something by doing a command and outputting to a file and checking the file's contents. The thing is with this #include method, I would put inside the first script an include for the second? Then when I run the second script via the macro linking to the exe and the executescript part, the second script will just know all the variables from the first file due to the first file having an include for the second file?

Maybe I am confused.

Share this post


Link to post
Share on other sites

This part looks the easiest, I just need to understand it a bit more. Can you explain what this does? I see the #include but I am not sure that that means for the functioning of the file. Basically I have the first script doing a bunch of things, but because Autoit3 is not able to do many things at once, I need to make a new script that will constantly keep checking something by doing a command and outputting to a file and checking the file's contents. The thing is with this #include method, I would put inside the first script an include for the second? Then when I run the second script via the macro linking to the exe and the executescript part, the second script will just know all the variables from the first file due to the first file having an include for the second file?

Maybe I am confused.

Hi Mrthawt,

yes, the include will really merge the two scripts into one.

What you are looking for are two separate scripts. You can pass "Variables" or Parameters between the first (calling) and the second script, when the second one is started.

Maybe you take a closer look at what smatree posted, he explains how AutoIT receives and handles parameters.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Maybe you take a closer look at what smatree posted, he explains how AutoIT receives and handles parameters.

I have but it does not make sense to me. I have only been messing with this program for 2 days now. I am doing well but that does not make any sense how it works. Parameters? Even setting a variable to have data via that command line part makes no sense to me whatsoever. The only thing I understand so far would be to output a file which contains the variable sets like output $var1 = "This"

$var2 = "That"

then maybe "include" that file on the second script to set the variables?

Share this post


Link to post
Share on other sites

I have but it does not make sense to me. I have only been messing with this program for 2 days now. I am doing well but that does not make any sense how it works. Parameters? Even setting a variable to have data via that command line part makes no sense to me whatsoever. The only thing I understand so far would be to output a file which contains the variable sets like output $var1 = "This"

$var2 = "That"

then maybe "include" that file on the second script to set the variables?

When running (DOS) programs you always are using parameters.

e.g.

dir C:\windows\*

or

cd c:\temp

Example:

You can call your script after compilation:

MyScript.exe Paramter1 "Parameter 2"

In your script you can use Variables to address the given parameters:

MsgBox(0,"Parameters", $CmdLine[1] & $CmdLine[2])

This will result in a messagebox displaying "Parameter1Parameter 2".

So AutoIT processes every parameter as a Vsariable.

If you just include the first script into the second, you have 2 problems:

1. Your first script will run twice

2. The variables may not be filled correctly


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

It seems so complicated and not logical since I do not understand the workings of that.

This is what I have at the moment unless I understand now the new thing works.

;test.au3
Global $hi = "Hello"
MsgBox(0,"Title","first" & $hi)
$varfile = FileOpen("vars.au3",2)
FileWrite($varfile, '$hi="' & $hi & '"' & @CRLF)
FileClose($varfile)

RunWait(@AutoItExe & ' /autoit3executescript test2.au3')

;test2.au3
#include "vars.au3"

MsgBox(0,"Title",$hi)

Is this a bad way of doing what I am looking for?

Share this post


Link to post
Share on other sites

When running (DOS) programs you always are using parameters.

e.g.

dir C:\windows\*

or

cd c:\temp

Example:

You can call your script after compilation:

MyScript.exe Paramter1 "Parameter 2"

In your script you can use Variables to address the given parameters:

MsgBox(0,"Parameters", $CmdLine[1] & $CmdLine[2])

This will result in a messagebox displaying "Parameter1Parameter 2".

So AutoIT processes every parameter as a Vsariable.

If you just include the first script into the second, you have 2 problems:

1. Your first script will run twice

2. The variables may not be filled correctly

If there are a large amount of variables that need sending to the new script, will that commandline method still work? Just seems very confusing having just numbered variables and the order meaning the difference between something working and breaking it all.

Share this post


Link to post
Share on other sites

#10 ·  Posted

If you really do have a large amount of values you want to use in several scripts I would suggest to use .ini files.

Look at the Ini*() Function in the help file, they are handier than a simple text file, you can change them during execution and they work whilst your script is compiled.

An include cannot be altered after your script is compiled. From my point of view I always work with compiled exes once the script is finished.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#11 ·  Posted

Thanks, I get it now. I will experiment with this ini part. I have tested the commandline part too but its a bit much. That is great for like ping.exe 127.0.0.1 and "127.0.0.1" being $cmdline[1] to be used inside the exe when run, but for passing var's to a new bat file its a bit clunky. Ini would work much nicer for all the reasons you mentioned. I never thought about using ini.

Thanks

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

hi again,

I whipped up a quick couple of scripts to illustrate Hannes123's concept. The following example shows how to pass crucial information between Scripts using Ini files.

Script1.au3

$ini = @ScriptDir & "\Report.ini"
$nextScript = @ScriptDir & "\Script2.au3"

;Slow blocking task simulation
$perc = 0
$pid = Run(@AutoItExe & " /AutoIt3ExecuteScript """ & $nextScript & """")
While $perc <> 100
    IniWrite($ini, "internals", "progress", $perc)
    Sleep(Random(300,800,1))
    If IniRead($ini, "internals", "exitflag", 0) = 1 Then
        ExitLoop
    EndIf
    $perc += 1
WEnd
If ProcessExists($pid) Then
    ProcessClose($pid)
EndIf
FileDelete($ini)
Exit

Script2.au3

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

AdlibRegister("changemsg", "5000")
Local $ini = @ScriptDir & "\Report.ini"

Global $currentMsg = 0
Dim $messagesArr[1]
$messagesArr[0] = 0
_ArrayAdd($messagesArr, "Script1 is running in the background.")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "But Script1 cant respond immediately to user input.")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "Why? because it uses the Sleep() function")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "So what has Sleep() got do with this?")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "Well, Sleep() tells your script to do nothing for a while")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "Script2 however,(this script) is displaying Script1's progress and you can exit this anytime :)")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "This Script will close Script1 automatically upon its exit")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "Also, Script1 will close this gui automatically when finished")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "And its all because of the values shared in the ini file")
$messagesArr[0] += 1
_ArrayAdd($messagesArr, "Try sneaking a peek at them while this script is running ;)")
$messagesArr[0] += 1

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Script2: A frontend for Script1", 471, 116, 187, 173)
$Progress1 = GUICtrlCreateProgress(8, 80, 457, 25)
GUICtrlSetTip(-1, "Script1 progress")
$Edit1 = GUICtrlCreateEdit("", 8, 8, 457, 57, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
changemsg()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            IniWrite($ini, "internals", "exitflag", 1)
            Exit

    EndSwitch
    $perc = IniRead($ini, "internals", "progress", 0)
    If $perc <> GUICtrlRead($Progress1) Then GUICtrlSetData($Progress1, $perc)
WEnd

Func changemsg()
    $currentMsg = Mod($currentMsg, $messagesArr[0]) + 1
    GUICtrlSetData($Edit1, $messagesArr[$currentMsg])
EndFunc   ;==>changemsg

Run Script1.au3 and watch the magic unfold.

Hope this helps :unsure:

-smartee

EDIT: Removed a stray percentage sign

Edited by smartee

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0