KM Terminal

Over the years, I've realized that the Terminal is one of my favorite tools -- it's a very basic Q&A with the computer. The python terminal especially is nice because you can work through simple commands and see how python will interpret them.

So I was just thinking how nice it would be if KM had a terminal, one where you could type in functions, tokens, etc., then hit return and see how KM interprets that string. Using the existing terminal would even be fine.

Does something like this already exist?

You can use the Value Inspector (Window menu).

Usually I just add a Set Variable to Text or Set Variable to Calculation action since they show the results. Or a Display Text in Window action perhaps.

I must not understand it. I tried this and it does nothing:

image

The thing I love most about the python terminal is I can do "dir(myVariable)" to see what that object contains. I wish there was some way to see all of the properties of %FoundImage%.

No such thing exists, though there's at least one quite advanced inspector macro for variables:

But something like what you want, I'm not sure it's possible to create by end users; I think Peter would have to add such functionality. (You can easily see any given values in a macro, but there's no overarching "terminal" that can provide the kind of insight you want.)

-rob.

No type of token works in a Calculation field, either in macros or in the Value Inspector. And some tokens don't work in the Editor or the Value Inspector window even when using a text field (although in theory The Architect might be able to make some tokens usable in these places.)

However you could write a macro that runs in a loop and assigns the token %FoundImage% to a global variable, like MyFoundImage, and then you could display that variable in the Value Inspector. That would basically be what you want in your example.

If that's what you want, then take my advice in the previous paragraph and you will achieve your goals 100%.

%FoundImage% is a token, not a variable. And it isn't available in the Editor:

That's because (my supposition here) it is local to the currently executing instance -- each executing macro, even each instance of the same macro, has its own %FoundImage% and the Variable Inspector has no context to evaluate the token in.

That's different to a "global" token such as %FrontBrowserURL%:

But...

"Local" tokens, including local variables, can be evaluated via AppleScript. From the KM Engine's dictionary:

process tokens v : Process the tokens in a string, returning the processed string.
process tokens text : the input string.
[instance text] : the macro execution instance (from environment variable KMINSTANCE).
→ text : the processed string.

So we can target a currently executing instance to process tokens if we know the instance's UUID!

Long story short:

Process Token For Executing Instance.kmmacros (6.5 KB)

Image

...which will let you do:

...to get:

image

Or even:

...for:

My usual shonky UX and lack of error checking, for a quick proof of concept. Run with it and see what (many!) improvements you can make.

I've been using KM for probably 20 years, but my mental model of it for this kind of thing is not good. It makes no sense to me why Calculation in the Value Inspector wouldn't accept tokens -- isn't it already converting whatever you're typing as text into something it can evaluate?

From the Wiki's "Token" page:

Tokens allow you to add textual information to token text fields.

Not numeric calculation fields. Just the same as in the Editor -- you can't do this:

image

...but you can do this:

image

See also the section on "Dot Notation" on the Calculations page -- note that:

In a Calculation field you can reference the numbers in a Keyboard Maestro Variable (which is always a string) using dot notation:

(Emphasis mine.)

Some tokens can be used in the Value Inspectors "Calculation" field, just as they can be used in any other numeric field. This will display the minutes of the current time plus one, for example:

CALCULATE(%ICUDateTime%m% + 1)

... but we have to use the CALCULATE() function to make the text token evaluate, just as in other numeric fields.

But even if you found a way to do that with %FoundImage% it still wouldn't help -- that token's value is specific to an executing instance and the Value Inspector only works with things that are globally available.

Did you try my macro above? It should do much of what you want. If not -- ask for more!

That's a good supposition, although not all tokens need to be instance-specific. That's why I said "in theory The Architect might be able to make some tokens usable in these places."

Have examples of some that aren't, given the usual rules of token handling in numeric fields? Because you can see from the above that %ICUDateTime% works just fine.

Then we can build a list, if only so people know which to avoid instead of tearing their hair out trying to get them to work!

Yes, if you use CALCULATE() around a token, then you can get tokens used inside Calculation fields, so I do regret my wording. Essentially, I was wrong. Sorry.

The CALCULATE() function is a useful tool, but it works with both expressions and numbers, so one may need to use some caution, as the following funny case returns an expression which currently evaluates and returns the value 2006:

image

In addition to CALCULATE() you can also use LINES(), WORDS() and CHARACTERS() which also normally take tokens.

It may also be worth noting that the Value Inspector updates itself about 3 times per second, while the KM Editor updates itself about 1 time per second, and the KM "Preferences: Variables" pane updates itself about 0.5 times per second.

See: Text Fields.

Tokens work in token text fields.

Numeric calculation fields accept calculations.

The two are quite different.

If they were the same, then to calculate 10 + X you would have to do 10 + %Variable%X%.

1 Like