antmar904

Pass a variable to another script

11 posts in this topic

#1 ·  Posted

Hi,

I know this has been asked many time but I can't seem to find the answer for what I want to do.

I have one script asking the user if they want to reboot their computer, if they click yes then run script 2.  

Script 2 will then read the what the $Reboot varabile is set to (either 0 or 1) and perform a job depending on the return value.

I can't seem to figure out how to get script two to read the $Reboot value from script 1

Script 1:

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnYes
                Global $Reboot = 1
                Run(@ScriptDir & "\PatchDelayRed.exe")
                Exit
            Case $btnNo
                GUISetState(@SW_HIDE)
                msg3()
                Exit
        EndSwitch
    WEnd

Script 2:

AutoItSetOption("TrayIconHide", 1)

Global $Reboot

If $Reboot = 0 Then
Do
    Sleep(5000) ;Sleep for two hours
    Run(@ScriptDir & "\PatchRebootRed_Eng.exe")
    Exit
Until $Reboot = 1
ElseIf $Reboot = 1 Then
MsgBox(0, "", "Rebooting Now")
Exit
EndIf

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

what do you mean script 1 script2 ? are these script has separate SciteWindow?


ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Share this post


Link to post
Share on other sites

#3 ·  Posted

script 1 is a gui with a yes and no button.

depending on what the user selects (yes or no) I set the $Reboot variable to either 1 (yes) or 0 (no) then I run script 2 which I need to be able to get the value of the variable $Reboot.

Share this post


Link to post
Share on other sites

#4 ·  Posted

Just use $CmdLineRaw or $CmdLine[1] in your second script, so for example:

Script 1

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnYes
                Global $Reboot = 1
                Run(@ScriptDir & "\PatchDelayRed.exe True")
                Exit
            Case $btnNo
                GUISetState(@SW_HIDE)
                msg3()
                Exit
        EndSwitch
    WEnd

Script 2

AutoItSetOption("TrayIconHide", 1)

Global $Reboot = False
If $CmdLine[0] >= 1 Then $Reboot = $CmdLine[1]

If $Reboot = False Then
    Do
        Sleep(5000) ;Sleep for two hours
        Run(@ScriptDir & "\PatchRebootRed_Eng.exe")
        Exit
    Until $Reboot = True
ElseIf $Reboot = True Then
    MsgBox(0, "", "Rebooting Now")
    Exit
EndIf

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

1 hour ago, antmar904 said:

script 1 is a gui with a yes and no button.

depending on what the user selects (yes or no) I set the $Reboot variable to either 1 (yes) or 0 (no) then I run script 2 which I need to be able to get the value of the variable $Reboot.

@antmar904

You can't read values of variables from one script/program in another script/program this is a misconception.
Variables are subject to an Scope, as far as I know there is not Scope for a variable outside the program/script.

What you need is to pass a parameter to your second script.

You could take a quick read of Variables Scopes and Functions Parameters and that would clarify your misconception and eventually help you to figure it out or to find a better way/approach.

Share this post


Link to post
Share on other sites

#6 ·  Posted

21 minutes ago, alien4u said:

@antmar904

You can't read values of variables from one script/program in another script/program this is a misconception.
Variables are subject to an Scope, as far as I know there is not Scope for a variable outside the program/script.
 

 

Share this post


Link to post
Share on other sites

#7 ·  Posted

4 hours ago, antmar904 said:

 if they click yes then run script 2

Why not : if they click yes then run func_1, if they click no then run func_2 ?

Share this post


Link to post
Share on other sites

#8 ·  Posted

8 hours ago, gil900 said:

 

My previous statement still valid since you are accessing the memory address of the variables, does that mean you modify the variable scope? does that make true the assumption that you can access/pass variables values from/to another program?
My point here is that the OP should consider learning basic concepts which would help him to understand/accomplish in a better way what he is trying to do.
(This is applying the premise: Teach a man how to Fish). After he knows how to fish then he can probably learn to catch fishes with his bare hands.

Share this post


Link to post
Share on other sites

#9 ·  Posted

Another solution is if Script 1 has "Reboot" selected then write a small text file with a unique name to the user's Documents folder. When Script 2 starts, check for that file - if it exists, delete it and know that the computer has been rebooted.


Who lied and told you life would EVER be fair?

Share this post


Link to post
Share on other sites

#10 ·  Posted

Thank you all for your responses.  The route I took was to right to a text file (either 0 or 1) then read that text file with script 2.

I'm sure all the other ways would work as well, but this is the way I went. :)

Thanks again!

Share this post


Link to post
Share on other sites

#11 ·  Posted

2 hours ago, antmar904 said:

Thank you all for your responses.  The route I took was to right to a text file (either 0 or 1) then read that text file with script 2.

I'm sure all the other ways would work as well, but this is the way I went. :)

Thanks again!

.INI Files & Registry Keys are both ways I do this kind of thing.

If working with files I keep it in the Temp Directory, but I prefer registry as its away from the user and easy to reference. 

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