无法让 Autohotkey 评估 if 语句中的变量
Can't get Autohotkey to evaluate variable in an if-statement
我正在尝试让 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
。不知道还能做什么。
所有变量(剪贴板、课程、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
根据我的评论:
^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
Hth
我正在尝试让 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
。不知道还能做什么。
所有变量(剪贴板、课程、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
根据我的评论:
^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
Hth