How to Randomize Text in AHK
I am looking to send three different messages every 10 seconds, currently this is what I have but it is not working. As you can see, every 10 seconds I am getting a random int between 1 and 3 and then using if statements to send the corresponding number. Then from there that should send the text.
{
Sleep, 10000 ;
Random, Rand, 1, 3 ;
}
if (Rand == 1)
{
Send {1}
Send {Enter}
}
if (Rand == 2)
{
Send {2}
Send {Enter}
}
if (Rand == 3)
{
Send {3}
Send {Enter}
}
1::
send, test 1
return
2::
send, test 2
3::
send, test 3
Esc::ExitApp
First I don't see that your script is looping, so it runs once and finishes. Then I don't think the bit at the end is needed unless you are specifically wanting to be able to manually press 1, 2 or 3 and have them do something, but that was not mentioned as being your intention.
Try something like this:
stopit = 0
While stopit = 0
{
Loop, 50
{
if stopit = 1
{
ExitApp
}
Random, Rand, 1, 3
if (Rand == 1)
{
Send {1}
Send {Enter}
}
if (Rand == 2)
{
Send {2}
Send {Enter}
}
if (Rand == 3)
{
Send {3}
Send {Enter}
}
Sleep, 5000
}
}
return
F10::
stopit = 1
return
Esc::
ExitApp
To repeat an action periodically (at a specified time interval) you need a timer or a Loop as in the other answer.
AFAIK SetTimer has higher performance than a Loop and consumes less resources.
#NoEnv
#SingleInstance Force
#UseHook
SetTimer, send_random_text, 10000 ; every 10 seconds
Return
send_random_text:
Random, Rand, 1, 3
if (Rand == 1)
GoSub 1 ; jump to this hotkey label (1::)
if (Rand == 2)
GoSub 2
if (Rand == 3)
GoSub 3
Return
1::send, test 1{Enter}
2::send, test 2{Enter}
3::send, test 3{Enter}
Esc::ExitApp