Jump to content

Newbie Guide to Functions


Guest dean_mck
 Share

Recommended Posts

Guest dean_mck

I'm way new to scripting period....the reason I checked out AutoIt to begin with was I was playing a game online and want to script multiple mouse clicks, about 500 or so. Not only that I want the clicks to be at random intervals. So here's my code.

Opt("MouseCoordMode", 0)       ;1=absolute, 0=relative
Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=...
Opt("SendKeyDelay", 1)       ;5 milliseconds

; Get Number of times to click
$times=InputBox("Get Ready To Click","How many times do you want to click?",500)
Run("C:\Program Files\Mozilla Firefox\firefox.exe","",@SW_MAXIMIZE) ;launch Firefox
WinWait("Firefox");wait for the window to load and then 2 seconds
Sleep(2000)
MouseMove(285,67);move the mouse to click the Address bar and wait half a second
MouseClick("left")
Sleep(500)
Send("http://dontwantthemfindingmeoutforcheating.com{ENTER}");Type the URL and hit enter, wait 2 seconds
Sleep(2000)
MouseMove(755,396);move mouse to the login button, and then click.  Wait 2 seconds
MouseClick("left")
Sleep(2000)
MouseMove(318,586);move mouse to Visit CZones and then click. Wait a second
MouseClick("left")
Sleep(1000)
MouseMove(841,663);move mouse to one of the featured zones and click. 
MouseClick("left")
Sleep(3000)     ;wait 3 seconds for zone to load
MouseMove(619,523);move mouse to Random button
$i = 0;set variable - only for counting         
Do
    $ran=Random(3000,10000) ;create random number for pause 7543.3434432
    $wait=round($ran)       ;round the number - used for sleep              7543
    $time=$wait/1000        ;divide whole number by 1000 to get number of seconds we're waiting 7.543
    $pause=round($time)     ;round time to whole number-
    $msgdelay=$pause-1.5    ;set wait time for msgbox - Equal to $wait minus 1500ms - 7043
    MouseClick("left")      ;Finally - click the random button
    $i = $i+1               ;increase the counter by one
    MsgBox(0,"waiting...", $pause &" seconds between clicks. This is click " & $i & " of " & $times, 3)
    Sleep($wait)
Until $i=$times
MsgBox(0, "Macro Complete", "This macro ran and clicked "& $times &" times.")

I'm thinking that the loop part could be created as a function, but everytime I try to do it it doesn't seem to work... here's what I tried...

Do
      Click()
Until $i=$times

Func Click
     $ran=Random(3000,10000)    ;create random number for pause 7543.3434432
    $wait=round($ran)       ;round the number - used for sleep              7543
    $time=$wait/1000        ;divide whole number by 1000 to get number of seconds we're waiting 7.543
    $pause=round($time)     ;round time to whole number-
    $msgdelay=$pause-1.5    ;set wait time for msgbox - Equal to $wait minus 1500ms - 7043
    MouseClick("left")      ;Finally - click the random button
    $i = $i+1               ;increase the counter by one
    MsgBox(0,"waiting...", $pause &" seconds between clicks. This is click " & $i & " of " & $times, 3)
    Sleep($wait)
EndFunc

but that doesn't seem to work.

Also the message box that displays the number of seconds between clicks, I'd really like to delay to the variable $msgdelay, but when I put the variable $msgdelay the status window never goes away.

Sorry about all this... I'm sure my code is a mess, but it's my first attempt, and I've had no help at all.

Thanks in advance.

Link to comment
Share on other sites

I'm thinking that the loop part could be created as a function, but everytime I try to do it it doesn't seem to work... here's what I tried...

...

but that doesn't seem to work.

Also the message box that displays the number of seconds between clicks, I'd really like to delay to the variable $msgdelay, but when I put the variable $msgdelay the status window never goes away. 

<{POST_SNAPBACK}>

A function is a good idea. You just had a few bugs in it. $msgdelay worked for me, but do you want the sleep after the msgbox pause? Here's your code with my additions and corrections. Compare it to yours (it's very close to the same). Do you realize the mouse click will be wherever the mouse happens to be? If this is not what you want, you need to add the x & y options to MouseClick.

If you are not using the free SciTE editor, I highly recommend it (search this forum for a link to it). It can check your code and show you exactly where the problem is. And it has a lot of other AutoIt specific features as well. Not bad for being new to scripting. Keep going.

$times=InputBox("Get Ready To Click","How many times do you want to click?",500)
$i = 0
Do
   Click ()
Until $i = $times
Exit

Func Click()
   $ran = Random(3000, 10000);create random number for pause 7543.3434432
   $wait = Round($ran);round the number - used for sleep 7543
   $time = $wait / 1000;divide whole number by 1000 to get number of seconds we're waiting 7.543
   $pause = Round($time);round time to whole number-
   $msgdelay = $pause - 1.5;set wait time for msgbox - Equal to $wait minus 1500ms - 7043
   MouseClick("left");Finally - click the random button
   $i = $i + 1;increase the counter by one
   MsgBox(0, "waiting...", $pause & " seconds between clicks. This is click " & $i & " of " & $times, $msgdelay)
;;; do you really want this sleep plus the msgbox delay?
   Sleep($wait)
EndFunc  ;==>

Phillip

Link to comment
Share on other sites

Guest dean_mck

A function is a good idea.  You just had a few bugs in it.  $msgdelay worked for me, but do you want the sleep after the msgbox pause? Here's your code with my additions and corrections.  Compare it to yours (it's very close to the same).  Do you realize the mouse click will be wherever the mouse happens to be?  If this is not what you want, you need to add the x & y options to MouseClick.

The idea about the msgbox pause is that I wanted a status of what is happening while it's counting. I wanted the message box to disappear 1.5 seconds before the next click occured.

Regarding the mouse clicks...The mouse needs to repeatedly click on the same spot. So the mouse moves to the spot it's gonna click on just before the loop.

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