I know I'm missing a small element here

But I can't figure out what is missing. I want to run an applescript from Keyboard Maestro rather than Automator, where I trigger it now. Could anyone shine some light on this for me? (screenshot attached)

The applescript simply counts words and characters in a selection.

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

1 Like

Chris,

Thank you for this! It works elegantly -- and I learned something about the concept.

Would you mind clarifying for me your advice:

It is often a good idea to use the filter clipboard action to make sure you have plain-text there instead of styled text.

Is that an additional instruction needed in KM, or is is already handled in the Applscript? Sorry I'm a bit dense about this.

Thanks again,

--Dennis

Hey Dennis,

The clipboard usually contains more than one kind of data.

If I copy an AppleScript in the Applescript Editor which is styled-text and then run 'clipboard info' in the Applescript Editor I'll get something like this as a result:

{{«class RTF », 12586}, {«class utf8», 1097}, {«class ut16», 2172}, {uniform styles, 42804}, {string, 1085}, {scrap styles, 6962}, {Unicode text, 2170}, {uniform styles, 42804}, {scrap styles, 6962}}

As you can see there're a few different data-types in there.

OSX is fairly good about providing the correct data-type in a given circumstance, but there are times when you might need to ensure your source data is of a particular type - hence my suggestion of a filter-clipboard-action.

Open TextEdit, type some styled text, save the file, and then open that file with TextWrangler, BBEdit or another plain-text editor. You'll see a whole lot of RTF-Style-Info in addition to the literal text.

The clipboard is a more sophisticated widget than most people realize. :wink:

--
Best Regards,
Chris

Chris,

I just tried your experiement -- and I never realized this! So is something like this what you recommend? [screen shot]


Hey Dennis,

As is often the case there's more than one way to do things. :smile:

Here's how I would ordinarily do it:

Word & Character Count of Selected-Text { AppleScript }.kmmacros (3.2 KB)

Best Regards,
Chris

Ah, but I like yours better! It seems to be a bit quicker. Thanks again for all the help, Chris!

–Dennis

These two methods should be functionally equivalent because in both cases Keyboard Maestro will read the plain text version of the clipboard.

Where you can get differences would be if you did this:

(the second action could equally be Filter Clipboard: Remove Styles).

In this case, the first action will read the styled text from the clipboard which may be different text to the plain text, and then Keyboard Maestro will write just that styled text to the clipboard (including plain text variants of that styled text). Keyboard Maestro will always write consistent data on the clipboard (that is the plain text and styled text will have the same characters. Other applications like Preview or Pages may not - the text of the plain text and the text of the styled text may be different.

Thanks, Peter! This opens up a whole new layer of KM for me – the power of the clipboard tools.

–Dennis