Dragonturtle
Dragonturtle

Reputation: 304

AutoIt script doesn't run

My AutoIt script should do a left click every 40 minutes inside a given time interval:

Func Main()
    Run("kocske.jpg") 
    While 0 < 1
        If CheckTime() == true Then 
            MouseClick("left")
        EndIf
        ; Sleep for 40 minutes
        Sleep(60000 * 40)
    WEnd
EndFunc

    ; The function checks if the current time is between 17:00 and 20:00
Func CheckTime()
    If @Hour >= 17 AND @Hour <= 20 Then
        Return true
    Else
        Return false
    EndIf
EndFunc

I saved it as .au3 file and compiled it to an executable. But when I run it, nothing happens (as if it never started).

I added Run("kocske.jpg") to test if the script starts at all, and placed a JPG file named "kocske.jpg" in the script's folder. It does not open the file, and the task manager does not show it running.

Why doesn't my script run?

Upvotes: 0

Views: 1141

Answers (2)

user4157124
user4157124

Reputation: 2904

  1. Running functions

    Why doesn't my script run?

    Because functions are defined, but not called.

    If you want Main() to be executed then add a line "Main()" outside of function definitions (global scope). Example (first line, as per Documentation - Keyword Reference - Func...Return...EndFunc):

    Main()
    
    Func Main()
        Run("kocske.jpg") 
        While 0 < 1
            If CheckTime() == true Then 
                MouseClick("left")
            EndIf
            ; Sleep for 40 minutes
            Sleep(60000 * 40)
        WEnd
    EndFunc
    
    ; The function checks if the current time is between 17:00 and 20:00
    Func CheckTime()
        If @Hour >= 17 AND @Hour <= 20 Then
            Return true
        Else
            Return false
        EndIf
    EndFunc
    
  2. Opening files

    I added Run("kocske.jpg") to test if the script starts at all …

    As per Documentation - Function Reference - Run():

    Runs an external program.

    "kocske.jpg" is not "an external program"; use ShellExecute("kocske.jpg") instead:

    Runs an external program using the ShellExecute API.

  3. Comparison operator

    There is no differentiation for = -use between assignment and comparison (as per Documentation - Language Reference - Operators). Example:

    ; Equal sign (=) as assignment operator:
    Global Const $g_bValue = True
    
    ; Equal sign (=) as comparison operator:
    If $g_bValue = True Then; Or just: If $g_bValue Then
    
        Beep(500, 1000)
    
    EndIf
    

    As per Documentation - Language Reference - Operators:

    ==

    Tests if two strings are equal. Case sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case sensitive.

Upvotes: 1

Stephan
Stephan

Reputation: 56180

I rewrote your program a bit to include usual habits (commented below)

Main()  ; calls the Main() Function

Func Main()
    ShellExecute("kocske.jpg") ; opens the image with it's default viewer; Note: you should add full path
    While True
        If CheckTime(17, 21) Then   ; good habit to work with parameters; makes your function flexible
            ; probably you want to locate your mouse to a special location before clicking
            ; and also activate a certain application? Consider ControlClick()
            MouseClick("left")
        EndIf
        Sleep(60000 * 40) ; Sleep for 40 minutes
    WEnd
EndFunc   ;==>Main

; The function checks if the current time is between 17:00 and 20:00 (19:59:59)
Func CheckTime($TimeA = 17, $TimeB = 20) ; defines default parameters, if they are not given
    If @HOUR >= $TimeA And @HOUR < $TimeB Then Return True  ; no Else needed
    Return False
EndFunc   ;==>CheckTime

Note: @HOUR < $TimeB instead of @HOUR <= $TimeB

Upvotes: 1

Related Questions