There are a couple issues with what you've done there. The most obvious is that the Set Variable that counts matches is incorrect: In a calculation field, you don't need %Variable%
—just put in the name of the variable.
The second is that the For Each loop looks at lines, not characters within words. So the logic needs to be reworked, because your test won't do what you want it to do. We need to step through the characters, not the lines. Especially as you said it's always going to be one line.
Third, you have to set Local_Count to zero before you can start incrementing it.
With all that said, here's a solution that works—there may be slightly simpler ways using a For Each, but this one seems to do what you want:
Finished Display On Screen Various - rg.kmmacros (34 KB)
(Note that I was using e|e|e|e|e
as a test trigger; you'd have to modify to integrate in your macro, obviously.)
Now, with all that said, this is definitely not the way I would do this :). I'd use the shell, where there are about 300 ways to do this rapidly; here's one I found on StackExchange years ago and kept around as a favorite shell script action:
$ echo "Why|Cant|I|Get|This" | tr -cd '|' | wc -c | tr -d ' '
4
The echo
command outputs the string, which is then sent to the second with (ironically) the pipe sysmbol. The tr
command is used to translate or delete characters—the c
option means "not the following character" and d
is "delete." So basically, this deletes everything except the pipe symbol. That's passed to wc
, which uses the -c
option to count characters. But that output contains leading spaces, which the final tr
takes care of.
And in Keyboard Maestro parlance for your string, a one-action shell script will spit out the answer:
echo "$KMVAR_ValueCombo" | tr -cd '|' | wc -c | tr -d ' '
Not my work at all, but very useful.
-rob.