AutoHotKey:如何设置当前循环迭代的起始值?

AutoHotKey:如何设置当前循环迭代的起始值?

我编写了一个基本的 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。

我发现了如何使用环形+A_索引并且它像上面的一样正常工作:

#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}


}

我现在需要能够设置起始值%A_index% 的使用脚本开头的输入框。

我希望能够在输入框中输入任意数字,并让 %A_index% 按顺序工作从该数字开始

因此,由于 A_Index 仅跟踪当前循环迭代,因此是一个无法改变的内置内部值……

...我主要想问的是:

如何使用输入框让脚本从特定的循环迭代开始?

有谁知道如何实现这一点?

答案1

您需要使用另一个中间变量,A_Index通过减 1 转换为 0 索引的循环计数,然后在循环内将它们加在一起

InputBox, myEntry, Enter a value, Enter starting value...

Loop {
    myIndex:=myEntry + (A_Index-1)
    myIndex:=(myIndex<10) ? "0" myIndex : myIndex
    ;...
    Send %myIndex%
    ;....
}

相关内容