Way to Count Characters?

Is there a way to have KM count the characters of selected text?

Yep. Copy to clipboard, and use the Filter Clipboard Action.

Note that this replaces the clipboard with the count.

Hi and good day,

I wonder if sth is wrong with my KM.

To the above macro, I added Display System Clipboard.

I ran the macro 3 times, and I got:

1st: 36 (looked correct but 2nd and 3rd:

2nd: 2 ( wrong!)

3rd: 1 ( wrong!)

I restarted KM and even restarted the Mac.

/
with best regards,
Omar K N
Stockholm, Sweden

I think that is because the first time the clipboard contained a string of 32 charachters.
When you use the filter clipboard, then the clipboard is actually changed to the number 32.
So the second time you run it, the clipboard contains the string "32".
Which is 2 characters.
And third time the clipboard contains the string "2", which is 1 character.

If you want to avoid this then do it like this:
Count characters from clipboard.kmmacros (2.7 KB)

That’s why I noted:

Hi and good day Jimmy & Michael,

Thank you, it’s working!

BTW: The text has to be copied into the clipboard, not just selected, of course.

/
with best regards,
Omar K N
Stockholm, Sweden

That’s why I wrote earlier:

Ah. Thank you very much. Here's my final macro.

If you want to avoid disturbing the clipboard you can use AppleScript.

-Chris

------------------------------------------------------------
# Assign count of characters on Clipboard to a KM varable.
------------------------------------------------------------
set characterCount to count of characters of (get the clipboard as Unicode text)
tell application "Keyboard Maestro Engine"
  if variable "clipboardCharacterCount" exists then
    set value of variable "clipboardCharacterCount" to characterCount
  else
    make new variable with properties {name:"clipboardCharacterCount", value:characterCount}
  end if
end tell
------------------------------------------------------------
1 Like

Given text such as “42” that your macro puts on the clipboard, how would I use that for the number of repetitions in a Repeat action. (That field doesn’t seem to be acting like either a normal number field or text field.)

Even if I use AppleScript to count the characters, returning a number, how would I get that number into the repetitions field?

Generic-Test 01.kmmacros (2.1 KB)

embarrassing.

FYI: I’ve since updated this macro to count characters AND words. Get Character & Word Count Macro

In the most recent version of the KM macro you copy the clipboard to a named clipboard, then you copy the named clipboard to a variable and filter the variable. Couldn’t you just filter the named clipboard instead? (I have fooled around with that and I don’t seem to be getting the results I expected.)

The Repeat action text field is a numeric field which accepts calculations. There is no function to retrieve the system clipboard, so you need to use the Set Variable to Text action with the CurrentClipboard token, either before or after filtering it with the Character Count (using either the Filter Clipboard or Filter Variable action).