Jump to content

Complete newbie issues with ControlClick() And with Do/Until loop


nodos
 Share

Recommended Posts

Hello all, this is my first post.

I am a complete newbie in scripting, Autoit is my first attemp at learn a bit about scripts and I still cannot wrap my head around the logic of many of the functions. So, following instructions from random posts in here and command samples in the wiki, or other snippets I found, I started trying to built simple scripts with some limited success.

I must apologize in advance for my English, as well as for my inability to use the correct search terms to research answers to the problem I run below - I am sure it has already been answered, maybe multiple times, but I cannot seem to be able to find the relevant information, after several hours of searching. Also, the script I try to build has no practical purpose, please let me know if I shouldn't make use of the time of the users of this forum for something that has not a practical purpose

While trying to make a Do/Until loop work, I run to a completely different problem:

I am using a message box that displays one by one the elements of my array. The message box has a button called "OK". What I was trying to do was to have the message box display, then after some time automatically press OK and move onto the next message box.

After finding the relevant information in AutoIt Window Info, I ended up with the following lines:

Func F_Esc()
   Sleep(1000)
   ControlClick("Message Box", "", "[ID:2; TEXT:OK; CLASS:Button; INSTANCE:1]", "right", "1")
   Sleep(500)
EndFunc

("right" because my mouse has inverted buttons)

However, the script simply hangs there until I manually press enter or click on "OK".

Which leads to my second issue: The way I have formatted the script, the moment I hit "OK" the loop stops, instead of go to the next element.

This works:

#include <array.au3>

Opt('MustDeclareVars', 1)

Global $zelmnt, $ucount, $count

F_declare()
F_loop()

Func F_declare()

;number of array elements
$zelmnt = 10
$ucount = $zelmnt-1

Global $elmnt[$zelmnt] = [ _
   "One", "Two", "Three", "Four", "Five", _
   "Six", "Seven", "Eight", "Nine", "Ten"]
EndFunc

Func F_loop()
Local $count = -1
Do
   $count = $count+1
   MsgBox(0, "Message Box", $elmnt[$count])
Until $count = $ucount
MsgBox(0, "", "Done!")
EndFunc

but with manually clicking on "OK"

While this does not:

#include <array.au3>

;Stop script with ESC
HotKeySet("{ESCAPE}", "F_quit")

Opt('MustDeclareVars', 1)

Global $zelmnt, $ucount, $count

F_declare()
F_loop()

Func F_declare()

;number of array elements
$zelmnt = 10
$ucount = $zelmnt-1

Global $elmnt[$zelmnt] = [ _
   "One", "Two", "Three", "Four", "Five", _
   "Six", "Seven", "Eight", "Nine", "Ten"]
EndFunc

Func F_loop()
Local $count = -1
Do
   $count = $count+1
   F_Show()
   F_Esc()
   F_Show2()
   F_Esc2()
Until $count = $ucount
MsgBox(0, "", "Done!")
EndFunc

Func F_Show()
   MsgBox(0, "Message Box", $elmnt[$count])
EndFunc

Func F_Esc()
   Sleep(1000)
   ControlClick("Message Box", "", "[ID:2; TEXT:OK; CLASS:Button; INSTANCE:1]", "right", "1")
   Sleep(500)
EndFunc

Func F_Show2()
   MsgBox(0, "Are you sure?", "OKAY")
   Sleep(1000)
EndFunc

Func F_Esc2()
   ControlClick ("Are you sure?", "", "[ID:2; TEXT:OK; CLASS:Button; INSTANCE:1]", "right", "1")
   Sleep(500)
EndFunc

Func F_quit()
Exit 0
EndFunc

Anyone can offer some insight? Thanks in advance and my apologies again if this has already been answered.

Edited by nodos
Link to comment
Share on other sites

ControlClick("Message Box", "", "[CLASS:Button; INSTANCE:1]","right")

Is how to use that.

But you cannot Click on a MsgBox() button in your own script.

MsgBox() is a blocking function, which means your script wiil not continue until it is closed.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thank you for your answer!

So there's no use to keep messing with MsgBox to test my Do/Until loop. I deleted both F_Esc functions from the second code box above.

Can I ask for some explanation on the Do/Until loop itself?

Is it not possible to call an array loop through functions, as I tried to do? The array call has to be included directly between the "Do" and "Until", instead of been referenced?

From the codes I pasted above, this works fine:

Func F_loop()
Local $count = -1
Do
   $count = $count+1
   MsgBox(0, "Message Box", $elmnt[$count])
Until $count = $ucount
MsgBox(0, "", "Done!")
EndFunc

While this keeps giving me message boxes with the array element "One":

Func F_loop()
Local $count = -1
Do
   $count = $count+1
   F_Show()
Until $count = $ucount
MsgBox(0, "", "Done!")
EndFunc

Func F_Show()
   MsgBox(0, "Message Box", $elmnt[$count])
EndFunc

(much similar to my earlier failure with For/Next loop :) )

Link to comment
Share on other sites

MsgBox has a WAIT parameter, which you can use to auto close it after the seconds you've specified.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Func F_loop()
Local $count = -1
Do
   $count = $count+1
   F_Show($count); pass count to function
Until $count = $ucount
MsgBox(0, "", "Done!")
EndFunc

Func F_Show($count)
   MsgBox(0, "Message Box", $elmnt[$count])
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Awesome, my script works perfectly well now!

Thank you both for the help, much appreciated.

 

#include <array.au3>

;Stop script with ESC
HotKeySet("{ESCAPE}", "F_quit")

Opt('MustDeclareVars', 1)

Global $zelmnt, $ucount, $count

F_declare()
F_loop()

Func F_declare()

;number of array elements
$zelmnt = 10
$ucount = $zelmnt-1

Global $elmnt[$zelmnt] = [ _
   "One", "Two", "Three", "Four", "Five", _
   "Six", "Seven", "Eight", "Nine", "Ten"]
EndFunc

Func F_loop()
Local $count = -1
Do
   $count = $count+1
   F_Show($count); pass count to function
   F_Show2($count); pass count to function
Until $count = $ucount
MsgBox(0, "", "Done!")
EndFunc

Func F_Show($count)
   MsgBox(0, "Message Box", $elmnt[$count], 1)
EndFunc

Func F_Show2($count)
   MsgBox(0, "Are you sure?", "OKAY", 1)
   Sleep(300)
EndFunc

Func F_quit()
Exit 0
EndFunc

Link to comment
Share on other sites

;#include <array.au3> -- you dont use _Array funcs so you dont need this include
Opt('MustDeclareVars', 1)
HotKeySet("{ESCAPE}", "F_quit")

Global $elmnt[10] = [ _
        "One", "Two", "Three", "Four", "Five", _
        "Six", "Seven", "Eight", "Nine", "Ten"] ; dont declare global variables in a func - it's bad coding practice and sometimes may not work as expected 
;~ Global $bQuit = False

F_loop()

Func F_loop()
    Local $count = -1
    Do
;~      if $bQuit Then ExitLoop -- just another variant of your quit func
        $count = $count + 1
        F_Show($count); pass count to function
        F_Show2($count); pass count to function
    Until $count = UBound($elmnt) - 1 ; with this function you don't need to declare an extra variable
    MsgBox(0, "", "Done!") ; if you use exitloop, this will be the first executed line
EndFunc   ;==>F_loop

Func F_Show($count)
    MsgBox(0, "Message Box", $elmnt[$count], 1)
EndFunc   ;==>F_Show

Func F_Show2($count)
    MsgBox(0, "Are you sure?", "OKAY", 1)
EndFunc   ;==>F_Show2

Func F_quit()
;~  $bQuit = True
    Exit
EndFunc   ;==>F_quit

I've rewritten your script and added a few comments which will be usefull to you.

Link to comment
Share on other sites

Wow, thank you very much. Very much appreciated.

I've managed to succesfully repeat the task with For/Next and While/Wend loop.

Opt('MustDeclareVars', 1)

HotKeySet("{ESCAPE}", "F_quit")

Global $elmnt[10] = [ _
      "One", "Two", "Three", "Four", "Five", _
      "Six", "Seven", "Eight", "Nine", "Ten"]
Global $bQuit = False

F_loop()

Func F_loop()
   For $count = 0 to UBound($elmnt) -1 step 1
      if $bQuit Then ExitLoop
      F_Show($count)
      F_Show2($count)
   Next
   MsgBox(0, "", "Done!", 5)
EndFunc   ;==>F_loop

Func F_Show($count)
   MsgBox(0, "Message Box", $elmnt[$count], 1)
EndFunc   ;==>F_Show

Func F_Show2($count)
   MsgBox(0, "Are you sure?", "OKAY", 1)
EndFunc   ;==>F_Show2

Func F_quit()
   $bQuit = True
;    Exit
EndFunc   ;==>F_quit

Opt('MustDeclareVars', 1)

HotKeySet("{ESCAPE}", "F_quit")

Global $elmnt[10] = [ _
      "One", "Two", "Three", "Four", "Five", _
      "Six", "Seven", "Eight", "Nine", "Ten"]
Global $bQuit = False

F_loop()

Func F_loop()
   Local $count = -1
   While $count < UBound($elmnt) - 1
      if $bQuit Then ExitLoop
      $count = $count + 1
      F_Show($count)
      F_Show2($count)
   WEnd
   MsgBox(0, "", "Done!", 5)
EndFunc   ;==>F_loop

Func F_Show($count)
   MsgBox(0, "Message Box", $elmnt[$count], 1)
EndFunc   ;==>F_Show

Func F_Show2($count)
   MsgBox(0, "Are you sure?", "OKAY", 1)
EndFunc   ;==>F_Show2

Func F_quit()
   $bQuit = True
;    Exit
EndFunc   ;==>F_quit

Now I was wondering if someone could point me to the right direction to find some exercises that I could use to continue practicing in scripting.

Thank you all again for taking the time to read through my script and help me out.

Link to comment
Share on other sites

Well, there are plenty of exercises in the Examples folder of your AutoIt install, plus plenty to see and investigate in the Examples section of the Forum. There is also a >Tutorial there, which I'm not exactly sure how up-to-date it is, but should provide plenty of interesting things to investigate.

EDIT

And don't forget the Wiki.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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