无法通过Autohotkey评估if语句中的变量

时间:2016-05-26 14:53:14

标签: autohotkey

我试图让Autohotkey评估从剪贴板分配的变量,然后根据变量的内容做一些事情。这是代码:

^j::
    Clipboard := ""           ; Must start off blank for detection to work.
    WinActivate, ahk_exe Excel.exe

    Send, ^c                        ; Copy text into Clipboard
    ClipWait, 2                     ; wait for it to be copied
    Course := Clipboard             ; Fetch the text into variable
    Clipboard := ""                 ; Reset Clipboard

    MsgBox %Course%                 ; To test is Course is "103"

    if ( Course == 103 ) 
    {
        Correct_Num := 105
        Correct_Title := Art Appreciation
    } 

    send, {Tab}
    send, {Tab}
    send, {Tab}
    send, %Correct_Num%
    send, {Tab}
    send, %Correct_Title%

尽管MsgBox显示103,但是if语句没有检测到Course == 103。我已经尝试用引号中的103编写语句,我已经尝试了IfEqual。不知道还能做什么。

2 个答案:

答案 0 :(得分:1)

所有变量(Clipboard,Course,Correct_Num和Correct_Title)必须从空白处开始以便检测才能工作。否则他们的最后一个值仍然在内存中。

^j::
    Course := "" 
    Correct_Num := "" 
    Correct_Title := "" 
    Clipboard := ""         
    WinActivate, ahk_exe Excel.exe
    WinWaitActive, ahk_exe Excel.exe ; important
    Send, ^c                         ; Copy the preselected text
    ClipWait, 2                      ; Wait for the clipboard to contain data
    if (!ErrorLevel)                 ; If NOT ErrorLevel clipwait found data on the clipboard   
    { 
        Course := Trim(Clipboard)        ; Fetch the text into variable (as suggested by PGilm)
        Clipboard := ""                  ; Reset Clipboard
        if (Course == 103) 
        {
            Correct_Num = 105
            Correct_Title = Art Appreciation
        }
        MsgBox %Course%`n%Correct_Num%`n%Correct_Title%
    }
    else
        MsgBox, The attempt to copy text onto the clipboard failed.
return

答案 1 :(得分:1)

根据我的评论:

^j::
    Clipboard := ""           ; Must start off blank for detection to work.
    WinActivate, ahk_exe Excel.exe

    WinWaitActive, ahk_exe Excel.exe ; important

    Send, ^c                         ; Copy text into Clipboard
    ClipWait, 2                      ; wait for it to be copied
    Course := Trim(Clipboard)        ; Fetch the text into variable
    Clipboard := ""                  ; Reset Clipboard

    MsgBox %Course%                  ; To test is Course is "103"

    Correct_Num =                    ; Clear variables per user3419297 so they don't
    Correct_Title =                  ; contain the results from a prior time the equality
                                     ; was satisfied (else you will get the same results
                                     ; even if Course <> 103 since those variables
                                     ; will still contain the values from earlier)

    ifEqual, Course, 103
    {
        MsgBox Course is equal to 103, Hooray!! 
        Correct_Num := 105
        Correct_Title := Art Appreciation
    } 

    send, {Tab}
    send, {Tab}
    send, {Tab}
    send, %Correct_Num%  ;  will be blank (send nothing) if course not 103
    send, {Tab}
    send, %Correct_Title%  ;  will be blank (send nothing) if course not 103

    ;  return  ;  not sure what the rest of your code does

H个