0

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?

1

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%
    ;....
}
  • Perfect! Thank you! – Myles May 29 at 11:53
  • Hi JJohnston2, I really appreciate your help there. Hoping you might have a workaround for one tiny issue i have with this. Is there a way to have it display 01 instead of 1 when sending %myindex%? Any chance you might be able to modify your answer if poss? – Myles May 29 at 13:13
  • I'm referring to the first 9 values it sends which are 1-9 which I would need to be 01-09. Is that at all possible? – Myles May 29 at 14:00
  • 1
    You can use the ternary operator ("?") to create a single If/Then statement. i.e., If (myIndex<10) THEN put a "0" in front ELSE use as-is. Technically you could combine both assignment statements into a single line, but that might be overly confusing – JJohnston2 May 29 at 22:59
  • Ah i understand now. Thank you so much that's done the trick! Upvotes on everything mate! – Myles May 30 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.