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:
1 2 3 4 5 | tell application "System Events" set appOfInterest to "Safari" set windowXY to size of the first window of application process appOfInterest display notification (item 1 of windowXY as text) & " x " & (item 2 of windowXY as text) with title "Frontmost window of " & appOfInterest & ":" end tell |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | try tell application "System Events" set appOfInterest to name of application processes whose frontmost is true set currentApplication to item 1 of appOfInterest set windowXY to size of the first window of application process currentApplication display notification (item 1 of windowXY as text) & " x " & (item 2 of windowXY as text) with title "Frontmost window of " & appOfInterest & ":" end tell on error number -1719 display notification "Error: No open windows!" with title "Frontmost window of " & appOfInterest & ":" end try |
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.
I appreciate your work. But I would like to set frontmost window to a fixed position of the screen?could you give me some advice?
Our app Moom will let you do that, as will almost any other third-party window management tool. None of them will be able to affect how/where the app opens the window originally (they can't override it), but you can then use them to easily size/move the window where you want it.
Using Moom, you'd create a new custom control using Move and Zoom and drag out the size and location. Or you could do a multi-part action (same shortcut key for all steps) where step one sets it to a precise pixel size with Resize, and the following steps use Move to Edge/Corner and Move to put it exactly where you want it.
-rob.
Hi Rob, have you tested and/or modified this script for Big Sur compatibility?
On my end, under Big Sur.4, the script in a KM macro or in Script Editor results in a notification that's missing the current window's dimensions, displaying only "Frontmost window of : Notification"
Is this fixable?
Thanks in advance!
Both seemed to run fine for me in Script Editor, as did the Keyboard Maestro version. Wish I could offer some help, but I have no idea why you'd be seeing that error.
-rob.
Hi Rob, thanks for your reply.
Clarification: I'm not seeing an error; the notification banner contains only title, application's name, and the colon. The app's window's dimensions are missing.
Same outcome with notification banner and alert.
:O(
My macro and running in Script Editor are both returning the window dimensions, at least for Safari.
-rob.
Hi Rob,
Thanks again for replying. :O)
I stumbled upon a solution.
When Script Editor in Notification System Preference is configured for Show Preview Never, the above symptom happens.
When instead it is configured for Always, then the notification does include the window's dimensions.
Comments are closed.