How to access screen width?

I want to access screen width and do something different based on screen size. But I have difficult to access the screen width value. See below screenshot. From %Screen%Main% we know the screen width is 2304. Now I tried to use it as if/else condition but I am not able to get the value tried different ways to access the width value. What is the correct way to do it?

The Keyboard Maestro editor can only show you results based on the existing values of variables - not on what they might be if you ran the macro.

So you can see here:

image

That the current value of the ScreenFrame variable is empty.

Since the ScreenFrame variable is empty, the calculation ScreenFrame.Width > 2300 is currently invalid, though it is syntactically correct so it displays as Orange not Red (Orange means the calculation is invalid currently, but with different values in the variables it could be correct; Red means it is incorrect and changing the variables will not make it correct).

This is why the second test:

image

Shows in Red, and will always be false, because you can't use text tokens in calculation fields.

Same for the final test condition.

Select Try from the gear menu of the first action, and it will set the ScreenFrame variable, which will then result in the calculation working (although the color of the text will not change from Orange to Black until you click in the field).

BTW, you can also just use the SCREEN function and the calculation:

SCREEN(Main,Width) > 2000

No variable is needed at all for that test.

Thanks @peternlewis! This works perfectly.

Is there any document for explaining when to use %Calculate% when no needed? (the orange color tips is very useful too!).

For numeric field like the width field in move and resize, what will be the syntax if I want to pass a variable to it?

If you type a space into the field you highlighted the field "expands" so the action then looks like this:

image

See how the field has changed?

Now look at the right-hand end of the field - it's got a small c there which indicates that the field is a "calculation" field and so expects a calculation-type expression. You can read about the different kinds of fields that KM supports in the KM wiki here Text Fields [Keyboard Maestro Wiki]

As for the variable question - to use a variable called NewWidth, say, you would enter it into the field just like that:

image

BTW - it's shown in orange because, until the action is actually run in a macro, the variable NewWidth currently doesn't have a value... There is some chatter in the KM forum about that.

And, for example, if I'd entered a variable token in the field it would show red since it is not allowed in a calculation field:

image

I hope this goes some way to answering your question. Cheers!

3 Likes

See: Text Fields

You have to know when it is a calculation field, or a token field. Keyboard Maestro shows you which kind of field it is when you edit the filed, with the indicator in the top right corner of the field.

A calculation field takes a calculation, so never has any % Tokens (but it can have Functions).

@tiffle gave some great additional information.

2 Likes

thank you both. have a good weekend!