Best Way to Test for Screen Size?

I use KM extensively for window management, and sync my macros across machines. What's the best way to have my macros check the screen size of the current device, so that they can take appropriate action based on that?

Use the SCREEN function anywhere you need to reference a location relative to the screen size.

SCREEN(screen|Main|Second|Third|Internal|External|Front|Back|Mouse,Left|Right|Top|Bottom|Width|Height|MidX|MidY)

The SCREEN function can give you the Left, Right, Top, Bottom coordinate (relative to the top left corner of the main screen), or the MidX or MidY (center), or the Width or the Height of the specified screen. And the screen choice can be the indexed screen (counting from the left), the Main screen (also the “0” screen), Second (left most screen that is not the Main screen), Third (leftmost screen that is not Main or Second), or the Internal or External screen (for MacBooks or iMacs), of the Front screen (screen with the frontmost window), or the Back screen (leftmost screen that is not the Front screen), or the Mouse screen (screen containing the mouse).

Lots of possibilities, so you can do pretty much anything you like.

1 Like

Thanks, Peter. What I’m missing is elementary help on how to use the SCREEN function in Keyboard Maestro. I mean, I know how I might use a simple function like SCREENCOUNT that would return a single number, but the wealth of parameters of the SCREEN function confuse me. Which do I have to specify in constructing a query? How do I query about a particular parameter and not any of the others?

Specifically, I need to distinguish whether the macro is running on my 13" MacBook Air (screen width = 1440) or my MacBook Pro in clamshell mode (screen width = 1920).

Why? The Why of it affects the answer as to how to use the SCREEN or other functions.

If you just want to detect if it is your MacBook Air or your MacBook Pro, use the %MacUUID% text token.

You can get the local MacUUID onto the clipboard with this action:

Or, if you really want to use the SCREEN function, you can use the calculation SCREEN(Main,Width) > 1600.

But the real question is why do you want to do this? Because maybe you don't really need to know which Mac your on, maybe what you need to know is the size of the screen. For example, you could move the mouse to the center of the screen like this:

But that is an inefficient way of doing it when you can just do this:

which would work on any Mac and any screen size.

Similarly, if you want to position a window on the Main screen, you can use the SCREEN function to position it regardless of the screen size.

(that example is just the "Move and Resize ➤ Full Screen" option, though its clear you can adjust the details as desired).

1 Like

Thank you for all the examples. I now have a lot to work with.

The MacUUID method would achieve my goals, but I prefer to build macros that will continue to work on computers that I don’t own yet,

I have used the SCREEN function in Move and Resize operations as you demonstrate, but I found that I wanted my windows sized and placed in different ways on differently sized screens, not adjusted by a percentage or number of pixels that is proportional to the difference in screen size. That’s what prompted my query. (For some reason I had conceptual trouble regarding how to use SCREEN to test a size instead of to set a size.)

Incidentally, the fourth screen shot in your reply tests whether the SCREEN function returns a MacUUID, which seems incorrect.

Thanks again for all the examples.

Yes, you may want to examine the size of the screen, and then position the windows appropriately (again using the SCREEN function). So perhaps if its wide, have two windows size by side (each taking the full half of the screen), and if it was narrow, have tow windows one above the other (each taking a full half of the screen).

Thanks. Fixed.

Just chiming in to say thanks for this. While the M1 transition has been smooth, my trusty "tell application "Finder" to set screen_resolution to bounds of window of desktop" stopped working, so I needed a way to find the width of the current user screen reliably. As usual, KM makes short work of things.

Interesting, "screen" has to be capitalized, e.g. SCREEN(Main, Width), not screen(main, width). I didn't know capitalization could matter here, but now I do.