AutoIt脚本不运行

时间:2018-01-23 17:14:54

标签: autoit

我的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")来测试脚本是否开始,并放置一个名为&#34的JPG文件; kocske.jpg&#34;在脚本的文件夹中。它不会打开文件,任务管理器也不会显示它正在运行。

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

2 个答案:

答案 0 :(得分:1)

  1. 运行功能

      

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

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

    如果您希望执行Main(),请添加一行&#34; Main()&#34;在函数定义之外(全局范围)。示例(第一行,根据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. 打开文件

      

    我添加Run("kocske.jpg")来测试脚本是否开始...

    根据Documentation - Function Reference - Run()

      

    运行外部程序。

    &#34; kocske.jpg&#34;不是&#34; 外部程序&#34 ;;改为使用ShellExecute("kocske.jpg")

      

    使用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

      

    ==

    测试两个字符串是否相等。 区分大小写。如果左侧和右侧值不是字符串,则它们将转换为字符串。只有在字符串比较需要区分大小写时才应使用此运算符。

答案 1 :(得分:1)

我重写了你的程序,包括通常的习惯(下面评论)

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

相关问题