Search code examples
autoit

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?


Solution

    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.