How to Resize a Window to a Percentage of the Available Screen Size

Note: in my setup, I work with a couple of different monitor configurations; sometimes I'm on the laptop's native screen, other times I'm mirroring to a 4k display so my monitor is a different resolution.

I'd like to design a KM macro that can get the current dimensions of the screen, then resize specific windows to specific percentages. For example:

Get current size of main screen (x,y)
Resize window 1 to 100% of x, 66% of y
Move window 1 to position x=0, y=.33*y
(this last command would move the window 1/3rd of the way down the screen, since the previous resize command tells it to resize to a 2/3rds height)

I'm having trouble understanding how to get the variables for this, and then how to apply it to Move or Resize Window actions.

You shouldn't need to get Variables for this as Keyboard Maestro has inbuilt Window Functions. I think the below is what you are trying to do. If it isn't you should be able to adjust the numbers/maths.

For positioning "Top" and "Left" refer to the top left corner of the screen. "Height" and "Width" are the height and width of the screen.

The window is positioned by its top left corner.

It can get quite confusing working out the formula and always takes me some trial and error...

You can play with this by running the Macro in the Keyboard Maestro Editor and letting it resize the Keyboard Maestro Editor Window, adjusting the numbers/maths until you get what you want.

Also - you can use fractions instead of percentages if that gets a better result.
* 2/3 can be used instead of * 66%

EXAMPLE Resize and Move Front Window.kmmacros (2.4 KB)

2 Likes

Not exactly the same, but much easier. If you can do without exact figures, you can use this action

image

Then press the "Get" button and you can draw the window with the mouse.

1 Like

Thank you both! These are great solutions.

One question @Zabobon : when I try to set up the Move and Resize action, my action looks different than yours - the fields only seem to accept digits. Why does yours look different than mine?

EDIT: Never mind, solved it. I had to choose Move and Resize > Full Screen instead of Move and Resize > Custom (which is what yielded the screenshot above). SOLVED.

Hey David,

All you have to do is start typing letters in one of the fields, and it will automatically accept variable entries.

-Chris

3 Likes

Adding another, slightly more complicated, version...

Various people have reported problems with properly moving and resizing windows lately. It's an OS thing, not a KM issue, and seems to happen if, before the window is moved, the new width or height would put a window edge off-screen. One solution is to repeatedly move/resize the window until it's frame is as you want:

Resize a Window (Loop).kmmacros (4.9 KB)

Image

You can't position a window on fractional pixels, so calculation results are forced to integers (for a single action that doesn't matter, KM rounds to fit, but here you could have an endless "While" loop). I've FLOORed the calculations to get integers -- I haven't thought it all the way through, but my gut tells me that ROUND could lead to overlaps or sizing errors if doing multiple windows and I'd rather have 1-pixel gaps instead. And, just in case, the "While" will stop after a maximum 10 attempts anyway.

2 Likes