Adding timers to a loop Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
eblanc
Posts: 62
Joined: 08 May 2019, 14:41

Adding timers to a loop

17 Jun 2019, 18:28

I need to figure out two timers.

1. End timer, break the loop and continue the script
2. End timer, send Ctrl+W and go to end label

I've marked where I need them with the number symbol and a comment.

I tried using a tickount but it was a fail for me, not sure what I was doing wrong.

Code: Select all

!a::
inputbox, userinput, Automated Mac Address, How many Mac Addresses in Sequence am I updating?,  ,400, 130
If ErrorLevel 
		Return
Else
loop, %userinput%
{
	;Is web page loaded?
	sleep, 200
	;##NEED A 45 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
	loop
		{ 
		sleep, 100
		PixelGetColor, pColor, 465, 599, RGB
		if (pColor = "0x31508B") 
			{
			break
			}
		}
	sleep,200
	loop, 5
	{
		send, {tab}
		sleep, 200
	}
	;Copy IP ADDRESS
	send, ^c
	clipwait
	sleep, 200
	send, ^t
	sleep, 200
	send, ^v
	sleep, 200
	Send, {enter}
	;Check if Camera menu is loaded
	;##NEED A 45 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
	loop
		{ 
		sleep, 500
		PixelGetColor,pColor,54,133,RGB
		if (pColor = "0xF0F0F0") 
			{
			break
			}

		}
	;Camera is Active
	mousemove, 291, 61
	sleep, 200
	mouseclick, left
	sleep, 150
	mouseclick, left
	sleep, 150
	mouseclick, left
	sleep, 150
	sleep, 500
	send, ^c
	clipwait
	send, {esc}
	;Making sure camera  window is closed properly. 
	;##NEED A 10 SEC TIMER IN CASE THERE'S ONLY 1 WINDOW BOX##
	loop
		{ 
		sleep, 500
		PixelGetColor,pColor,54,133,RGB
		if (pColor = "0xF0F0F0") 
			{
			break
			}
		}
	send, {esc}
	sleep, 500
	send, {esc}
	sleep, 200
	send, ^w
	;closed camera window
	mousemove, 1068, 411
	sleep, 200
	mouseclick, left
	Loop, 30
		{
		send, {tab}
		sleep, 100
		}
	;Entering Mac ADDRESS
	mac:=Substr(clipboard, -12, 12)
	mac2 := RegExReplace(MAC, "\w{2}(?!$)", "$0:")
	send, %mac2%
	sleep, 500
	loop, 12
		{
		send, {tab}
		sleep, 100
		}
	sleep, 200
	FormatTime, CurrentDateTime,, MM/dd/yyyy
	send, %CurrentDateTime%, -EB-{space}Mac Address Updated{tab}{space}
	;Checking Message box popped up																			
	loop
	;##NEED A 10 SECOND TIMER##
	{ 
		mousemove,890,600
		sleep, 500
		PixelGetColor,pColor,890,600,RGB
		if (pColor = "0xE8E8E8") 
			{
			break
		}
	}
	sleep, 800
	send, {space}
	sleep, 600
	;Restart Script
	send, ^w
	sleep, 200
	;FIRST TIME MAKING A LABEL
	LABELEND:
}
msgbox, Process Finished.
return

Rohwedder
Posts: 2049
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Adding timers to a loop

18 Jun 2019, 00:32

Hallo,
Timers cannot break loops! Timers interrupt loops.
However, a loop can breaks itself with Break:

Code: Select all

[code]SetTimer, Break, 4000
Loop
{
	Loop
	{
		IF Break
			Break ;inner loop breaks itself
		ToolTip,% A_Index
		Sleep, 100
	}
	ToolTip, Break
	Break := False
	Sleep, 500
}
Break:
Break := True
Return
swagfag
Posts: 2722
Joined: 11 Jan 2017, 17:59

Re: Adding timers to a loop

18 Jun 2019, 03:04

Code: Select all

...
...
;Is web page loaded?
sleep, 200
;##NEED A 45 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
if !pColorFound()
{
	send ^w
	goto LABELEND
}
sleep,200
loop, 5
{
	send, {tab}
...
...

pColorFound() {
	start := A_TickCount

	while (A_TickCount - start < 45000)
	{
		sleep, 100
		PixelGetColor, pColor, 465, 599, RGB
		if (pColor = 0x31508B) 
			return true
	}

	return false
}
just me
Posts: 6425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Adding timers to a loop

18 Jun 2019, 05:18

... or check the time within the loop:

Code: Select all

!a::
inputbox, userinput, Automated Mac Address, How many Mac Addresses in Sequence am I updating?,  ,400, 130
If ErrorLevel
   Return
; Else <<<<< not needed
Loop, %userinput%
{
   ;Is web page loaded?
   sleep, 200
   ;##NEED A 45 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
   Found := False
   EndTime := A_TickCount + 45000
   While (A_TickCount < EndTime)
   {
      Sleep, 100
      PixelGetColor, pColor, 465, 599, RGB
      If (pColor = "0x31508B")
      {
         Found := True
         Break
      }
   }
   If (Found = False)
   {
      Send, ^w
      Sleep, 200
      Continue
   }
   ...
User avatar
eblanc
Posts: 62
Joined: 08 May 2019, 14:41

Re: Adding timers to a loop

18 Jun 2019, 14:44

just me wrote:
18 Jun 2019, 05:18
... or check the time within the loop:

Code: Select all

!a::
inputbox, userinput, Automated Mac Address, How many Mac Addresses in Sequence am I updating?,  ,400, 130
If ErrorLevel
   Return
; Else <<<<< not needed
Loop, %userinput%
{
   ;Is web page loaded?
   sleep, 200
   ;##NEED A 45 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
   Found := False
   EndTime := A_TickCount + 45000
   While (A_TickCount < EndTime)
   {
      Sleep, 100
      PixelGetColor, pColor, 465, 599, RGB
      If (pColor = "0x31508B")
      {
         Found := True
         Break
      }
   }
   If (Found = False)
   {
      Send, ^w
      Sleep, 200
      Continue
   }
   ...
This worked. well. Makes my script work. However, something really odd happens.

at line 25, when page times out I don't even get to the message box, it closes my entire application (firefox) it should just close 1 tab go to label end, and restart loop. Instead, it closes all my tabs and application and it keeps running (on whatever I have open at the time)

Any idea why this might be?

here is my code, which by the way, thanks! I'm maximizing my work by almost 1000%

Code: Select all

!a::
inputbox, userinput, Automated Mac Address, How many Mac Addresses in Sequence am I updating?,  ,400, 130
If ErrorLevel 
		Return
Else
loop, %userinput%
{
	;Is web page loaded?
	sleep, 200
	;##NEED A 15 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
	 Found := False
   EndTime := A_TickCount + 15000
   While (A_TickCount < EndTime)
   {
      Sleep, 100
      PixelGetColor, pColor, 465, 599, RGB
      If (pColor = "0x31508B")
      {
         Found := True
         Break
      }
   }
   If (Found = False)
   {
      Sleep, 200
	  send, {escape}
	  msgbox, camera was timed out
      GOTO LABELEND
   }
	sleep,200
	loop, 5
	{
		send, {tab}
		sleep, 200
	}
	;Copy IP ADDRESS
	send, ^c
	clipwait
	sleep, 200
	send, ^t
	sleep, 200
	send, ^v
	sleep, 200
	Send, {enter}
	;Check if Camera menu is loaded
	;##NEED A 30 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
	Found := False
	EndTime := A_TickCount + 30000
	While (A_TickCount < EndTime)
		{ 
		sleep, 500
		PixelGetColor,pColor,54,133,RGB
		if (pColor = "0xF0F0F0") 
			{
			found := True
			break
			}
		}
		if (found = False)
		{
		send, {escape}
		sleep, 200
		send, ^W
		sleep, 200
		GOTO LABELEND
		}
	;Camera is Active
	mousemove, 291, 61
	sleep, 200
	mouseclick, left
	sleep, 150
	mouseclick, left
	sleep, 150
	mouseclick, left
	sleep, 150
	sleep, 500
	send, ^c
	clipwait
	send, {esc}
	;Making sure camera  window is closed properly. 
	;##NEED A 10 SEC TIMER IN CASE THERE'S ONLY 1 WINDOW BOX##
	loop
		{ 
		sleep, 500
		PixelGetColor,pColor,54,133,RGB
		if (pColor = "0xF0F0F0") 
			{
			Break
			}
		else
			{
			sleep, 1000
			break
			}
		}
	
	send, {esc}
	sleep, 500
	send, {esc}
	sleep, 200
	send, ^w
	;closed camera window
	mousemove, 1068, 411
	sleep, 200
	mouseclick, left
	Loop, 30
		{
		send, {tab}
		sleep, 100
		}
	;Entering Mac ADDRESS
	mac:=Substr(clipboard, -12, 12)
	mac2 := RegExReplace(MAC, "\w{2}(?!$)", "$0:")
	send, %mac2%
	sleep, 500
	loop, 12
		{
		send, {tab}
		sleep, 100
		}
	sleep, 200
	FormatTime, CurrentDateTime,, MM/dd/yyyy
	send, %CurrentDateTime%, -EB-{space}Mac Address Updated{tab}{space}
	;Checking Message box popped up																			
	;##NEED A 10 SECOND TIMER##
	loop, 20
	{ 
		mousemove,890,600
		sleep, 500
		PixelGetColor,pColor,890,600,RGB
		;msgbox, %pcolor%
		if (pColor = "0xE8E8E8") 
			{
			break
		}
	}
	sleep, 800
	send, {space}
	sleep, 600
	;Restart Script
	LABELEND:
	send, ^w
	sleep, 200
	
	}
msgbox, Process Finished.
return
just me
Posts: 6425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Adding timers to a loop

19 Jun 2019, 04:14

Hi,

I see no reason why the MsgBox shouldn't show. Maybe it's somewhere in the background?

Also, your original request:

Code: Select all

	;##NEED A ?? SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
Your actual code at line 23 and below:

Code: Select all

   If (Found = False)
   {
      Sleep, 200
      send, {escape}
      msgbox, camera was timed out
      GOTO LABELEND
   }
   ...
   ...
   ;Restart Script <<<<< actually: Continue the loop
   LABELEND:
   send, ^w
   sleep, 200
}
Do you really want to continue the main loop in this case after sending Esc and Ctrl+w?
User avatar
eblanc
Posts: 62
Joined: 08 May 2019, 14:41

Re: Adding timers to a loop

19 Jun 2019, 10:41

just me wrote:
19 Jun 2019, 04:14
Hi,

I see no reason why the MsgBox shouldn't show. Maybe it's somewhere in the background?

Also, your original request:

Code: Select all

	;##NEED A ?? SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
Your actual code at line 23 and below:

Code: Select all

   If (Found = False)
   {
      Sleep, 200
      send, {escape}
      msgbox, camera was timed out
      GOTO LABELEND
   }
   ...
   ...
   ;Restart Script <<<<< actually: Continue the loop
   LABELEND:
   send, ^w
   sleep, 200
}
Do you really want to continue the main loop in this case after sending Esc and Ctrl+w?
Yes I want to continue, basically I'm automating a 20-50 pages at a time, So I'll put the user input to that number of pages, if one of them fails (by not being able to connect to the camera which happens, I would like it to cancel the scrip, close the window, restart and still count it as a loop so that my count doesn't change. That way I can just leave the script running while I go eat some grapes or some other fancy thing to symbolize me not having to be sitting at the computer, haha.

Right now, I modified the script to avoid time out, and I'll sit next to the computer and re-start the script manually. Because the time out feature will close all my active firefox windows. It's really odd. not sure what's causing it.
just me
Posts: 6425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Adding timers to a loop  Topic is solved

20 Jun 2019, 05:00

After the first inner loop 'timed out', which action should change the conditions when the outer loop continues?
What is Send, {escape} intended to do?
What is Send, ^w intended to do (if sent to a firefox window with only one tab, it will close the whole window)?
User avatar
eblanc
Posts: 62
Joined: 08 May 2019, 14:41

Re: Adding timers to a loop

20 Jun 2019, 11:48

just me wrote:
20 Jun 2019, 05:00
After the first inner loop 'timed out', which action should change the conditions when the outer loop continues?
What is Send, {escape} intended to do?
What is Send, ^w intended to do (if sent to a firefox window with only one tab, it will close the whole window)?
Hi Just me,

Escape is intended to stop the page from loading. (I guess I could remove it)
and Ctrl + W is intended to close just 1 tab.

in fact it works perfectly in all other instances, when at the end of the loop. it just doesn't work with page time out.

I'm gonna remove that escape, see if that makes any difference. If it works I'll report back.
User avatar
eblanc
Posts: 62
Joined: 08 May 2019, 14:41

Re: Adding timers to a loop

20 Jun 2019, 12:12

I removed Escape, and changed te firefox settings to NOT close multiple tabs.

I found that it's trying to close multiple tabs, so it must be a command. I'm inadvertently using? I'm gonna spam msgbox in my code until I find where exactly is stopping
User avatar
eblanc
Posts: 62
Joined: 08 May 2019, 14:41

Re: Adding timers to a loop

20 Jun 2019, 14:02

Found IT! ! !

It was that send, ^w was different from send, ^W (Capital W closes all windows, duh....)

Now it works!! ! !

fixed a few things like adding a label if it does recognize the page is loaded

Code: Select all

^+m::
inputbox, userinput, Automated Mac Address, How many Mac Addresses in Sequence am I updating?,  ,400, 130
If ErrorLevel 
		Return
Else
loop, %userinput%
{
	;Is web page loaded?
	sleep, 200
	;##NEED A 15 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
	 Found := False
   EndTime := A_TickCount + 15000
   While (A_TickCount < EndTime)
   {
      Sleep, 100
      PixelGetColor, pColor, 465, 598, RGB
      If (pColor = "0x31508B")
      {
         Found := True
         Break
      }
   }
   If (Found = False)
   {
      Sleep, 200
	  send, {escape}
	  msgbox, camera was timed out
      GOTO LABELEND
   }
	sleep,200
	loop, 5
	{
		send, {tab}
		sleep, 200
	}
	;Copy IP ADDRESS
	send, ^c
	clipwait
	sleep, 200
	send, ^t
	sleep, 200
	send, ^v
	sleep, 200
	Send, {enter}
	;Check if Camera menu is loaded
	;##NEED A 30 SECOND TIMER, IF PCOLOR NOT FOUND THEN SEND CTRL + W AND GOTO "LABEL END" (TO RESTART LOOP AND COUNT -1 LOOP)###
	;loop
	Found := False
	EndTime := A_TickCount + 30000
	While (A_TickCount < EndTime)
		{ 
		sleep, 500
		PixelGetColor,pColor,54,133,RGB
		if (pColor = "0xF0F0F0") 
			{
			found := True
			GOTO LABELFOUND
			}
		}
		if (found := False)
		{
		send, ^w
		sleep, 1000
		GOTO LABELEND
		}
	LabelFound:
	;Camera is Active
	mousemove, 291, 61
	sleep, 200
	mouseclick, left
	sleep, 150
	mouseclick, left
	sleep, 150
	mouseclick, left
	sleep, 150
	sleep, 500
	send, ^c
	clipwait
	send, {esc}
	;Making sure camera  window is closed properly. 
	;##NEED A 10 SEC TIMER IN CASE THERE'S ONLY 1 WINDOW BOX##
	loop
		{ 
		sleep, 500
		PixelGetColor,pColor,54,133,RGB
		if (pColor = "0xF0F0F0") 
			{
			Break
			}
		else
			{
			sleep, 1000
			break
			}
		}
	
	send, {esc}
	sleep, 500
	send, {esc}
	sleep, 200
	send, ^w
	;closed camera window
	mousemove, 1068, 411
	sleep, 200
	mouseclick, left
	Loop, 30
		{
		send, {tab}
		sleep, 100
		}
	;Entering Mac ADDRESS
	mac:=Substr(clipboard, -12, 12)
	mac2 := RegExReplace(MAC, "\w{2}(?!$)", "$0:")
	send, %mac2%
	sleep, 500
	loop, 12
		{
		send, {tab}
		sleep, 100
		}
	sleep, 200
	FormatTime, CurrentDateTime,, MM/dd/yyyy
	send, %CurrentDateTime%, -EB-{space}Mac Address Updated{tab}{space}
	;Checking Message box popped up																			
	;##NEED A 10 SECOND TIMER##
	loop, 20
	{ 
		mousemove,890,600
		sleep, 500
		PixelGetColor,pColor,890,600,RGB
		;msgbox, %pcolor%
		if (pColor = "0xE8E8E8") 
			{
			break
		}
	}
	sleep, 800
	send, {space}
	sleep, 600
	;Restart Script
	LABELEND:
	send, ^w
	sleep, 200
		
	}
msgbox, Process Finished.
return

Return to “Ask For Help”

Who is online

Users browsing this forum: RazorHalo, Xtra and 144 guests