2016-05-26 49 views
0

我試圖讓AutoHotkey對從剪貼板分配的變量進行求值,然後根據變量做些什麼。下面的代碼:無法獲取AutoHotkey來評估if語句中的變量

^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% 

if語句沒有檢測到Course == 103儘管顯示103,我試着寫在報價與103的聲明中MSGBOX,我已經試過IfEqual。不知道還有什麼要做。

+0

代碼爲我工作。你必須複製一些不是'103'的東西。也許是在2501或者 – 2501

+0

。當我將變量Course打印到消息框中時,它讀出爲103.AHK沒有辦法調用某個整數嗎?它確實看起來像103 –

+0

事實上,當我在Notepad中註釋掉了'WinActive'並使用'103'時,它確實有效。我會看看是否可以將Excel中的單元格格式化爲數字,但它應該已經是我想的 –

回答

1

所有的變量(剪貼板,課程,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

每我的評論:

^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個

+1

我發現上面的代碼有問題:如果變量「Course」等於「103」,您將獲得其他變量的正確值,並且這些值將保留在腳本的內存中。下次按^ j複製預選文本時,即使變量「課程」不等於「103」,也會得到相同的值。 – user3419297

+1

瞭解,@ user3419297(和我編輯上面相應)。當然,我們不知道OP代碼的其餘部分(不包括「返回」)。除非OP想提供更多/更好的背景。 。 。 – PGilm

+0

啊,所以我的回答並不比你的更好,你是第一個(我看到你編輯了你的答案,以反映我的建議)。我相應地對你進行了UV處理。 。 。如果OP確認'Trim(Clipboard)'使解決方案正常工作(但我想獲得一些代表),可能我可以刪除我的答案。 。 。 – PGilm