[Feature Request] Inline Functions: Variable Interpolation

Hi,

It would be nice, when using Inline Functions in otherwise non-calculation based actions (ex: “Insert Text”, “Display Text”), if variables were interpolated.

IOW: Call a variable by name, and have the function run on the contents of the variable, not the variable name.

I’ve tried all of these, and none work as expected:
%Calculate%CHARACTERS(%var__name%)%
%Calculate%CHARACTERS(%Variable%var__name%)%
CHARACTERS(%var__name%)
%CHARACTERS(%Variable%var__name%)%

Am I missing something, or is this a valid Feature Request?

Thanks.

I believe what you are expecting is a reasonable expectation, but apparently the Calculate Token only supports KM variables that can be evaluated as a number:

Calculate Token

The %Calculate%% token returns the numeric result of performing a calculation,
where may be any of the following:

Mathematical formula like (2 + 3)*12
Use any Variable which may be evaluated as a number
:!: Just enter the Variable Name (without the %) in the Calculate token
Examples:
%Calculate%MyVar + 1%
%Calculate%(MyVarWordsPerSec * 60)/MyVarWordsPerPage%

So although you, and I, expect that this should work:

it does not because, in this case, myKMVar has text and cannot be evaluated as a number.

Result:

This also does NOT work:

Result:

One might expect that since the CHARACTERS() function returns a number, it should work, but it does not.

I'm not sure if this is a bug, or by design.
Maybe @peternlewis can tell us.

In a calculation, a variable can only (currently) contain a series of numbers separated by commas.

As well, tokens cannot contain % characters within them, so you can't nest them generally.

What you need to do is use the Filter Variable action with the Calculate option on the variable to calculate the value of the variable first, then you can use the variable as normal.

In a calculation field, you can use the CALCULATE function.

I have enhanced the calculation engine for the next version so that variables used in calculations can themselves contain calculations that are implicitly evaluated. So with the Variable set to "235,7*11", you can use %Calculate%Variable[1]% to get the result (30).

BTW, the CHARACTERS function returns the number of characters in the text, it does not calculate anything it.

Yes, I am well aware of that, but CHARACTERS() does return a number, so it is actually doing a calculation. I use the equivalent functions all the time in formulas I have in many other languages. Using the length of a string in the same statement as other numeric operations is often very useful.

IMO, any KM function that returns a number should be allowed in any KM token that does a calcuation, especially:
%calculate%CHARACTERS(%myKMVar%)%

To make it clear for the user, it seems to me that the %calculate% token in a text area should work identical to any Action that provides a numeric field.

For example:

Thanks for considering this request.

They are.

The problem is not that CHARACTERS() is not allowed, the issue is that you cannot have a percent character within the expression of the %Calculate%expression% token. %Calculate%CHARACTERS(text)% works fine and returns 4.

I will ponder whether I can add bracket matching the the %Calculate% token, since unbalanced brackets would never make a valid calculation. I'm not sure about other tokens what might take calculations and how easy it is to generalise this. I not sure I really want the token parser to have to know what constitutes valid parameters when parsing them.

Continuing your orthog doctrine, if CHARACTERS() can accept % in a numeric field, then it should be able to accept it anywhere. IOW, you should parse the function independent of where it is used.

Just my 2¢.

Done for the next version.

3 Likes

Thanks for being so responsive, Peter.
I look forward to KM 8.

Sorry for dropping out after my original post. JMichaelTX addressed my thoughts exactly.

Cool solution.

I'm sure this has been asked before, but given how (ahem) verbose statements like these are getting, what are your thoughts about using UI tokens (discrete, click-and-drag-able, less error/typo prone) to replace the currently 100% text based input.

Something like the search field in Apple Mail.

Obviously that's a lot of work, and you must have thought about it already, and the costs likely outweigh the benefits. But I'm just wondering your thoughts on it.

Agreed.

If I understand you correctly, KM already provides a means to auto-enter tokens and functions:

  1. If auto-complete is enabled (which it is by default), then when you start typing a token/function, it should offer a suggestion to complete.
  • From the Action block, click on the "Insert Token" popup list to get a selection:

.

  • Insert Tokens, Functions, and Actions, from the KM Editor menu:

Does this meet your needs, or were you thinking of something else?

Sorry I should have been more clear.

Hum.
This I don't see.
I tried typing a few different prefixes and none of them auto-completed as I typed. Is that what you mean?

Yes I know these.

I'm referring to something that "locks in" the function/token/variable visually.

The goal:

  • Less error prone (i.e.: typos)
  • Visually it would be more clear what's going on (via contrasting color, font weights, borders, etc.)

Here's an example from Apple Mail's search field:

Instead of From and Subject, there could be options like: Variable, Calculation, Token, Function.

Yep. Here's an example. I will type a few characters, and then pause to let autocomplete appear. IMO, the pause/delay is too long.
After my pause the suggestion will appear highlighted in green. I then press TAB to accept, or ESC to cancel.

Personally, I don't like it and don't use it. I have turned it using this Terminal command:
defaults write com.stairways.keyboardmaestro.editor AutomaticCompletion -bool NO

I find it gets in my way more than it helps. I would prefer for the auto-complete to activate ONLY when I press a key (F5 is the Mac standard).

I'm not sure how that would work. It sounds nice, but it also sounds like it could get in the way. But I do get your point. The current method is somewhat verbose, and can easily be corrupted by a simple, unintended, character change.

KM has 3 basic entities that can be used in an Action text box:

  1. Variables
  • Tokens
  • Functions

All of these are typed in a plain text field.
What would be nice is if it were a special rich text field, like the coding pane for Script Editor, and each of the three would appear in different colors, and KM could parse the text without us having to use the % delimiter.

Something like:

So, in our above use case, all I would need is:

as opposed to what we have now, which is more difficult to read:

But I really doubt that we could see this for KM 8 (which I think is just about done), even if Peter were to accept it for a future version.

Agreed

This was not turned on for me.
But I enabled it. Thanks!

Autocomplete doesn't work everywhere that variables/tokens/functions/calculations are accepted though.

  • Set variable action = YES
  • Display Text... action = NO

F5 doesn't auto-complete for me. I'd like it to though :slight_smile:

  • Verb (ex: From): Would be either Variable, Function, Token, or Calculation.
  • Noun (ex: JMichaelTX): Would be the Variable, Function, Token, or Calculation name.

The Noun would be populated via auto-complete.

Maybe one way to think of it would be something like how this site visualizes Regular Expressions:

(I can imagine it would get tricky with calculations because of nesting. But I'm just getting the thought started :slight_smile: )

That would be a great step in the right direction!
(And maybe ultimately better than my idea.)

It does not need to be turned on, it is on by default.

Autocomplete is somewhat context sensitive and knows which kind of fields they are. Display Text is a token text field and will complete tokens, so if you type %ICU and either wait 3 seconds or press F5 (or perhaps Shift-F5) then Keyboard Maestro will autocomplete the token for you. The AutomaticCompletion preference lets you turn off the "wait 3 seconds part". I will drop it down to 2 seconds.

F5 (or maybe Shift-F5) is a standard Mac facility.

I'll leave you two to continue your musics about improvements to the display of the field.

When I ran:

defaults read com.stairways.keyboardmaestro.editor AutomaticCompletion -bool

I received one of these:

2017-07-08 11:15:56.290 defaults[89184:5482389]
The domain/default pair of (~/Library/Preferences/com.stairways.keyboardmaestro.editor, AutomaticCompletion) does not exist

No big deal though.
I enabled it with defaults write

Yes. That works.
Variables too actually. I think maybe before auto-complete was getting confused because above I had some other incomplete variables.

Correct, as it should be, there is no need to set the preference, it is enabled by default.