Jump to content

Pass a variable to another script


Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...