I use AppleScripts for quite a few apps to get the bounds of their windows (once), and then hit a hotkey to set the bounds when needed. For example:
I'll run this once to get the bounds
tell application "Mail" to get the bounds of the window 1
And then use this whenever I want to set the bounds (reset the window position)
tell application "Mail" to set the bounds of the window 1 to {20, 43, 1300, 1348}
But occasionally I come across an app that doesn't support AppleScript, so can't use AppleScript to get and set the bounds of those apps' windows.
Once I've set the size and position how I want it for an app that doesn't support AppleScript, is there a way to record/grab the bounds of it's window with Keyboard Maestro?
After that, of course I can use Keyboard Maestro to set the bounds of that app's window with 'Move and Resize Front Window'
I'm trying to do this now and I'm finding that the token is not outputting the correct result.
I have tried every variation of %WindowFrame%1% or %FrontWindowFrame% that I can think of and it's not outputting the same bounds as Script Editor. No matter what I try it's outputting the incorrect result. Does anyone know what I'm doing wrong?
The application I'm trying to get the bounds of is Finder and there shouldn't be any issues there.
Undertaker01,
I have tried %WindowFrame%1% or %FrontWindowFrame% and they are getting the correct bound for Script Editor and Finder under the main screen
What version of OS are you running ?
Still running Big Sur! I haven't had any problems like this before, it could be the OS causing the issue, not sure.
It is outputting the first two values correctly, the second two are off by a decent amount and if I set the bounds to what Keyboard Maestro outputs it drastically changes the position of the window.
I'm trying to get the bounds of the finder window you see in the screenshot. The output script editor can be seen in the screenshot and the output of the keyboard maestro action.
The macro I uploaded isn't the same one as I was using earlier - it's the same one in the screenshot. I think I deleted the one I was working on earlier. I have so many macros that grouping them is an issue. Am I able to group groups together, so use them like actual folders?
Edit: And yes script editor and keyboard maestro are getting the bounds of the same window in the same position without it moving. The script editor output is where the window is actually located because if I set the bounds to script editor's output the finder window stays in the same place.
Unfortunately making subfolders for macros is not possible and Peter has mentioned it's unlikely to happen. I have over 1,100 macros and I group a fair amount of them according to what application they work in. Others I group by their main function, like clipboard macros, calculations and conversions, music etc.
As for your macro... when and how are you triggering it? Because if you trigger it from inside KM it will return KM's window information... not Finder's. Assign it a shortcut and then trigger it when you have a Finder window at the front and see what it returns.
I was triggering it with f16 on my keyboard and a keyboard maestro button on my Stream Deck. Was I using the correct actions for my macro? Is there a "better" way to get the bounds?
Any way you trigger it is fine as long as the app and window you need the bounds of is at the front.
Here's a screen recording of it working fine on my system. As you can see, it returns the Finder window's bounds correctly each time (I'm triggering it via a simple hotkey trigger).
Question: Do you have multiple monitors?
AppleScript sees all monitors as one giant canvas, which means if you have more than one, it will always return different results than AppleScript.
There are ways of breaking them down by monitor using AppleScriptObjC but that is way beyond my skill level.
Update: Since AppleScript sees all monitors as one canvas, to get Finder window dimensions, use System Events.
Download this macro and run it. It opens your Desktop folder, and then gets it's position and size via Keyboard Maestro and AppleScript and shows them in a window. See if they show the same thing or something different and report back.
I use an M1 MacBook pro with a displaylink dock that allows me to have 3 27 inch displays. I use an old iPad with duet so I technically have 4 displays, I use the macbook in clamshell mode.
This might be my issue, I'll download & try that macro later. Right now I only have the laptop.
Thanks!
Keyboard Maestro's %FrontWindowFrame% and AppleScript's Window Bounds are two entirely different animals.
Example Return of these tokens:
228,23,1314,1173
left,top,width,height
Note width and height...
This is analogous to what you get with System Events' window size and position attributes – but NOT window bounds when application scripting supports it.
Window bounds are:
Top-Left Window Corner X, Y and Bottom-Right Window Corner X, Y