Reputation: 47
I was writing a simple AHK program which takes text from a .txt and outputs them.
When it ran, it repeatedly sent out whatever placeholder value was stored in 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
Output:
0
0
0
...
The only files in the code's directory was itself and read.txt. I suspect it was my misuse of directory syntax.
Upvotes: 0
Views: 1049
Reputation: 6499
I see four actual problems.
{
on the wrong line, missing a closing }
, using legacy syntax in an expression statement, and using a timer wrong.
So firstly, you can't have your starting {
on the same line as the filename (take that OTB users). It'll be read as being apart of the filename.
Drop the brace down one line.
And then missing the closing brace, should be obvious. That should be added in (assuming we'd first fix the other problems in the code)
Then usage of legacy syntax in an expression statement.
line := %A_LoopReadLine%
Referring to a variable by wrapping it in %%
is what you'd do in ancient and deprecated legacy AHK syntax. But you're using :=
which assign an expression to a variable. As opposed to assigning literal text to a variable, which is what the legacy =
does (=
should never be used to assign values anymore, and arguably neither should %%
).
So in a modern expression statement you refer to the variable by just typing its name. Ditch the %
s.
And then the timer. I can only guess why you have a timer on there. I'd assume you probably thought it does something else than what it actually does.
What a timer basically does, is launches a predefined function/label every x milliseconds.
What you tried to do is launch a bunch of timers that would all spew out randomly whatever line your loop happens to be on. (The loop would reach the end before a single timer would have time to do anything)
Revised code:
;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
My answer probably left you confused on legacy syntax vs expression syntax, here's a previous answer of mine about %%
and %
.
Also includes a documentation link to read even more about legacy vs expression.
As a bonus, if you're interested in knowing what exactly your script did, here's what happened:
The loop tried to read a file named %A_WorkingDir%\read.txt {
.
No such file existed, so the loop didn't even launch.
Then it went to the timer, and started printing out 0
(which is the value you assigned to the line
variable) every 1050 ms.
This would've continued on forever.
Upvotes: 0