AHK 文件读取问题
Issue with AHK file reading
我正在编写一个简单的 AHK 程序,它从 .txt 中获取文本并输出它们。
当它 运行 时,它重复发送存储在 line
.
中的任何占位符值
SetWorkingDir %A_ScriptDir%
timer := 1050
line = 0
^q::
Loop, Read, %A_WorkingDir%\read.txt {
line := %A_LoopReadLine%
Settimer, Label, %timer%
Return
^+q::ExitApp
Label:
Send %line%{enter}
Return
输出:
0
0
0
...
代码目录中唯一的文件是它本身和 read.txt。
我怀疑这是我对目录语法的误用。
我看到四个实际问题。
{
在错误的行上,缺少结束符 }
,在表达式语句中使用旧语法,并且错误地使用了计时器。
所以首先,您不能将起始 {
与文件名放在同一行(以 OTB 用户为例)。它将被读取为文件名的一部分。
将大括号放下一行。
然后缺少右大括号,应该是显而易见的。应该添加(假设我们首先修复代码中的其他问题)
然后在表达式语句中使用遗留语法。
line := %A_LoopReadLine%
通过将变量包装在 %%
中来引用它是您在古老且已弃用的遗留 AHK 语法中所做的事情。但是你正在使用 :=
which assign an expression to a variable. As opposed to assigning literal text to a variable, which is what the legacy =
确实(=
不应该再被用来赋值,并且可以说 %%
也不应该)。
因此,在现代表达式语句中,您只需键入变量名称即可引用该变量。放弃 %
s。
然后是计时器。我只能猜测为什么你在那里有一个计时器。我假设您可能认为它做了一些与实际不同的事情。
timer 基本上做的是每 x 毫秒启动一个预定义的 function/label。
你试图做的是启动一堆计时器,无论你的循环恰好在哪一行,它们都会随机喷出。 (循环将在单个计时器有时间做任何事情之前结束)
修改后的代码:
;this is already the working directory by
;default, this does nothing for us
;SetWorkingDir %A_ScriptDir%
;neither of these does anything for us
;removed
;timer := 1050
;line = 0
^q::
;the file is assumned to be in the working directory
;by default, no need to explicitly specify it
;
;also forced expression syntax for the parameter
;by starting it off with a % and a space to
;explicitly specify we're working with a string
;not actually needed, but looks better if you
;ask me
Loop, Read, % "read.txt"
{
;ditched the legacy syntax and switched
;over to SendInput, it's documented as
;faster and more reliable
line := A_LoopReadLine
SendInput, % line "{Enter}"
}
Return
^+q::ExitApp
我的回答可能让您对遗留语法与表达式语法感到困惑, 我之前关于 %%
和 %
.
的回答
还包括一个 documentation link 以阅读更多关于遗留与表达的信息。
作为奖励,如果您有兴趣了解您的脚本究竟做了什么,下面是发生的事情:
循环尝试读取名为 %A_WorkingDir%\read.txt {
.
的文件
不存在这样的文件,因此循环甚至没有启动。
然后它转到计时器,并开始每 1050 毫秒打印出 0
(这是您分配给 line
变量的值)。
这会一直持续下去。
我正在编写一个简单的 AHK 程序,它从 .txt 中获取文本并输出它们。
当它 运行 时,它重复发送存储在 line
.
SetWorkingDir %A_ScriptDir%
timer := 1050
line = 0
^q::
Loop, Read, %A_WorkingDir%\read.txt {
line := %A_LoopReadLine%
Settimer, Label, %timer%
Return
^+q::ExitApp
Label:
Send %line%{enter}
Return
输出:
0
0
0
...
代码目录中唯一的文件是它本身和 read.txt。 我怀疑这是我对目录语法的误用。
我看到四个实际问题。
{
在错误的行上,缺少结束符 }
,在表达式语句中使用旧语法,并且错误地使用了计时器。
所以首先,您不能将起始 {
与文件名放在同一行(以 OTB 用户为例)。它将被读取为文件名的一部分。
将大括号放下一行。
然后缺少右大括号,应该是显而易见的。应该添加(假设我们首先修复代码中的其他问题)
然后在表达式语句中使用遗留语法。
line := %A_LoopReadLine%
通过将变量包装在 %%
中来引用它是您在古老且已弃用的遗留 AHK 语法中所做的事情。但是你正在使用 :=
which assign an expression to a variable. As opposed to assigning literal text to a variable, which is what the legacy =
确实(=
不应该再被用来赋值,并且可以说 %%
也不应该)。
因此,在现代表达式语句中,您只需键入变量名称即可引用该变量。放弃 %
s。
然后是计时器。我只能猜测为什么你在那里有一个计时器。我假设您可能认为它做了一些与实际不同的事情。
timer 基本上做的是每 x 毫秒启动一个预定义的 function/label。
你试图做的是启动一堆计时器,无论你的循环恰好在哪一行,它们都会随机喷出。 (循环将在单个计时器有时间做任何事情之前结束)
修改后的代码:
;this is already the working directory by
;default, this does nothing for us
;SetWorkingDir %A_ScriptDir%
;neither of these does anything for us
;removed
;timer := 1050
;line = 0
^q::
;the file is assumned to be in the working directory
;by default, no need to explicitly specify it
;
;also forced expression syntax for the parameter
;by starting it off with a % and a space to
;explicitly specify we're working with a string
;not actually needed, but looks better if you
;ask me
Loop, Read, % "read.txt"
{
;ditched the legacy syntax and switched
;over to SendInput, it's documented as
;faster and more reliable
line := A_LoopReadLine
SendInput, % line "{Enter}"
}
Return
^+q::ExitApp
我的回答可能让您对遗留语法与表达式语法感到困惑,%%
和 %
.
的回答
还包括一个 documentation link 以阅读更多关于遗留与表达的信息。
作为奖励,如果您有兴趣了解您的脚本究竟做了什么,下面是发生的事情:
循环尝试读取名为 %A_WorkingDir%\read.txt {
.
的文件
不存在这样的文件,因此循环甚至没有启动。
然后它转到计时器,并开始每 1050 毫秒打印出 0
(这是您分配给 line
变量的值)。
这会一直持续下去。