Resizing Windows Like Spectacle

I've been using Spectacle to resize windows, but I realize this is silly, since I already have Keyboard Maestro.

But there's one feature that I don't know how to replicate in Keyboard Maestro: you can press the same keyboard shortcut several times and have the window resized in different ways.

For example, if you use ⌥⌘← to "resize to the left 1/2 of the screen" you can press ⌥⌘← again to have it reduce to 1/3 of the screen, and if you press ⌥⌘← yet again, it will resize to the left 2/3rd of the screen.

Pressing ⌥⌘← again will bring it back to the left 1/2 of the screen.

The same can be done for "right 1/2 of the screen" or "top 1/2" or "bottom 1/2".

This is how Spectacle's window resizing FAQ explains it:

Windows may also be resized between thirds using any of the shortcuts above. For example, to have a window resized between 1/3 and 2/3 of the left region of the screen simply activate the left half ⌥⌘← keyboard shortcut more than once. Each time the shortcut is activated Spectacle will move the window between 1/3, 2/3, and back to 1/2 of the left side of the screen. This feature also applies to the upper left, lower left, upper right, and lower right shortcuts.

Spectacle can also move windows between horizontal and vertical thirds of the screen. The ⌃⌥→ keyboard shortcut will move a window to the next third of the screen, starting with the horizontal third region on the left of the screen. ⌃⌥← will move a window to the previous third of the screen.

I have no idea how to do this with Keyboard Maestro. I assume it would require checking the size of the current window, but I'm not sure how to do that.

I'm hoping that maybe someone has already done this.

This can be done fairly easily with a Switch/Case action and a variable tracking which window size to set the next time the macro is run. Here's one for the left side of the screen that should behave how you describe; I imagine you should be able to adapt it to the other sides as well without too much trouble.

Resize Window By Half and Thirds (Left Side).kmmacros (6.7 KB)

1 Like

The down side if tracking how many times you have triggered the macro like that is the need to reset it at some point. For example, if you press ⌥⌘Left Arrow, and then half an hour later press it again, you don't expect it go to 1/3 size.

There are a couple of improvements that can be made:

  • Have the WindowSize reset back to 1/2 after some time. This would work, but it is somewhat tricky to go.
  • Remember the time of last execution, and reset the WindowSize to 1/2 if it has been a while. This is pretty straight forward.
  • Instead of remembering the last/next desired size, base the macro execution on the current size of the window.

This last technique is probably the best. It means if the window is already at 1/2 size, it will immediately go to 1/3 size. So you might do something like:

  • Set Variable WindowWidth to WINDOW(0,Width)
  • If calculation WINDOW(0,Width) > SCREENVISIBLE(Main,Width)*51%
    • Resize to 1/2
  • Else if calculation WINDOW(0,Width) > SCREENVISIBLE(Main,Width)*35%
    • Resize to 1/3
  • Else
    • Resize to 2/3

You might want to adjust the behaviour, but this sort of behaviour, where you can tell the current state and then choose the new state is a good solution for a macro that you want to perform multiple variants.

2 Likes

Great suggestion. Revised, tested, and confirmed to work nicely:

Resize Window By Half and Thirds (Left Side).kmmacros (4.7 KB)
image

EDIT: Tried slimming the macro down just a bit more by eliminating the variable altogether and using only calculations.

1 Like

A problem that I only just realized is that it has to work across different apps, so if I am in BBEdit "maximized" and press ⌥⌘Left Arrow then I want it to go 50%, but then if I switch to Finder and it is "maximized" and press ⌥⌘Left Arrow I still want it to go to 50%, not 33% just because the last app that I resized was at 50%.

So really the only option is to calculate based on the size of the current window in the current app.

I'll give that a shot. Thanks!

Ah. so here's an interesting tidbit… what happens when an app refuses to go to 33% of the width of the screen?

For example, on my 12" MacBook, I can't get Safari to go that narrow, which means that @gglick's method didn't work for Safari, but did work for Finder and other apps that don't enforce a minimum width.

My solution, then, was slightly different:

  • If the width is > 50% of the screen, make it 50% width.
  • if the width is exactly 50% of the screen, make it 33% width
  • otherwise, make it 66% width

This still isn't foolproof. The Keyboard Maestro editor also won't go less than 50% of the width of my screen, so when it tries to resize to 33% it fails, and stays at 50% which means it never gets back to 66%. Not sure how to fix that.

(Also note that I changed the Hot Key to Hyper+Left Arrow)

Keyboard Maestro 8.2.4 “Resize Window By Half and Thirds (Left Side)” Macro

Resize Window By Half and Thirds (Left Side).kmmacros (5.2 KB)

@peternlewis - ah, going to need some more help here… when I tried to do the same thing for the right side, I get the 50% easy enough, but when I used this to reduce the size to 33%:

SCREENVISIBLE(Main,MidX)
SCREENVISIBLE(Main,Top)
SCREENVISIBLE(Main,Width)*33%
SCREENVISIBLE(Main,Height)

it resized from the right side of the screen towards the center (meaning that there is now a 27% "gap" between the right side of the window and the right side of the screen).

Maybe a screeenshot will help. Here is what TextEdit looks like after going from 50% to 33%

I suspect I might similarly need help with either top or bottom to not run into the same issue there.

Yes, because the left coordinate is still MidX (middle). Instead, use:

SCREENVISIBLE(Main,Left,66%)

That is a short form equivalent to:

SCREENVISIBLE(Main,Left) + 66% * SCREENVISIBLE(Main,Width)

(MidX is just 50% instead of 66%).

Yes, lots of windows have width minimum requirements. The only way to solve that would be to check the window width afterwards, and if it has failed, resize it to full size.

1 Like

A post was split to a new topic: How Do I Move and Resize a Window to Fit Against Right Side of Screen?