The Robservatory

Robservations on everything…


How to display the size of an app’s frontmost window

A user asked me a question about Moom

Say I’ve resized a window to the dimensions I want. Is there a way to figure out what these are so I can create a resize action in Moom?

Basically, the user wants to save a window size as a custom action, to make it easy to reapply that action to any window. (If it were just one window in one app, you could use Moom's Save Window Layout feature to save that layout for easy recall.)

There is a way to see this info in Moom, but it requires enabling our debug log and digging through a bunch of output. As an easier alternative, I was certain that AppleScript could do this; I fiddled a bit on my own, and did some web searching, which led me to this thorough post on StackExchange.

Using the very first bit of the first script there, I came up with this version:

Run the above, assuming Safari is running and has an open window, and you'll see this system notification:

Change Safari to whichever app you're interested in, re-run the script, and you'll have that app's window dimensions. This script is incredibly basic (no error checking, hardcoded app), but it works1If you see a message about ScriptEditor needing Accessibility access, open System Preferences > Security & Privacy > Privacy, click on Accessibility in the left panel, click the lock icon to unlock the panel, click the plus sign to add an app, and navigate to Script Editor in Applications > Utilities, then click Open..

Of course, me being me, I decided I'd spend a couple hours making it more useful, even though I probably won't use it all that often. So I modified it to work for whichever app is frontmost, and made it run from Keyboard Maestro. I then assigned it a gesture trigger with my mouse, so I can easily see any window's dimensions with a simple mouse movement.

My updated script is even more closely matched to the one in the StackExchange posting, so all credit to bobtiki for posting the original. Here's my somewhat modified code:

The only really new bit is the addition of the error trap: About the only thing that can trip up this simple AppleScript is an app without any open windows, so that's what the on error number bit traps and displays a message to let the user know what happened.

As this wouldn't be very useful when run in Script Editor (as that would be the only app it'd ever see as frontmost), I converted it into this Keyboard Maestro macro:

You can also download the macro [2KB] if you prefer, but it's quite simple as you can see. I have it set to trigger when I move the mouse upwards while holding down the Command key, and this works quite well.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Robservatory © 2021 • Privacy Policy Built from the Frontier theme