Friday, April 27, 2018

windows 7 - AutoHotKey: How do I set the starting value of the current loop iteration?


I've made a basic AHK script that adds unique metadata to each file on a system I use every day. The idea is to add _01 to the first file followed by _02 to the second file and going up forever until i exit the script.


At first, i felt like I've been doing this the long way and I wasn't sure what was required to make the script smaller and less painful to edit.


Here's the script:


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
F3::
{
Send, {LAlt}
Sleep, 20
Send, {Right}
Sleep, 200
Send, {Right}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 107, 30
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 346, 38
Sleep, 200
Send, ^v
Sleep, 100
Send, _01
Sleep, 200
Send, {Enter}
Sleep, 200
Send, {Enter}
Sleep, 200
MouseClick, Left, 91, 102
Sleep, 100
MouseClick, Left, 92, 100
Sleep, 100
MouseClick, Left, 147, 156
Sleep, 100
Send, {Down}
Send, {LAlt}
Sleep, 20
Send, {Right}
Sleep, 200
Send, {Right}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 107, 30
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 346, 38
Sleep, 200
Send, ^v
Sleep, 100
Send, _02
Sleep, 200
Send, {Enter}
Sleep, 200
Send, {Enter}
Sleep, 200
MouseClick, Left, 91, 102
Sleep, 100
MouseClick, Left, 92, 100
Sleep, 100
MouseClick, Left, 147, 156
Sleep, 100
Send, {Down}
Send, {LAlt}
Sleep, 20
Send, {Right}
Sleep, 200
Send, {Right}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 107, 30
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 346, 38
Sleep, 200
Send, ^v
Sleep, 100
Send, _03
Sleep, 200
Send, {Enter}
Sleep, 200
Send, {Enter}
Sleep, 200
MouseClick, Left, 91, 102
Sleep, 100
MouseClick, Left, 92, 100
Sleep, 100
MouseClick, Left, 147, 156
Sleep, 100
Send, {Down}
Send, {LAlt}
Sleep, 20
Send, {Right}
Sleep, 200
Send, {Right}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 107, 30
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 346, 38
Sleep, 200
Send, ^v
Sleep, 100
Send, _04
Sleep, 200
Send, {Enter}
Sleep, 200
Send, {Enter}
Sleep, 200
MouseClick, Left, 91, 102
Sleep, 100
MouseClick, Left, 92, 100
Sleep, 100
MouseClick, Left, 147, 156
Sleep, 100
Send, {Down}
Send, {LAlt}
Sleep, 20
Send, {Right}
Sleep, 200
Send, {Right}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 107, 30
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 346, 38
Sleep, 200
Send, ^v
Sleep, 100
Send, _05
Sleep, 200
Send, {Enter}
Sleep, 200
Send, {Enter}
Sleep, 200
MouseClick, Left, 91, 102
Sleep, 100
MouseClick, Left, 92, 100
Sleep, 100
MouseClick, Left, 147, 156
Sleep, 100
Send, {Down}
}

So as you can see, I've had to copy/paste the entire script and change the _01 to be _02 in the next part, followed by _03, _04 and _05.


I found out how to do this with the use of Loop + A_index and it works fine just as the above does:


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
F3::
Loop, 5
{
Send, {LAlt}
Sleep, 20
Send, {Right}
Sleep, 200
Send, {Right}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Right}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 107, 30
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Down}
Sleep, 20
Send, {Enter}
Sleep, 100
MouseClick, Left, 346, 38
Sleep, 200
Send, ^v
Sleep, 100
Send, _
Sleep, 100
Send %A_Index%
Sleep, 200
Send, {Enter}
Sleep, 200
Send, {Enter}
Sleep, 200
MouseClick, Left, 91, 102
Sleep, 100
MouseClick, Left, 92, 100
Sleep, 100
MouseClick, Left, 147, 156
Sleep, 100
Send, {Down}
}

I now need to be able to set the starting value of %A_index% with the use of a input box at the beginning of the script.


I want to be able to enter any number into the inputbox and have %A_index% work sequentially starting from that number.


So since A_Index simply keeps track of the current loop iteration, and therefore is a built-in internal value that cannot be changed...


...what i'm essentially asking is:


How do I get the script to start from a specific loop iteration using inputbox?


Does anyone know how this can be achieved?


Answer



You need to use another intermediate variable, convert A_Index to a 0-indexed loop count by subtracting 1, and add them together inside the loop


InputBox, myEntry, Enter a value, Enter starting value...
Loop {
myIndex:=myEntry + (A_Index-1)
myIndex:=(myIndex<10) ? "0" myIndex : myIndex
;...
Send %myIndex%
;....
}

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...