Where to find what the variable to use for the current element of a for each loop is?
I am trying to split a string into lines and work on each of those
%Variable%SingleLine% doesn't seem to cut it in this case
And Peter, this would an awesome spot to mention what the missing variable is
You need to use the variable name from the For each action.
So in this case your set variable to should be:
OMG that actually worked. I was certain your suggestion was wrong
Can you explain the logic behind that though? Where does VarName come from?
Sounds like a placeholder to me, which you would use when explaining how the %Variable%…% construct works
It looks like you're not understanding that “VarName” at the top of the action image in your first post is a USER-entered value.
Since you're using a Lines-In Collection action try changing “VarName” to “lineContents”.
Actions like that one have DEFAULT variable names where appropriate, and that is one of them.
Note that the field can be edited.
The For Each action is incredibly useful, but it can definitely be tricky when you’re first wrapping your mind around how it works. The key thing to understand is that when the macro runs, the For Each action assigns every item in the selected collection(s), one at a time, to whatever variable name you enter in the “For each ______” field (regardless of what the placeholder variable placeholder name may be).
Local__code variable case, the For Each action will take each line within it and set it as the content of a variable that is called whatever you enter in the “For each _____” field (
VarName by default, but it could also be
SingleLine, or even something nonsensical like
BlargleHopper). This variable (
BlargleHopper, or whatever) will then retain the single line from
Local__code as its value for the duration of the loop (i.e. the actions you choose to run for every item in the collection you’re working with), after which For Each will move on to the next line in the collection and repeat the process.
However, if you don’t actually do something with the variable from the “For each _____” field in the loop, then no variables are actually changing from loop to loop, so it won’t produce the results you expect. The warning you saw was telling you to use the same variable name in the For Each actions to ensure that different variable contents were used in every loop.
Hopefully this (and everyone else’s answers) helps shine a bit of light on the logic behind it! If you want a little more information, you can always check the For Each wiki page: https://wiki.keyboardmaestro.com/action/For_Each
As other mention, then you define the variable name for each line yourself. So in this case you left it to the default VarName.
How could I have missed that???
Got it now
The speed at which all of you chimed in was awesome
Don't feel too bad. I did the exact same thing many years ago.
And its nice that @peternlewis has made, so KM checks if you use the variable.
At least now people know what question to ask.
I will extend the text in the alert a little:
A For Each action sets the variable you specified at the start of the action to each entry in the collection(s) and performs the enclosed actions
Perhaps that will help the next person.