Best way to count how many lines in a variable?

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

1 Like

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.

1 Like

I suggest you use the "LINES()" Function. Something like this:

You can find the documentation in the Wiki:

1 Like

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:

Keyboard Maestro Export

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)


  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.4.1 (22F770820d)
  • Keyboard Maestro v10.2

1 Like

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 =)

1 Like

You're welcome, @JuanWayri. I've added them to my Favorite Actions (i.e., Actions > Add to Favorites).

1 Like

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:

  1. In the Repeat action, disable the action I provided, and enable the action that includes: LINES(%Variable%local_List%)

  2. Disable both actions above the first TIMING group (:green_circle:).

  3. Select the top disabled action in the macro.

  4. Use Edit > Insert Action > By Name (or ⌃⌘A), and then type: set variable to text

  5. Press return.

  6. For the inserted action, in the Set variable field, type: local_List.

  7. Run the test macro. The result will be: local_LineCount [1]

  8. Return to the action you inserted above and in the to field, type: ABC (but do not type return after the ABC)

  9. Run the test macro. The result will be: local_LineCount [1].

  10. Return to the action you inserted above and in the to field after ABC, type return.

  11. Run the test macro. The result will be: local_LineCount [1].