Jump to content

Setting a HotKey to exit a loop


Recommended Posts

My script is a GUI that creates a list of programs to install, and provides a conection to the installation file itself. The next step I am taking is to fully automate the installation process. It isn't a problem, except when I or a user closes out the installation midway. I am telling AutoIt to winwaitactive() for a window that will no longer exist given that the installation was terminated. It makes my GUI unresponsive becuase once the installation would be through, it would exit back out to the GUI for another one to be selected. But since this isn't happening, the script is in a state of limbo. I want to create a F11 hotkey that can be used to Exit the If...ElseIf...EndIf loop if this happens, that way when F11 is pressed it gets the program out of the installation loop script and kicks it back to the GUI.

Here is my format:

HotKeySet("{F11}", "ExitLoop") 

$msg = GUIGetMsg()

Global $i_index
While 1

;This is where the rest of my script is, it is very long and it would be annoying to scroll through.

WEnd

Func ExitLoop
    $exitloop = NOT $exitloop
    While $exitloop
        sleep(100)
        ToolTip
    WEnd
    ToolTip("")
EndFunc

I have no idea what I am doing. I basically copyed and pasted from the AutoIt help file. I was hoping that the top line would be enough to satisfy, but alas, it gives me this error.

C:\Scripts\GUI.au3(46,30) : ERROR: ExitLoop(): undefined function.
HotKeySet("{F11}", "ExitLoop")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Scripts\GUI.au3 - 1 error(s), 0 warning(s)

any help is greatly appriciated. :D

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

Func exitloop() <-- brackets

{brackets} or (parenthasis) ?

I tried parenthasis already as in ExitLoop() and it didnt make a difference. I will try ExitLoop{}.

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

I tried ExitLoop(), ExitLoop{}, ExitLoop(1)

it doesn't seem to register that ExitLoop is a function. it tells me:

ERROR: ExitLoop()(): undefined function.

notice the second set of parenthasis? :D

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

I tried ExitLoop(), ExitLoop{}, ExitLoop(1)

it doesn't seem to register that ExitLoop is a function. it tells me:

ERROR: ExitLoop()(): undefined function.

notice the second set of parenthasis? :D

try this _Exitloop()

edit: the function would be like this func _Exitloop() but the hotkey would be like this "_Exitloop"

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Exitloop is already a built-in command. Try using a different name like Exit_Loop() or something.

OK I get it. you can't make a hotkey using a command that already exists?

edit: I tried plugging the _ExitLoop() in and it gave me the same error. do I have to define the function before plugging it there?

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

HotKeySet("{F11}", "_ExitLoop") 

$msg = GUIGetMsg()

Global $i_index
While 1

;This is where the rest of my script is, it is very long and it would be annoying to scroll through.

WEnd

Func _ExitLoop()
    local $exitloop = NOT $exitloop
    While $exitloop
        sleep(100)
        ToolTip('')
    WEnd
    ToolTip("")
EndFunc

Edit:Is $exitloop a global variable? I'm not sure if thats what you want.

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

HotKeySet("{F11}", "_ExitLoop") 
Global $exitloop = False

While 1
    If $exitloop = True Then
        MsgBox( 0 , '' , '$exitloop = '& $exitloop &' , Will exit the loop now.' )
        $exitloop = NOT $exitloop
        ExitLoop
    EndIf
WEnd

Func _ExitLoop()
    $exitloop = True
EndFunc

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

HotKeySet("{F11}", "_ExitLoop") 
Global $exitloop = False

While 1
    If $exitloop = True Then
        MsgBox( 0 , '' , '$exitloop = '& $exitloop &' , Will exit the loop now.' )
        $exitloop = NOT $exitloop
        ExitLoop
    EndIf
WEnd

Func _ExitLoop()
    $exitloop = True
EndFunc
Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

I just noted something.

"ExitLoop will break out of a While, Do or For loop." (not an "if" loop!!) :D

Is there a command that breaks out of an If loop or cancels the current command? I am beginning to think that my problem has no solution. :wacko:

EDIT: nevermind about the above, I realized that the whole thing that I would wish to kill with the hotkey is within a While/WEnd string so this seems to be the best way to tackle the problem.

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

EDIT: nevermind about the above, I realized that the whole thing that I would wish to kill with the hotkey is within a While/WEnd string so this seems to be the best way to tackle the problem.

Good, :D

Feel free to ask me any question you have in mind.

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Good, :D

Feel free to ask me any question you have in mind.

Here's a question: How can I make it work? :wacko: I just hope that all this typing I am doing helps you understand my problem. If not, I can explain further.

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

You shouldnt need the $exitloop variable at all. I think you also have the wrong quotes on your "Will exit the loop now" MsgBox. You also have a typo(i think) in WinActivate("Neoopsystems FEAD Optimizer", "") am I right to assume it should be netopsystems instead of neoopsystems... try this.

HotKeySet("{F11}", "_ExitLoop")

Global $i_index
While 1
    $msg = GUIGetMsg()
    
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
            
        Case $msg = $okbutton
            MsgBox(4096, "Hot Key", "If this window ever becomes unresponsive, especially after quitting an install that is in progress, press F11.  If that does not work, please quit the script by right clicking on the dark blue icon on the taskbar and selecting " & '"' & "End Script" & '"' & ".")
            $data = GUICtrlRead($combo)
            
            If $data = "Select Program" Then
                MsgBox(64, "Whoops!", "Please select a program to (un)install.")
            ElseIf $data = "Adobe Reader 6.0" Then
                $adobe_auto = MsgBox(4100, "Automated?", "Would you like the (un)installation of " & $data & " to be automated?")
                If $adobe_auto = 6 Then
                    Run("\\rminas\software\Adobe\Acrobat Reader\AdbeRdr60_enu_full.exe")
                    MsgBox(4096, "Please Wait", "Please be patient as the installer initializes...", 5)
                    WinWaitActive("Netopsystems FEAD Optimizer", "")
                    WinActivate("Netopsystems FEAD Optimizer", "")
                    WinWaitActive("Adobe Reader 6.0.1 - Setup", "Next")
                    Send("{ENTER 4}")
                    WinWaitActive("Adobe", "Finish")
                    Send("{ENTER}")
                    MsgBox(4096, "Finished", "Installation of Adobe Acrobat Reader 6.0.1 is finished.")
                ElseIf $adobe_auto = 7 Then
                    Run("\\rminas\software\Adobe\Acrobat Reader\AdbeRdr60_enu_full.exe")
                EndIf
    EndSelect
    
WEnd
Func _ExitLoop()
    MsgBox(0 , '"" , "Will exit the loop now.")
    ExitLoop
EndFunc

Edit: forgot to close code tag

Edit2: What is $i_index for? I dont see it anywhere in the script. Is it used in the GUI?

Edited by BPBNA
Link to comment
Share on other sites

Thanks BPBNA for the help.

OK, I think this should help, you don't need to break out if the If statments.

Opt( 'RunErrorsFatal' , 0 )
HotKeySet("{F11}", "_ExitLoop")
Global $exitloop = False



$msg = GUIGetMsg()

Global $i_index
While 1
    If $exitloop = True Then
        MsgBox( 0 , '' , '$exitloop = '& $exitloop &' , Will exit the loop now.' )
        $exitloop = NOT $exitloop
        ExitLoop
    EndIf
   
    $msg = GUIGetMsg()
   
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
           
        Case $msg = $okbutton
            MsgBox(4096, "Hot Key", "If this window ever becomes unresponsive, especially after quitting an install that is in progress, press F11.  If that does not work, please quit the script by right clicking on the dark blue icon on the taskbar and selecting 'End Script'.")
            $data = GUICtrlRead($combo)
           
            If $data = "Select Program" Then
                MsgBox(64, "Whoops!", "Please select a program to (un)install.")
            ElseIf $data = "Adobe Reader 6.0" Then
                $adobe_auto = MsgBox(4100, "Automated?", "Would you like the (un)installation of " & $data & " to be automated?")
                If $adobe_auto = 6 Then
                    _AutomateInstall(1)
                    If @error = 1 Then MsgBox( 0 , "Error" , "Unable to run the setup file." )
                    If @error = 2 Then MsgBox( 0 , "Time out." , "Install failed! Sorry better luck next time." )
                    
                ElseIf $adobe_auto = 7 Then
                    Run("\\rminas\software\Adobe\Acrobat Reader\AdbeRdr60_enu_full.exe")
                    If @error Then MsgBox( 0 , "Error" , "Unable to run the setup file." )
                EndIf
            EndIf
    EndSelect
WEnd
Func _AutomateInstall($P_Mode)
    Switch $P_Mode
        Case 1
            Run("\\rminas\software\Adobe\Acrobat Reader\AdbeRdr60_enu_full.exe")
            If @error Then
                SetError(1)
                Return
            EndIf
            MsgBox(4096, "Please Wait", "Please be patient as the installer initializes...", 5);You can replace this with SplashTextOn()
            If WinWaitActive("Netopsystems FEAD Optimizer", "" , 300) = 0 Then      ;Timeout of 5 minutes
                SetError(2)
                Return
            EndIf
            
            WinActivate("Neoopsystems FEAD Optimizer", "")
            If WinWaitActive("Adobe Reader 6.0.1 - Setup", "Next" , 300) = 0 Then       ;Timeout of 5 minutes
                SetError(2)
                Return
            EndIf
            Send("{ENTER 4}")
            WinWaitActive("Adobe", "Finish")
            Send("{ENTER}")
            MsgBox(4096, "Finished", "Installation of Adobe Acrobat Reader 6.0.1 is finished.")
        Case 2
            ;Install something else
        Case 3
            ;Something else here too, WOW
        Case Else
            ;KUKU in the head!!!
    EndSwitch
    
EndFunc

Func _ExitLoop()
    $exitloop = True
EndFunc

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

BPBNA :

-I dont know where the Global i_index comes from but I'll leave it, as long as the script works. :wacko:

-yes the "netopsystem" was a typo but I guess it didn't make a difference becuase it worked just fine.

-I like your script changes better becuase it allows me to import it without making large changes to my whole script.

-It tells me that I am not allowed to have an ExitLoop outside of a loop (in the Func area on the bottom).

rbhkamal:

-while I am sure that getting rid of the If's would work, I have 300 lines of code as it is and I don't want to change the inner-workings of my script. I would get too lost to know what I was doing. I have a while covering the whole thing, three case selections (one for the OK button of the GUI, one for the cancel button, and one for closing the window) and then a dozen or so If's when the OK button is pressed. Can I have a case within a select? Also could you try to outline the changes you made so I could follow it? I hardly recognized my own script! :D

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

Here is the File that creates the GUI and reads what the combo box selection is. The FULL file also checks for missing device drivers, makes sure the computer name matches the service tag, the computer is joined to the domain, and then scans for all of the programs and if they are not installed they are typed out in the input box above the combo box.

It should run as is, and if not, make sure all the include files are in the correct folder.

edit: this program may not work because I added the hotkey stuff. Erase lines 47-52 for the version I had on my own before the hotkey.

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

Dear Mike,

1- Your script will always get stuck at winwaitactive() for ever if the window does not exist, so in the first case (adobe) I added a timeout for each winwaitactive() of five minutes (change it if required).

2- If the script failed to execute the function run () it will crash so I added in the top (Opt (RunErrorsFatal, 0)) to prevent that from happening.

3- If the Run() function failed to start the setup file (or what ever) you will have to let the user know, so I added this

Run("\\rminas\software\Adobe\Acrobat Reader\AdbeRdr60_enu_full.exe")
    If @error Then  ;Added
        ;This means that the RUN() failed!, you can't continue in this case.  ;Added
        MsgBox( 0 , "Error" , 'Lunching setup failed!' )  ;Added
        Return;Will ignore everything beyond this and go back to the top of the while loop.  ;Added
    EndIf  ;Added

4- To keep your While 1 loop clean, I cut and past to a function.(_OKButton())

5- In attempt to make the function _OKButton() look better I cut and past to the function AutoMate_Adobe_Install()

Notice the positioning of the (Return) functions.

Don't forget about the help file!!!!!!!!!

It's getting dark over here.....

Edit:I removed the attachment to save space

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Dear Mike,

1- Your script will always get stuck at winwaitactive() for ever if the window does not exist, so in the first case (adobe) I added a timeout for each winwaitactive() of five minutes (change it if required).

2- If the script failed to execute the function run () it will crash so I added in the top (Opt (RunErrorsFatal, 0)) to prevent that from happening.

3- If the Run() function failed to start the setup file (or what ever) you will have to let the user know, so I added this

Run("\\rminas\software\Adobe\Acrobat Reader\AdbeRdr60_enu_full.exe")
    If @error Then  ;Added
        ;This means that the RUN() failed!, you can't continue in this case.  ;Added
        MsgBox( 0 , "Error" , 'Lunching setup failed!' )  ;Added
        Return;Will ignore everything beyond this and go back to the top of the while loop.  ;Added
    EndIf  ;Added

4- To keep your While 1 loop clean, I cut and past to a function.(_OKButton())

5- In attempt to make the function _OKButton() look better I cut and past to the function AutoMate_Adobe_Install()

Notice the positioning of the (Return) functions.

Don't forget about the help file!!!!!!!!!

It's getting dark over here.....

That's some mastery of AutoIt right there! :)

Thanks for the help. The Return idea was defidently good, but the thing I was worried about is what if the User presses cancel sometime during the install process OR the install ends itself (for example if the program is already installed and it quits on its own)? That is why i wanted the option to quit the loop to be user-defined. I wouldnt want to add that timeout to every line of code. :D

Maybe there is a way to set the hotkey to do the "Return" command? If not then I will probably just go with what you so graciously provided me with! :D

Thanks for taking some time out of your life to help me out. I really appriciate it. :(

Sounds like its time for you to get some :wacko::D !

[/overuse of smileys]

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

but the thing I was worried about is what if the User presses cancel sometime during the install process OR the install ends itself (for example if the program is already installed and it quits on its own)? That is why i wanted the option to quit the loop to be user-defined. I wouldnt want to add that timeout to every line of code.

If that happened then the winwaitactive() will timeout and that will trigger the return.

:D

RK

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

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...