我制作了一个基本的 AHK 脚本,它为我每天使用的系统上的每个文件添加了唯一的元数据。我的想法是将 _01 添加到第一个文件,然后将 _02 添加到第二个文件,然后一直上升,直到我退出脚本。
起初,我觉得我已经做了很长一段时间,我不确定需要什么才能让脚本更小,编辑起来更轻松。
这是脚本:
#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}
}
如您所见,我不得不复制/粘贴整个脚本,并在下一部分中将 _01 更改为 _02,然后是 _03、_04 和 _05。
我发现了如何使用Loop + A_index来做到这一点,它工作正常,就像上面的一样:
#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?
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