Request for String Functions

Request for String Functions

@peternlewis, you have provided us with a large set of KM Functions, but I believe all of them operate only on numeric data.

There are a few choices for Variable and Clipboard Filter Actions, that seem to me to be more like functions, than filters (at least as I understand the word "filter").

Would it be possible in the next major version of Keyboard Maestro to provide the following "filters" as functions, so that they could be used anywhere a function can be used:

Add to Functions

  • Count the characters, words or lines and return the
  • Calculate an expression and return the result, see the Calculations
  • Process Text Tokens and return the result, see the Text
    Tokens section.

It is counter-intuitive to me to replace a variable with something that does not contain any part of the variable, and call this a "filter". An example is the Filter "Character Count".

When I'm looking to manipulate strings, I often forget that these functions are embedded in "Filters", and not "Functions".

So, I'd like to be able to use, "Set Variable to" Action, and use a function that works on strings, like "Character Count".

It would also be great to use these string functions in IF Then Else Actions, like with the "Calculation" condition:

If All Conditions Met
The calculation ‘CharacterCount (My Text Var) > 20’ returns true (non-zero)


Well, this depends on the content quality of the text. I have seen many texts that could rather losslessly be replaced by their character count. This can save huge amounts of disk space!

In that sense the character count filter can very well act as a highly efficient clean-up filter, stripping unnecessary characters :grinning:

1 Like

Tom, I think you need to take a break. You’ve written so many macros the quality of your text is breaking up. :open_mouth:

Ha, you see now how useful the Character Count filter is, and why it’s called a filter?!

My filtered post above is “320” and your last post is “111”. And the whole thread would just be “1830”, without the quoted text. Much less work for the forum server! :stuck_out_tongue:

Functions can only be used in numeric fields. Currently they have no support for strings, and I don’t really plan to add support for strings - the cost/benefit on that seems prohibitively negative.

There could be a COUNTCHARACTERS(Variable) (COUNTWORDS, COUNTLINES) function that takes a variable name and returns the result. There could also be a CALCULATE(Variable) function that returns the numeric result of the variable after it is calculated.

I’m not sure about processing text tokens. I suppose I could suspend the calculation engine until the matching closing bracket, just like the %Calculate% text token does, and then it would work if and only if the text tokens returned a numeric result (so %ICUDateTime%yyyy% for example).

So all that is relatively straight forward.

What I don’t plan to do is something like “abc” + “def” to produce “abcdef” or anything like that.

Whether a case can be made for anything like COUNTCHARACTERS(“Hello”) or COUNTCHARACTERS("%ICUDateTime%yyyy%"), that I’m not sure. Maybe.

If any of these are added as functions, then they will work in the calculation condition as well as any other numeric field.

So how do you concatenate two strings (each in a variable)?

Just use a “Set Variable To” Action, with the two variables in the to: Text Area:
%Variable%Var A%%Variable%Var B%

this is the same as this in code:

var varC = varA + varB   // JavaScript
set varC to varA & varB  -- AppleScript 
1 Like

[Your ridiculously quick responses are much appreciated :smile:.)

This must be a KM7 feature, as I can’t find it in 6. I guess I can just drop into AppleScript for this.

I'm pretty sure this was available in KM 6:

You can find the "Set Variable to" Action here:

OR, just press ⌘⌃A and type "set var" and you should see a list of Actions that include "Set Variable ..."

1 Like

Thought you meant there was something called “Set Variable to Action” whatever that would mean. I had tried what you suggested. It actually worked – had I looked at the “old value ➞ new value” in gray in the concatenation action. Instead I was looking at a Display Text Briefly that followed, which didn’t display anything, so I thought there was a failure to concatenate. I still don’t know why the text isn’t displaying.

This is done for the next version. New functions that take text token processed strings and produce either the number of characters, words or lines in the resulting string, or the value of calculating the string

  • CALCULATE(%ICUDateTime%yyyy%+3) — Process Text Tokens and then calculate
  • CHARACTERS(%Variable%Text%) — Process Text Tokens and then count characters
  • WORDS(%Variable%Text%) — Process Text Tokens and then count words
  • LINES(%Variable%Text%) — Process Text Tokens and then count lines

Peter, that’s outstanding! :thumbsup:
Thank you very much.