Variable, Token, Function Question

Upfront, I am sorry to ask this question. I did review the Wiki and a number KMF posts and I cannot see the answer to this question which is fundamental knowledge to all you guys but not to me.

What I can never remember is how to actually enter variables, tokens and functions into KM actions. Every time it has been a frustrating trial and error process.

Specifically, which actions require:

Variables in the format of: %Variable%Var10% vs %Var10% vs Var10

Tokens: %Token%LongDate% vs %LongDate% vs LongDate

Functions: WINDOWCOUNT() vs %WINDOWCOUNT()%

Reading a forum post, I think the answer to this is that the only way to use a function is to first SET VARIABLE TO CALCULATION and in the "to" field in that action use WINDOWCOUNT() instead of %WINDOWCOUNT()% I know this works but am I correct with the "the only way to use a function in an action is..." part?

Can tokens be inserted straight into an action which requires a number or array from a token or tokens also cannot be inserted themselves into an action and they have to be used like functions (assign to variable first)?

Is the answer "depends"? Where there are a class of actions which require the "assign to variable first" and another class of actions which will accept a function or token directly? If yes, how do I tell the difference?

Finally, I cannot figure out the .left or .top type of parameters of a variable created from a function. Example: SET VARIABLE Coords to CALCULATION WINDOW() and then I want the Left parameter from the variable Coords, I cannot figure out if I use:

SET VARIABLE WinLeft to Text %Variable%Coords.left% or some other %xxx% formatting.

How to use it in an IF Coords.left is 100 THEN context?

Is it IF %Variable%Coords.left% is 100 OR IF Coords.left is 100 or something else?

Of course I can figure this out by trial and error but that takes time every time and I can't remember which one worked the next time I need to use it. If I just understood the logic, I will know which one to use. This is not from lack of trying, for example, I cannot find anywhere (>20 mins of searching) in the Wiki where there is an example of an action that accomplishes the parameter extraction of: SET Variable TO Var10.left

Thank you in advance for the newbie sounding question. (Kind of embarrassed to ask this after months of using KM daily and creating about 100, somewhat complex (>20 actions each) macros with a fair bit of AppleScript calls, all of which work well.)

Alex

Hey Alex,

Take a look at this:

TIP: Understanding KM Variables, Tokens, & Functions

Keep in mind that when you place the cursor into an input field, you will see a visual cue in the upper right corner of the field:

C == Calculation
T == Text
V == Variable

Calculation Fields can take Functions and bare variable names

Text Fields can take Text-Tokens and variables in the format:
⠀⠀⠀%Variable%YourVariableName%

Variable Fields should be self-explanatory.

Never use the highlighted format for a variable. KM allows it, but you won't be able to tell by looking what's a text-token and what's a variable.

Always use the %Variable%Var10% format, except in calculation fields and variable fields.

There is no such format for tokens.

That's meaningless. It is only valid within the context of a text-token.

Functions are NEVER tokenized.

See these menu items:

image

See the Insert by Name submenus for each.

Nyet.

You can use functions in text fields IF you use the calculate text-token:

%Calculate%WINDOWCOUNT()%

There is no such function as WINDOW().

Try:

WINDOW(left) or WINDOW(2,left) or WINDOW(2,right)

Take notes. Make yourself a cheatsheet.

-Chris

2 Likes