You're basically just confused about the deprecated legacy syntax and the modern expression syntax.
Referring to a variable by wrapping it in %
is what you'd do in legacy AHK.
However, that's legacy and basically should never be done anymore.
In an expression, you refer to a variable by just typing its name.
If you are in a legacy statement (basically just parameters in commands), you switch over to the expression syntax by starting off the parameter with a single %
followed up by a space. You'll see examples of this below.
I'll fix your code line by line documenting the changes:
Here I force AHK to interpret an expression on the filename parameter just to explicitly specify a string with ""
.
This is not needed and makes no difference in practice, but it looks a lot better if you ask me. Especially if we were to have a longer and more complex string. Would eliminate the need of e.g. escaping certain characters etc.
Loop, Read, % "out.txt"
Here I removed the %
s, because we're in function's parameters.
Functions aren't legacy AHK and they use the modern expression syntax, so we refer to variables by just simply typing their name.
tgt := StrSplit(A_LoopReadLine, A_Space)
Now we're again using a command. All commands use the legacy syntax on each parameter by default (unless otherwise specified in the documentation).
PixelGetColor
actually happens to be one of those commands which would accept an expression on the x and y parameters, so me forcing an expression here by starting off the parameter with %
isn't actually needed.
But it doesn't hurt, so I'll just do it to make it clearer and make it show what you'd normally do with commands.
PixelGetColor, hue, % tgt[0], % tgt[1]
The non-legacy if doesn't use the legacy syntax, so %%
gets removed.
if (hue != tgt[2])
The Click
command doesn't accept expressions, so we have to force it with a %
.
Click, % tgt[0] ", " tgt[1]
And now if you were paying attention above, you should wonder why I didn't do this:
Click, % tgt[0], % tgt[1]
And that would be a very good question. You would do exactly that if it was literally any other command but the Click
command.
The reason why you don't do that with the Click command, is because it's special (for good or for worse). It doesn't actually have different parameters, it just has one single string parameter. That one parameter contains all the required words, commas, and numbers.
And one single string parameter is what tgt[0] ", " tgt[1]
creates. It concatenates those two values with the string comma and space.
So here's the full script:
Loop
{
if (GetKeyState("Space"))
{
Loop, Read, % "out.txt"
{
tgt := StrSplit(A_LoopReadLine, A_Space)
PixelGetColor, hue, % tgt[0], % tgt[1]
if (hue != tgt[2])
{
Click, % tgt[0] ", " tgt[1]
}
}
}
Sleep, 10
}
To learn more about legacy vs expression, here's a pretty good documentation page to get you started:
https://www.autohotkey.com/docs/Language.htm