Finish loop before next command

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lucaso3live
Posts: 2
Joined: 17 May 2021, 10:30

Finish loop before next command

Post by lucaso3live » 17 May 2021, 10:54

Hi everyone, I'm new with AutoHotKey, but it seems my issue is a well known one without real documented solution (at least with my google skills): how to ensure a script proceed to complete a full loop first before triggering the next command.


For information my code is something like this :

Code: Select all

Loop, 3
{
  Send, +{End}^c{ClipWait, 1}{Clip%A_Index% := ClipBoardAll}
  Send, +{Tab}+{End}^c{ClipWait, 1}{Clip%A_Index% := ClipBoardAll}
  Send, +{Tab}+{End}^c{ClipWait, 1}{Clip%A_Index% := ClipBoardAll}
  Send, {down}{SetKeyDelay, 10, 10}
  ... (et caetera)
}
Msgbox, Success!
Return
But of course, the Msgbox is triggered almost at the beginning of the loop.
I tried implementing an if statement as below, but it doesn't work much :

Code: Select all

counter = 0
Loop, 3
{
  Send, +{End}^c{ClipWait, 1}{Clip%A_Index% := ClipBoardAll}
  Send, +{Tab}+{End}^c{ClipWait, 1}{Clip%A_Index% := ClipBoardAll}
  Send, +{Tab}+{End}^c{ClipWait, 1}{Clip%A_Index% := ClipBoardAll}
  Send, {down}{SetKeyDelay, 10, 10}
  ... (et caetera)
  counter+=1
}
if (counter = 3) { ;the same as the loop reiteration
MsgBox, Success!
}
Setting a long sleep (Sleep, 5000) before the MsgBox kinda works, but it is not ideal as processing the loop would take different time depending on the subjet. And for the actual script behind MsgBox, my script is supposed to copy datas from a word file (the loop), change to another app (the msgbox) and copy all datas to the other app (a 3rd script)

I'm pretty sure there is an easy solution on this but the love of me I cannot get a clue >_<
User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Finish loop before next command

Post by mikeyww » 17 May 2021, 11:01

Debugging this is straightforward, because you can insert other commands to show you what is happening along the way. Send executes quickly, but you cannot forge your own syntax and insert commands inside braces and hope that it will work. Put commands on their own lines (usually). Example.

"Etc" might be misleading as a way to convey a problem with your script, because if it contains a Break, then the loop will be interrupted. That is just one example.
Last edited by mikeyww on 17 May 2021, 11:05, edited 1 time in total.
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Finish loop before next command

Post by Rohwedder » 17 May 2021, 11:03

Hallo,
your code should look more like this:

Code: Select all

Loop, 3
{
  Send, +{End}^c
  ClipWait, 1
  Clip%A_Index% := ClipBoardAll
  Send, +{Tab}+{End}^c
  ClipWait, 1
  Clip%A_Index% := ClipBoardAll
  Send, +{Tab}+{End}^c
  ClipWait, 1
  Clip%A_Index% := ClipBoardAll
  Send, {down}
  SetKeyDelay, 10, 10
  ;... (et caetera)
}
But it still doesn't make sense to me! The variable Clip%A_Index% will be overwritten twice each time
lucaso3live
Posts: 2
Joined: 17 May 2021, 10:30

Re: Finish loop before next command

Post by lucaso3live » 17 May 2021, 12:55

Thank you both for the tip. I wrote the code that way because it allowed me to follow the process with more ease, but I changed it back to what you suggested and it is actually considerably faster.
Otherwise, on the following code:

Code: Select all

Send, +{End}^c
ClipWait, 1
Clip%A_Index% := ClipBoardAll
Send, +{Tab}+{End}^c
ClipWait, 1
Clip%A_Index% := ClipBoardAll
That idea is that in a table, Word copy a data, which is stored in a variable Clip1, then move to the next cell, copy the next data, which is stored in a variable Clip2, and so on.
Then, normally, on the new app, I would apply this next script :

Code: Select all

Loop, 
{
ClipBoard := Clip%A_Index%	
Sleep, 150
Send, ^v
Sleep, 150
Send, {Down}	
}
 
Which is supposed to set Clip1 to the clipboard, paste it, set Clip2 to the clipboard, paste it, and so on.
It is highly likely that my script is far from ideal... but it somewhat works aside from the main issue of this thread. :)
User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Finish loop before next command

Post by mikeyww » 17 May 2021, 13:11

A_Index is constant throughout a single iteration of the loop. I think the point was that if you are not doing anything with the clipboard before the next copy event, then the point of the copying is not clear.

ClipWait has no effect unless the clipboard is cleared first. In many cases of rapid clipping sequences, the clipping will fail without a proper ClipWait or comparable wait of some kind, because the clipboard is somewhat slow in many instances.
Post Reply

Return to “Ask for Help (v1)”