How to implement something like a select-case-esac structure in AutoHotKey

StackOverflow https://stackoverflow.com/questions/22824725

  •  26-06-2023
  •  | 
  •  

質問

I am not sure how to start this one. I went through the help files of AHK with no result.

I need to establish a construct like this (this is not in either UNIX shell or in AHK scripting language. Just shows the idea I am after)

while true
do

gosub screenscrape
; here, all text on page is on clipboard
; variable "input" is a predefined portion of the clipboard

case $input in
string-1)
gosub subroutione1;;
string-2)
gosub subroutine2;;

...

*)
echo "not found"
sleep 1minute

done

to make the things more complex, the parts noted as string-1, through string-n are also variables, read in from few different files, depending on time of the day or by trigger from an event.

can someone point me to the right direction for this ?

役に立ちましたか?

解決

Autohotkey does not have any case statements. What everyone does is to use if and else statements. (I know how it feels. I like case, too)

A few tips for you:

  • You can't have that $ or a - in your variable name in autohotkey
  • ...but you CAN have an underscore.
  • You don't need to terminate your lines with a ;.

Here is a translation into autohotkey for you.

gosub screenscrape
; here, all text on page is on clipboard
; variable "input" is a predefined portion of the clipboard

if(string1){   ;if the variable is not blank it will evaluate to `true`
    gosub subroutine1
}else if(string2){
    gosub subroutine2
}else{
    msgbox not found
    sleep 60000  ;milliseconds
}

Also, you can use real functions in autohotkey - you don't have to rely on gosubs. If you use gosubs, make sure you place a return at the end of them. You should also read the autohotkey docs concerning the "auto execute" section.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top