Jump to content
Sign in to follow this  
kjpolker

Loop segment for selected number of times?

Recommended Posts

kjpolker

Okay now I was wondering if it is possible to loop just a select few of lines over and over for a selected amount of times (number is chosen in input box).

For an example imagine I have this small script:

#include <GUIConstants.au3>

$Main = GUICreate("Example", 117, 117, 303, 219)
$Input1 = GUICtrlCreateInput("0", 40, 40, 33, 21)
$OK = GUICtrlCreateButton("OK", 40, 64, 33, 17, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            WinActivate('[TITLE:Untitled - Notepad]')
            Send('This is a test')
    EndSwitch
WEnd

Okay now that is as far as I can go, now what I want this script to do is that the line that writes a sentance to notepad, gets repeated the amount of times in the input box. So lets say I insert the number "10", I hit the "OK" button and notepad pops up and types out the line "This is a test" 10 times. How can I do this? Thanks!

Edited by kjpolker

Share this post


Link to post
Share on other sites
Quual

There are different types of loops here are 3 basic versions

the While Wend loop

$i = 0
While $i <= $Input1
    Send('This is a test')
    $i += 1
WEnd

the Do Until loop

$i = 0
Do
    Send('This is a test')
    $i += 1
Until $i = $Input1

the For Next Loop

For $i = 1 To $Input1
    Send('This is a test')
Next

Share this post


Link to post
Share on other sites
kjpolker

I wasn't able to find out how to get this to work. I threw the loop script in the while and now when I launch the program it just spams the 'This is a test' in the input box.

Share this post


Link to post
Share on other sites
jebus495

I wasn't able to find out how to get this to work. I threw the loop script in the while and now when I launch the program it just spams the 'This is a test' in the input box.

Local $banana

Do
$banana += 1
MsgBox("", "bananaz", "you have " & $banana & " bananaz")
Until $banana > 9

^ Example

Here's what I would prolly do in your case.

#include <GUIConstants.au3>

$Main = GUICreate("Example", 117, 117, 303, 219)
$Input1 = GUICtrlCreateInput("0", 40, 40, 33, 21)
$OK = GUICtrlCreateButton("OK", 40, 64, 33, 17, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            WinActivate('[TITLE:Untitled - Notepad]')
            WinWaitActive('[TITLE:Untitled - Notepad]') ;Waits for Notepad to be active.
            sendsomecrap() ;loop function
    EndSwitch
WEnd

Func sendsomecrap()
Local $times ;declare this
Do
$times += 1 ;adds 1 to $times every time it loops.
Sleep(100) ;don't wanna go too fast eh
Send("This is a test" & @LF) ;@LF will put each "this is a test" on a new line
Until $times = GUICtrlRead($Input1) ;compares $times to the value in the input box. when they are equal the loop will stop.
EndFunc
Edited by jebus495

Share this post


Link to post
Share on other sites
kjpolker

Local $banana

Do
$banana += 1
MsgBox("", "bananaz", "you have " & $banana & " bananaz")
Until $banana > 9

^ Example

Here's what I would prolly do in your case.

#include <GUIConstants.au3>

$Main = GUICreate("Example", 117, 117, 303, 219)
$Input1 = GUICtrlCreateInput("0", 40, 40, 33, 21)
$OK = GUICtrlCreateButton("OK", 40, 64, 33, 17, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            WinActivate('[TITLE:Untitled - Notepad]')
            WinWaitActive('[TITLE:Untitled - Notepad]') ;Waits for Notepad to be active.
            sendsomecrap() ;loop function
    EndSwitch
WEnd

Func sendsomecrap()
Local $times ;declare this
Do
$times += 1 ;adds 1 to $times every time it loops.
Sleep(100) ;don't wanna go too fast eh
Send("This is a test" & @LF) ;@LF will put each "this is a test" on a new line
Until $times = GUICtrlRead($Input1) ;compares $times to the value in the input box. when they are equal the loop will stop.
EndFunc

Thank you so much! my project is finally complete!!! I appreciate it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×