I've been using this for now but I'm sure there must be a better way
The Set Variable Text to Local Line is just a frivolous action to allow the action to work as you must reference the line somewhere in the action for it to work
I've been using this for now but I'm sure there must be a better way
The Set Variable Text to Local Line is just a frivolous action to allow the action to work as you must reference the line somewhere in the action for it to work
You can try Filter Action. I remember there is a line number option.
Can you send the lines of text to a shell script?
If so, wc -l
will return the number of lines.
I suggest you use the "LINES()" Function. Something like this:
LINES(%Variable%SN_LinkList%)
You can find the documentation in the Wiki:
https://wiki.keyboardmaestro.com/function/LINES
I'm not sure why, but the LINES() function will return 1 is the variable is blank, i.e., includes no lines. In all other cases, LINES() returns the expected number. Thus I use this to return the count of lines:
Thanks for the observation. Yeah, sometimes it gives unexpected results.
Do you think including the CHARACTERS function have a performance impact or is it negligible?
The only unexpected result that I know of is the case I mentioned, i.e., a variable with no lines. Do you know others?
Negligible. Here's a test macro that proves that it makes no measurable difference.
Download: Count Lines.kmmacros (6.6 KB)
It's because it includes "empty" lines in the calculation AFAIK, that's why in your "blank" variable the result is 1 (blank variable = 1 "empty" line), IMO.
When working with loops, I often "remove" the text from already processed lines and having them "empty" doesn't help unless I also remove the linebreaks (logical but unexpected for me, given that I was used to working with loops having the option "Include blank lines" turned off by default).
Forget to include in my previous reply:
Thank you very much for the "Timing Actions" included in your macro. It will help me a lot testing and optimizing my macros =)
You're welcome, @JuanWayri. I've added them to my Favorite Actions (i.e., Actions > Add to Favorites).
I think I understand what you are saying here, @JuanWayri. But if that's the case, try these steps and I think you'll see the unexpected inconsistency that I'm addressing with the LINES(%Variable%local_List%)*(CHARACTERS(%Variable%local_List%)>0)
For the test macro that I provided above:
In the Repeat action, disable the action I provided, and enable the action that includes: LINES(%Variable%local_List%)
Disable both actions above the first TIMING group ().
Select the top disabled action in the macro.
Use Edit > Insert Action > By Name (or ⌃⌘A), and then type: set variable to text
Press return.
For the inserted action, in the Set variable field, type: local_List.
Run the test macro. The result will be: local_LineCount [1]
Return to the action you inserted above and in the to field, type: ABC (but do not type return after the ABC)
Run the test macro. The result will be: local_LineCount [1].
Return to the action you inserted above and in the to field after ABC, type return.
Run the test macro. The result will be: local_LineCount [1].