Jump to content

Trying to use a variable to set how many times a for loop should run in autoit, but I only know python.


Recommended Posts

Normally use python but need autoit for a specific project, I'm trying to run a for loop which is controlled by a variable which is taken from an inputbox, something like this in python but in autoit, python text below, can't find online or figure out how to do this without using a while loop:

amount = int(input("How many times would you like this to run? "))
for i in range(amount):

 

Link to comment
Share on other sites

1 hour ago, Jos said:

Helpfile might give you all the answers you want when it comes to syntax... ;) 

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
Func Inputter()
   $Amount = Number(InputBox("Test box", "How many times shall I run?", "0", "", -1, -1, 0, 0))
EndFunc
Func Opera()
   MouseMove(722,407)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(704,403)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(1181,281)
   MouseClick($MOUSE_CLICK_LEFT)
   MouseMove(838,525)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(1000)
   MouseMove(22,52)
   MouseClick($MOUSE_CLICK_LEFT)
EndFunc
Call(Inputter())
;Local $Timeout = 10
;MsgBox($MB_SYSTEMMODAL,"ETC","This will timeout in 10 seconds.",10)
MouseMove(571,836)
MouseClick($MOUSE_CLICK_LEFT)
For $Amount = 1 To $Amount Step 1
   Call(Opera())
Next

Kind've fixed it, works now but doesn't repeat with this but repeats under other conditions, still working on it.

 

Link to comment
Share on other sites

Doesnt work, dont understand why, spent more than 5 hours trying to get this working, i need help. Decided to not use a For loop as that didnt work either, ive narrowed the problem down to the input, the $sAmount variable just doesnt work, but any other variable does work at the bottom when needs to be equal to $count.

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
Func Inputter()
   Local $sAmount = (InputBox("Test box", "How many times shall I run?", "0", "", -1, -1, 0, 0))
   $sAmount=Number($sAmount)
EndFunc
Func Opera()
   MouseMove(722,407)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(704,403)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(1181,281)
   MouseClick($MOUSE_CLICK_LEFT)
   MouseMove(838,525)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(1000)
   MouseMove(22,52)
   MouseClick($MOUSE_CLICK_LEFT)
EndFunc
;Local $count=0
Call(Inputter())
;Local $Timeout = 10
;MsgBox($MB_SYSTEMMODAL,"ETC","This will timeout in 10 seconds.",10)
MouseMove(571,836)
MouseClick($MOUSE_CLICK_LEFT)
;For $i = 1 To $Amount Step 1
 ;  Call(Opera())
;Next
Do
   Call(Opera())
   $count = $count+1
Until $count>$sAmount

 

Link to comment
Share on other sites

Hi @Data_Unavailable:)

Here is your working code:

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
Global $sAmount
Func Inputter()
   $sAmount = (InputBox("Test box", "How many times shall I run?", "0", "", -1, -1, 0, 0))
   $sAmount = Number($sAmount)
EndFunc
Func Opera()
   MouseMove(722,407)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(704,403)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(1181,281)
   MouseClick($MOUSE_CLICK_LEFT)
   MouseMove(838,525)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(1000)
   MouseMove(22,52)
   MouseClick($MOUSE_CLICK_LEFT)
EndFunc
Global $count=0
Inputter()
;Local $Timeout = 10
;MsgBox($MB_SYSTEMMODAL,"ETC","This will timeout in 10 seconds.",10)
MouseMove(571,836)
MouseClick($MOUSE_CLICK_LEFT)
;For $i = 1 To $Amount Step 1
 ;  Call(Opera())
;Next
Do
   Opera()
   $count = $count+1
Until $count>=$sAmount

Here comes the explanation to the changes:

  • You defined $sAmount as a Local variable within the Inputter function. Doing so it is available within the function only, so i instead added it as a global variable and referenced it in the function.
  • In you latest post, you commented out your variable definition of $count, so using it in the loop below resulted in a crash. Also using Local outside a function works, but does basically the same as global, except confuse the reader.
  • You used the Call function for function calls. This is not how the Call function works. Your expression within the Call function parentheses was executed the actual call, and the Call function afterwards tried to execute the response as a function, failing silently via the @error flag.
  • And finally i changed your greater than condition to a greater or equal to, as the loop +1 of the requested number given from the input box.

But all in all, i think this is a great start for you :D . You seem to take (constructive) criticism well and the script was 90% working already, nice :D

Hope it helps!

Edited by genius257
Link to comment
Share on other sites

Currently I'm a student and my homework system is google classrooms, I have a few dozen pieces of homework outstanding from last year from when i had covid, and i could manually go into and mark them as submitted, and i thought i could make this code do that easily as an auto clicker, but i tripped out yesterday trying to make the loop, when I start a project I normally like to finish it. Basically the script moves the mouse from starting the program and clicks the google tab on taskbar, then goes into the homework, and then clicks the go back button in top left corner and loops to start of program after where it enters google.

Link to comment
Share on other sites

12 hours ago, genius257 said:

Hi @Data_Unavailable:)

Here is your working code:

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
Global $sAmount
Func Inputter()
   $sAmount = (InputBox("Test box", "How many times shall I run?", "0", "", -1, -1, 0, 0))
   $sAmount = Number($sAmount)
EndFunc
Func Opera()
   MouseMove(722,407)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(704,403)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(100)
   MouseMove(1181,281)
   MouseClick($MOUSE_CLICK_LEFT)
   MouseMove(838,525)
   MouseClick($MOUSE_CLICK_LEFT)
   Sleep(1000)
   MouseMove(22,52)
   MouseClick($MOUSE_CLICK_LEFT)
EndFunc
Global $count=0
Inputter()
;Local $Timeout = 10
;MsgBox($MB_SYSTEMMODAL,"ETC","This will timeout in 10 seconds.",10)
MouseMove(571,836)
MouseClick($MOUSE_CLICK_LEFT)
;For $i = 1 To $Amount Step 1
 ;  Call(Opera())
;Next
Do
   Opera()
   $count = $count+1
Until $count>=$sAmount

Here comes the explanation to the changes:

  • You defined $sAmount as a Local variable within the Inputter function. Doing so it is available within the function only, so i instead added it as a global variable and referenced it in the function.
  • In you latest post, you commented out your variable definition of $count, so using it in the loop below resulted in a crash. Also using Local outside a function works, but does basically the same as global, except confuse the reader.
  • You used the Call function for function calls. This is not how the Call function works. Your expression within the Call function parentheses was executed the actual call, and the Call function afterwards tried to execute the response as a function, failing silently via the @error flag.
  • And finally i changed your greater than condition to a greater or equal to, as the loop +1 of the requested number given from the input box.

But all in all, i think this is a great start for you :D . You seem to take (constructive) criticism well and the script was 90% working already, nice :D

Hope it helps!

Ah thanks this helps a lot, I was thinking that local just meant that the variable was high priority or something 🤔.

 

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