Comparing Numeric Variables

Am setting Variable X to clipboard value.
Tabbing to next field, then setting Variable Y to new copied clipboard value.

I want to test for equals, greater than, and less than in If…then statements.

However, I cannot work out how to state if Variable X is equal to Variable Y then…else.

Obvious when you know how - I don't. Ideas? Thanks.

Michael

The trick to referencing variables in Keyboard Maestro is to be aware of what kind of text field you're working in at any given time. Variable and Calculation fields generally expect variable names to be written as-is (in your example, just X and Y), whereas in text token fields, variable names must be written like %Variable%[VARIABLE NAME]% (so in your example, %Variable%X% and %Variable%Y%). Fortunately, KM8 (which I can tell you're using from your screenshot) makes this much easier by displaying a small V, C, or T at the right side of the current text field to indicate which kind of field you're working in (you can see this in action in your screenshot, which shows a V at the right side of the current text field, Set Variable Y).

In an If Then Else action, if you click in the is = field (the one that says "value" in your screenshot) you will see a small C on the right side of the field, indicating that If…then statements are calculation fields, and thus expect "plain" variable names. Therefore, to see if variable X is equal, lesser than, etc. to variable Y, you would type Y in the appropriate field:

As you said: obvious when you know how, but definitely not obvious when you don't :slightly_smiling_face:
Hopefully it's a bit clearer now!

On a different note, if I may offer a suggestion based on your stated desire to test two variables for multiple conditions (i.e. equals, greater than, less than, etc.) I would recommend looking into using a Switch/Case action instead of an If Then Else action, as it makes testing multiple conditions against the same data much easier. Example:

Feel free to post again if you have any more questions!

2 Likes

This is so clear - a huge help.

Many thanks

Michael

1 Like

@gglick covered most of this, but I thought I'd add some screenshots for additional clarity.

In order to perform calculations, you must either be in an Action Calculation field or use the Calculate token (KM Wiki).

So, how do you know when the input area for an Action is a "Calculation" field, vs. a "Text" field?

Well, when you click in the input area you will see a small indicator in the far right of the field that will be one of the following:

like this:

Action with Text Field

This field expects everything you enter to be explicit text, unless you encode it with the % delimiters, indicating a Token (KM Wiki) of some type.
To perform calculations here, you must use the Calculate token (KM Wiki).

Set Variable to Text action (KM Wiki)

Action with Calculation Field

This field expects explicit Variable Names (without the % delimiters) or Functions (KM Wiki), or Operators (KM Wiki).

Set Variable to Calculation action (KM Wiki)

# Action with Variable Field

This field expects the actual Variable Name, OR, in some cases, a Variable Token that will evaluate to another Variable Name

If this all seems a bit confusing, then be assured this is often confusing to new KM users, but will soon feel like second nature, or muscle memory, after you have built a few macros. :wink:

For more information, see the KM Wiki:
FAQ -- What is the difference between text token fields and calculation fields?

Questions?

3 Likes

This thread is exemplary, imho. Chapeaus all 'round. :smiley:

1 Like

Out of interest this is what I ended up with - happy bunny :grinning:

1 Like