Sign in to follow this  
Followers 0
BlueScreen

"for" Loop Executed In A "while"

9 posts in this topic

I want to perform a certain operation in a loop using "For" function but only if a certain window is active.

$Iterations=1
WinActivate ("blablabla")
While WinActive ("BlaBlaBla")
    For $Loop=1 To $Iterations
  Send ("abcd")
    Next
Wend

Why the "For" loop is not terminated after only 1 iteration? :whistle:

Share this post


Link to post
Share on other sites



Right. So how can I do it right?

Share this post


Link to post
Share on other sites

What are you trying to do? Do you never want to send keystrokes to that window again after the first time (even though it will still be active)? Do you need the while loop or would an IF be correct?

$Iterations=1
WinActivate ("blablabla")
If WinActive ("BlaBlaBla") Then
  For $Loop=1 To $Iterations
   Send ("abcd")
  Next
EndIf

Share this post


Link to post
Share on other sites

Well, the "IF" is not good enoght.

I want to send lots of text to a window but I'm afraid this window will not be in Focus and all the text will be entered elsewhere. So a "While" will give me the constant ability to stop sending the text in a case that the target window is not active. Get it?

Share this post


Link to post
Share on other sites

Hmm, the If still looks OK for me based on that description..

Let's try this way:

1. How many times do you want to send the text?

2. What do you want to happen if the window loses focus? Stop the script? Wait until it's active again? MsgBox error? Nothing? :whistle:

Share this post


Link to post
Share on other sites

Well, I want to send the text 100 times, and when the window gets out of focus, I want the script to resume when the window gets back to focus... :whistle:

Share this post


Link to post
Share on other sites

WinActivate("blablah")
For $Loop = 1 to 100
  While Not WinActive("blablah")
    Sleep(10)
  Wend
  Send("abcd")
Next

Or you could have a line to force the window to be active during the for loop.

Share this post


Link to post
Share on other sites

10x, Jon. It is working well. :whistle:

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  
Followers 0