Jump to content

My script terminates almost instantly


Eggzy
 Share

Recommended Posts

Hi, I'm trying to make a script that can click on a specific color while simulating human-like behavior. The clicking and moving loop toggles with the press of a key.

When I run the script it instantly terminates with no error message. Can anybody help me fix this?

 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.16.1
 Author:         Eggzy

 Script Function:
    Toggles clicking a specific color with a hotkey.

#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
;====================================================
;CHANGE - START
;====================================================
Global $SEARCH_COLOR, $COLOR_VARIATION, $MOVE_TIME, $SEARCH_DELAY, $ALLOWED_APP;
$SEARCH_COLOR       =   "286894" ;Hexadecimal color ID
$COLOR_VARIATION    = 15;
$MOVE_TIME          =   1000;
$SEARCH_DELAY       =   1500    ;Delay for loop in milliseconds (1 sec = 1000 ms) (default = 1)
$ALLOWED_APP            =   "MozillaWindowClass";

;====================================================
;CHANGE - END
;====================================================
; Ease in function
func __calci1($i, $sm)
    return $i ^ $sm;
endFunc

; Ease out function
func __calci2($i, $sm)
    return 1 - ((1 - $i) ^ $sm);
endFunc

; Ease in out function
func __calci($i, $sm)
    if ($i < 0.5) then
        return __calci1($i * 2, $sm) / 2;
    else
        return (__calci2(($i - 0.5) * 2, $sm) / 2) + 0.5;
    endIf
endFunc

; Ease backward function
func __calof($i, $sm)
    if ($i < 0.5) then
        return __calci($i * 2, $sm);
    else
        return __calci((1 - $i) * 2, $sm);
    endIf
endfunc

; MAIN FUNCTION
func mouseMove2($x2, $y2)
    $x1 = mouseGetPos(0);
    $y1 = mouseGetPos(1);
    $xv = random(-100, 100);
    $yv = random(-100, 100);
    $sm = random(1.5, 2.5);
    $m = random(50, 160);
    for $i = 0 to $m
        $ci = __calci($i / $m, $sm);
        $co = __calof($i / $m, $sm);
        $cx = $x1 + (($x2 - $x1) * $ci) + ($xv * $co);
        $cy = $y1 + (($y2 - $y1) * $ci) + ($yv * $co);
        mouseMove ($cx, $cy, 0);
    next
endFunc

; FUNCTION WITH SPEED CONTROL
; MAIN FUNCTION
func mouseMove3($x2, $y2, $m)
    $x1 = mouseGetPos(0);
    $y1 = mouseGetPos(1);
    $xv = random(-100, 100);
    $yv = random(-100, 100);
    $sm = random(1.5, 2.5);
    for $i = 0 to $m
        $ci = __calci($i / $m, $sm);
        $co = __calof($i / $m, $sm);
        $cx = $x1 + (($x2 - $x1) * $ci) + ($xv * $co);
        $cy = $y1 + (($y2 - $y1) * $ci) + ($yv * $co);
        mouseMove ($cx, $cy, 0);
    next
endFunc

; Test Script
;mouseMove3 (512, 386, 1000);
;MsgBox($MB_SYSTEMMODAL, "Title", "Ceci est un message qui se fermera apres 5 sec", 5)

;-------------------------------------------------------------------------------------------------------
;THIS IS THE CODE FOR MAKING OUR HOTKEYS
;-------------------------------------------------------------------------------------------------------
Global $scriptPaused = False;
Global $scriptActive = False;

HotKeySet("p", "StartScript") ; Alt-P
HotKeySet("{PAUSE}", "TogglePause") ;
;HotKeySet("{ESC}", "Terminate")
;-------------------------------------------------------------------------------------------------------
;THIS IS THE CODE FOR LOOPING AND CLICKING ON A SPECIFIC COLOR
;-------------------------------------------------------------------------------------------------------
Func StartScript()
    $scriptActive = Not $scriptActive;
    While (WinActive(String("[CLASS:" & $ALLOWED_APP & "]")) And Not @error And $scriptActive)
        Local $colorCoord   = PixelSearch(0, 0, 20, 300, "0x" & $SEARCH_COLOR, $COLOR_VARIATION);
        Local $foundX       =   $colorCoord[0];
        Local $foundY       =   $colorCoord[1];
        
        If Not @error Then
            mouseMove3($foundX, $foundY, $MOVE_TIME);
            MouseClick($MOUSE_CLICK_LEFT);
            Sleep($SEARCH_DELAY);
        EndIf
    WEnd
EndFunc 

;-------------------------------------------------------------------------------------------------------
;THIS IS THE FUNCTIONS FOR TOGGLING AND STOPPING THE SCRIPT
;-------------------------------------------------------------------------------------------------------
Func TogglePause()
        $scriptPaused = Not $scriptPaused
        While $scriptPaused;
                Sleep(100);
                ToolTip('Script is "Paused"', 0, 0);
        WEnd
        ToolTip("")
EndFunc   ;==>TogglePause

Func Terminate()
        Exit
EndFunc   ;==>Terminate

 

Link to comment
Share on other sites

4 hours ago, ripdad said:

You're not calling any functions.

Also, did you intend to use a main loop?

 

The functions are supposed to be called with the hotkey using HotKeySet.

 

What do you mean by the main Loop : if you mean the while (WinActive....) yes it is intended as it the circle changes place and needs to be clicked on, I want it to repeat as long as the window stays active or the hotkey is pressed again

Link to comment
Share on other sites

add

 

Do
Until GUIGetMsg()=-3

 

i.e.

 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.16.1
 Author:         Eggzy

 Script Function:
    Toggles clicking a specific color with a hotkey.

#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
;====================================================
;CHANGE - START
;====================================================
Global $SEARCH_COLOR, $COLOR_VARIATION, $MOVE_TIME, $SEARCH_DELAY, $ALLOWED_APP;
$SEARCH_COLOR       =   "286894" ;Hexadecimal color ID
$COLOR_VARIATION    = 15;
$MOVE_TIME          =   1000;
$SEARCH_DELAY       =   1500    ;Delay for loop in milliseconds (1 sec = 1000 ms) (default = 1)
$ALLOWED_APP            =   "MozillaWindowClass";

;====================================================
;CHANGE - END
;====================================================
; Ease in function
func __calci1($i, $sm)
    return $i ^ $sm;
endFunc

; Ease out function
func __calci2($i, $sm)
    return 1 - ((1 - $i) ^ $sm);
endFunc

; Ease in out function
func __calci($i, $sm)
    if ($i < 0.5) then
        return __calci1($i * 2, $sm) / 2;
    else
        return (__calci2(($i - 0.5) * 2, $sm) / 2) + 0.5;
    endIf
endFunc

; Ease backward function
func __calof($i, $sm)
    if ($i < 0.5) then
        return __calci($i * 2, $sm);
    else
        return __calci((1 - $i) * 2, $sm);
    endIf
endfunc

; MAIN FUNCTION
func mouseMove2($x2, $y2)
    $x1 = mouseGetPos(0);
    $y1 = mouseGetPos(1);
    $xv = random(-100, 100);
    $yv = random(-100, 100);
    $sm = random(1.5, 2.5);
    $m = random(50, 160);
    for $i = 0 to $m
        $ci = __calci($i / $m, $sm);
        $co = __calof($i / $m, $sm);
        $cx = $x1 + (($x2 - $x1) * $ci) + ($xv * $co);
        $cy = $y1 + (($y2 - $y1) * $ci) + ($yv * $co);
        mouseMove ($cx, $cy, 0);
    next
endFunc

; FUNCTION WITH SPEED CONTROL
; MAIN FUNCTION
func mouseMove3($x2, $y2, $m)
    $x1 = mouseGetPos(0);
    $y1 = mouseGetPos(1);
    $xv = random(-100, 100);
    $yv = random(-100, 100);
    $sm = random(1.5, 2.5);
    for $i = 0 to $m
        $ci = __calci($i / $m, $sm);
        $co = __calof($i / $m, $sm);
        $cx = $x1 + (($x2 - $x1) * $ci) + ($xv * $co);
        $cy = $y1 + (($y2 - $y1) * $ci) + ($yv * $co);
        mouseMove ($cx, $cy, 0);
    next
endFunc

; Test Script
;mouseMove3 (512, 386, 1000);
;MsgBox($MB_SYSTEMMODAL, "Title", "Ceci est un message qui se fermera apres 5 sec", 5)

;-------------------------------------------------------------------------------------------------------
;THIS IS THE CODE FOR MAKING OUR HOTKEYS
;-------------------------------------------------------------------------------------------------------
Global $scriptPaused = False;
Global $scriptActive = False;

HotKeySet("p", "StartScript") ; Alt-P
HotKeySet("{PAUSE}", "TogglePause") ;
;HotKeySet("{ESC}", "Terminate")

Do
Until GUIGetMsg()=-3
;-------------------------------------------------------------------------------------------------------
;THIS IS THE CODE FOR LOOPING AND CLICKING ON A SPECIFIC COLOR
;-------------------------------------------------------------------------------------------------------
Func StartScript()
    $scriptActive = Not $scriptActive;
    While (WinActive(String("[CLASS:" & $ALLOWED_APP & "]")) And Not @error And $scriptActive)
        Local $colorCoord   = PixelSearch(0, 0, 20, 300, "0x" & $SEARCH_COLOR, $COLOR_VARIATION);
        Local $foundX       =   $colorCoord[0];
        Local $foundY       =   $colorCoord[1];
        
        If Not @error Then
            mouseMove3($foundX, $foundY, $MOVE_TIME);
            MouseClick($MOUSE_CLICK_LEFT);
            Sleep($SEARCH_DELAY);
        EndIf
    WEnd
EndFunc 

;-------------------------------------------------------------------------------------------------------
;THIS IS THE FUNCTIONS FOR TOGGLING AND STOPPING THE SCRIPT
;-------------------------------------------------------------------------------------------------------
Func TogglePause()
        $scriptPaused = Not $scriptPaused
        While $scriptPaused;
                Sleep(100);
                ToolTip('Script is "Paused"', 0, 0);
        WEnd
        ToolTip("")
EndFunc   ;==>TogglePause

Func Terminate()
        Exit
EndFunc   ;==>Terminate

 

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