Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Loop increment


  • Please log in to reply
7 replies to this topic
TheDewd
  • Members
  • 837 posts
  • Last active: Sep 25 2014 01:59 PM
  • Joined: 28 Mar 2010
How can I run a loop and have a number increase by one each time the script is executed in the loop?

Below is some quick code I whipped up to do a task I'm working on. It's not pretty, but it works for my needs.

#NoEnv
SendMode Input
#SingleInstance Force
#Persistent
#Include C:\Program Files\AutoHotkey\Lib\COM.ahk
title = Styles.txt - Notepad++
title2 = 3270 Terminal - mainframe.*********.com (Session A )

Hotkey, ^!Tab, Script
return

Script:
Loop
{
clipboard =
WinActivate, %title%
WinWait, %title%
WinMenuSelectItem, %title%, , Search, Go to...
Send 1{Enter}
Send {Shift Down}{Right 7}{Shift Up}^c
ClipWait
FileDelete, C:\NETMANAG.32\My Scripts\TEMP.scr

FileAppend,
(
Function ( Main )
	Number(timeout)
	String(read_results)
	String(ins_string)
	Number(IsRow)
	Number(IsCol)
	Number(Len)
	Number(rows)
	Number(cols)
	Set(timeout, 30)
	SetCursor (8, 2)
	SendKey("I")
	SendKey("%clipboard%")
	SendKey("<PF8>")
	
	WaitCursor(timeout,  8,  2)
	If($ResultCode = 2)
		Goto (Error)
	Endif
	
	SetCursor (8, 2)
	SendKey("C")
	
	SetCursor (10, 76)
	SendKey("F")
	
	SendKey("<PF5>")
	
	WaitCursor(timeout,  8,  2)
	If($ResultCode = 2)
		Goto (Error)
	Endif
	
	Stop
Error:
	Type("Error")
EndFunction ( Main )
), C:\NETMANAG.32\My Scripts\TEMP.scr

WinActivate, %title2%
WinWait, %title2%

Run, C:\NETMANAG.32\VSP.EXE C:\NETMANAG.32\My Scripts\TEMP.scr

Return
}

If you scroll down through the code, you will see the following:
Script:
Loop
{
clipboard =
WinActivate, %title%
WinWait, %title%
WinMenuSelectItem, %title%, , Search, Go to...
Send 1{Enter}
Send {Shift Down}{Right 7}{Shift Up}^c
ClipWait
FileDelete, C:\NETMANAG.32\My Scripts\TEMP.scr

I need the bold part to increase its number by one each time the loop is ran.

HotKeyIt
  • Moderators
  • 7109 posts
  • Last active: Today, 04:41 AM
  • Joined: 18 Jun 2008
:?:
Loop 10

   Send [color=red]%A_Index%[/color]{Enter}


TheDewd
  • Members
  • 837 posts
  • Last active: Sep 25 2014 01:59 PM
  • Joined: 28 Mar 2010
I think I did it correctly, but it appears my script only will run one time. The loop is not working.

#NoEnv
SendMode Input
#SingleInstance Force
#Persistent
#Include C:\Program Files\AutoHotkey\Lib\COM.ahk
title = Styles.txt - Notepad++
title2 = 3270 Terminal - mainframe.*********.com (Session A )

Hotkey, ^!Tab, Script
return

Script:
Loop, 93
{
clipboard =
WinActivate, %title%
WinWait, %title%
WinMenuSelectItem, %title%, , Search, Go to...
Send %A_Index%{Enter}
Send {Shift Down}{Right 7}{Shift Up}^c
ClipWait
FileDelete, C:\NETMANAG.32\My Scripts\TEMP.scr

FileAppend,
(
Function ( Main )
	Number(timeout)
	String(read_results)
	String(ins_string)
	Number(IsRow)
	Number(IsCol)
	Number(Len)
	Number(rows)
	Number(cols)
	Set(timeout, 30)
	SetCursor (8, 2)
	SendKey("I")
	SendKey("%clipboard%")
	SendKey("<PF8>")
	
	WaitCursor(timeout,  8,  2)
	If($ResultCode = 2)
		Goto (Error)
	Endif
	
	SetCursor (8, 2)
	SendKey("C")
	
	SetCursor (10, 76)
	SendKey("F")
	
	SendKey("<PF5>")
	
	WaitCursor(timeout,  8,  2)
	If($ResultCode = 2)
		Goto (Error)
	Endif
	
	Stop
Error:
	Type("Error")
EndFunction ( Main )
), C:\NETMANAG.32\My Scripts\TEMP.scr

WinActivate, %title2%
WinWait, %title2%

Run, C:\NETMANAG.32\VSP.EXE C:\NETMANAG.32\My Scripts\TEMP.scr
WinWait, %title2%
Return
}


HotKeyIt
  • Moderators
  • 7109 posts
  • Last active: Today, 04:41 AM
  • Joined: 18 Jun 2008
Remove the red part :?:
;..

), C:\NETMANAG.32\My Scripts\TEMP.scr



[color=red]WinActivate, %zodiac%

WinWait, %zodiac%[/color]



Run, C:\NETMANAG.32\VSP.EXE C:\NETMANAG.32\My Scripts\TEMP.scr

WinWait, %zodiac%

Return


TheDewd
  • Members
  • 837 posts
  • Last active: Sep 25 2014 01:59 PM
  • Joined: 28 Mar 2010
I removed the red part but there's something more to it... The script will still only run one time instead of looping. Not sure what else is causing it to only run once.

HotKeyIt
  • Moderators
  • 7109 posts
  • Last active: Today, 04:41 AM
  • Joined: 18 Jun 2008
This must be the problem
Run, C:\NETMANAG.32\VSP.EXE C:\NETMANAG.32\My Scripts\TEMP.scr
[color=red]WinWait, %zodiac%[/color]
Where does it stop? Does it create the file, does it run it?

TheDewd
  • Members
  • 837 posts
  • Last active: Sep 25 2014 01:59 PM
  • Joined: 28 Mar 2010
It runs the file but it never starts over. Anyway, I managed to get it working to a certain extent by removing the trigger hotkey (Ctrl+Alt+Tab) and removing the last return. I think it's okay this way so there's no need to further investigate the problem. Thanks for your help though!

Desi
  • Members
  • 162 posts
  • Last active: Jan 22 2014 09:12 PM
  • Joined: 29 Oct 2010
You may be over-engineering it. Why not just use a counter variable?

; Initialise Counter var. Always a good idea to initialise anyway, I feel.
Counter = 1

Loop
{
	Send %Counter%
	; Increase Counter var by +1.
	Counter += 1
}