AutoIt 脚本没有 运行

AutoIt script doesn't run

我的 AutoIt 脚本应该在给定的时间间隔内每 40 分钟执行一次左键单击:

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

我将其保存为.au3 文件并将其编译为可执行文件。但是当我 运行 它时,什么也没有发生(好像它从未开始过)。

我添加了 Run("kocske.jpg") 来测试脚本是否完全启动,并在脚本的文件夹中放置了一个名为 "kocske.jpg" 的 JPG 文件。打不开文件,任务管理器也不显示运行ning.

为什么我的脚本没有 运行?

  1. 运行 函数

    Why doesn't my script run?

    因为定义了函数,但没有调用。

    如果要执行 Main(),则在函数定义(全局范围)之外添加一行“Main()”。示例(第一行,根据 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. 打开文件

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

    根据Documentation - Function Reference - Run()

    Runs an external program.

    "kocske.jpg" 不是“外部程序”;使用 ShellExecute("kocske.jpg") 代替:

    Runs an external program using the ShellExecute API.

  3. 比较运算符

    = 没有区别 - 在赋值和比较之间使用(根据 Documentation - Language Reference - Operators)。示例:

    ; 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
    

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

我稍微重写了你的程序以包含通常的习惯(在下面评论)

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

注意:@HOUR < $TimeB 而不是 @HOUR <= $TimeB