This is a loooooong post and something most of you who have been using Keyboard Maestro for a long time will find too obvious, but now that I finally understand this combo (For Each
+ Search and Replace
), I decided to create a note for myself in case I forget about it again, and decided to share it here in case others struggle with the same issue.
As always, feel free to share your feedback or if something is not correct.
The For Each
action is a loop that runs all the actions inside execute the following actions
section as many times as there are lines in a variable / Finder selection / Finder location / clipboard, etc.
So if my variable (or any of the other options above) contains these 5 lines:
My name is...
My age is...
I live in...
My exercise routine is...
I like to eat...
The For Each
action (loop) will run the actions inside the execute the following actions
section, 5 times, because there are 5 lines.
If I add the main variable (or any of the other options above) as my target in the Search and Replace
action like this
... it will analyze and make any necessary changes to the whole variable Local__Code
, 5 times.
Example with the Local_Code
variable being this:
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
... and the For Each
action being this:
The first time it runs, it will replace all a
in Local__Code
(not the Local__line
variable -- this is what was making everything confusing to me) with @
:
So I will get this result, even after just the first run:
/* Cre@te the checkm@rk/indic@tor (hidden when not checked) */
.checkm@rk:@fter {
content: "";
position: @bsolute;
displ@y: none;
}
... because I was using the main variable Local__Code
in my Search and Replace target, not Local_line
.
Now even if it runs 5 more times, nothing will change, because all a
were already replaced with @
, but in other cases as I share lower in this post, it makes a difference.
Initially I was thinking that the workflow was:
"In each line of variable XYZ (in this case it was Local__Code
), run these actions below, and automatically update the variable XYZ with the new changes on that line."
So it would read the first line, make the changes only to that line, and update the first line in XYZ variable with the modified line. Move on to the second line, repeat. At the end of the loop I would get the XYZ variable updated with all the changes.
That's why I was always under the impression that the workflow that would make sense to me was that I should use the variable inside the For Reach
text field (in this case Local__line
) as my Search and Replace
target, but then if I do that, I have to do something with that variable in order to create a new list with all the modified lines (more on that at the bottom of the post).
Now when I'm trying to replace "something" with another "something" that also has the previous "something", and there's more than 1 line, then I have a problem if I'm using the main variable Local__Code
as my target.
Example: replacing "
with \"
in the line content: "";
This first time I run this:
I will get this, as expected:
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: \"\";
position: absolute;
display: none;
}
but the second time I run it (because the variable has 6 lines, so it will run all actions 6 times), it will now replace both "
in \"\"
with \"
So "
will be replaced with \"
giving me \\"
after the second run:
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: \\"\\";
position: absolute;
display: none;
}
Since I have 6 lines, it runs 6 times and I get this at the end of the loop:
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: \\\\\\"\\\\\\";
position: absolute;
display: none;
}
If I just want to replace it once and get this:
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: \"\";
position: absolute;
display: none;
}
... then I need to use the variable Local__line
as my target so Keyboard Maestro can make changes to that line alone, not the whole Local__Code
variable. Then I need an action to add that changed variable to a new variable (e.g. Local__finalCode
) and on each run it will keep adding each modified line Local__line
to the new Local__finalCode
variable: