Hello Haevuus,
You're trying to recreate Automator's input/parameter system, and that doesn't work with Keyboard Maestro as is.
Basically Automator gets selected-text to the clipboard, takes that as input, and gives it to an AppleScript handler as the variable input.
In Keyboard Maestro you have to manage this process yourself.
Word & Character Count of Selected-Text.kmmacros (2.4 KB)
As you can see I'm using KM to display the info instead of an AppleScript dialog.
The pause is to allow the clipboard's contents to catch up to the copy action before trying to use it. That pause can vary from nothing to a fairly long time depending upon what app you're in and how much text you're copying.
It is often a good idea to use the filter clipboard action to make sure you have plain-text there instead of styled text. The clipboard usually has more than one type of data in it, and the system tries to feed the appropriate one to the appropriate app. For instance the clipboard might contain styled text and plain text, and it would feed the former to TextEdit but plain-text to BBEdit. The system doesn't always do the right thing, so under some circumstances you need to force the issue.
You have to be careful when using words in AppleScript, because what you define as a word and what it defines as a word will not always be the same.
Run this in the Applescript Editor:
set _text to "now_then now-then"
words of _text
I forget the other possible pitfalls, but they're probably listed somewhere on MacScripter.net.
Another method of doing this task is to use the shell's wc
command in an Execute Shell Script Action (replacing the Execute AppleScript Action in the previous macro):
pbpaste | tr '\r' '\n' | wc
Produces line-count, word-count, and character-count.
Once again though you're dependent upon how wc
defines words.
--
Best Regards,
Chris