KM automation: Auto get window locations and size

I’m planning on setting up “Project Desktops” or “Workspaces” that will arrange a set of windows based on the needs of the project.

Both Moom and BetterTouchTool have quick and easy ways to capture and use app window locations and I’d rather do it all from within KM.

I find it a bit tedious to capture and populate the fields within the move and resize action and would like to simply move a window and set its size and run a macro that records location and size into the move and resize action for building my workspaces.

Anyone have ideas or has done this already?

Yep, I have a macro that does much of what it sounds like you want.
Here it is -- warts an all. LOL
I wrote this a couple of years ago, and my knowledge and style has changed substantially since then. If I wrote it now it would probably be much different. But maybe it will give you some ideas and a good starting point.

MACRO:   Move and Set Window Size Per App & Win Name

~~~ VER: 2.0    2018-04-23 ~~~


Move and Set Window Size Per App & Win Name.kmmacros (33 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

Please feel free to ask any questions. So of the Actions may not be obvious.

Hey @JMichaelTX, thank you for this! Just this month I've read my first intros to AppleScript. I'm at the very beginnings of putting some organization into leaning scripting. Me: 01.
With this level of understanding I see 02
looking at this macro.

Just m a y b e I'll live long enough to learn to do something useful and maybe it will always just be a lot of banging at the keys making noise...

While I'd love for someone to endlessly sit on the bench, keep me company, and speak words of encouragement, I'm not sure even my mother if she were alive would be up for the task.

This will sit with me as an inspiration to keep practicing and I'll reply as my understanding grows and I have something useful to ask.

Meanwhile, I'll use XScope's Crosshair to readout the window locations so I can move forward with building these "project spaces".

Really, thanks again ("thanks" doesn't express my full appreciation) for your time and all the other postings of yours and others that I'm gleaning much from!

Ooooh, so this thing actually works!!! Wow, cool!!! I set a window location and then long press the hot key and it somehow records the selected window’s location, then when I short press the same hot key it moves a selected window back to that same location. So it must be recording the window’s location then applying it.

Where are the values it records stored? Can I see them? That would be faster then using the crosshairs to get the locations.

Did you read the “How to Use” comment Action in the macro?

Here are the comments from the “How to Use” comment Action that I see:
This maco provides two functions, triggered by the same Hot Key, but one with a long key press, and the other with a normal short key press.

In both cases, you start by selecting the target window.

  1. Long Key Press – Add the current window to the Master Window List
  2. Short Key Press – Move and Resize the Current Window per the Master List

As uploaded, it uses a hot key of ⌃⇧Z
The Master Window List is a KM Variable: DND_MSW__WindowList

Sooo, that it says it provides two functions I could assume that means two and only two and not anything but two functions. My asking for a stored value would be a third function and it doesn’t say it has three functions, only two functions. It this what you are pointing to?

It also says "The Master Window List is a KM Variable: “DND_MSW__WindowList” I thought maybe if I find the variable “DND_MSW__WindowList” that the values representing the window location would be there but that didn’t work out so well. There are numerous references to “DND_MSW__WindowList” but none display any pixel references, just screen location descriptions like TOP or LEFT.

There is a step within the REPLACE Window Frame for APP that ends with “to Source”. Is that how the macro “picks up” the window location?

How does the macro know where to place and resize the window that it gets from the Long Key Press? There are actions to detect a long press which set the variable “MACRO_Option” to on or off and if on set the variable “DND_MSW__WindowList” to “APP_NAME,WINDOW_NAME,TOP,LEFT,WIDTH,HEIGHT” which I do not know the significance of. Then comes the PROMPT User to set Mouse Position which I’m guessing is what make the dialog window pop up. Then comes a bunch of defining the window’s name actions and actions to clean up that name. Then comes Update WindowList actions which look for and if not found, add Window Frame to list, then the variables that got set earlier get deleted. That seems to be the gist of what’s happening as best as I can tell.

Actually, having a "stored value" is part of both of the two, and ONLY two functions this macro provides.

Did you look at the contents of the "DND_MSW__WindowList" variable in the KM Variables Panel? It should look something like this:


Did you see this Action in the macro?


If you will expand it, it will show you all of the details.

@BernSh, as I stated above, I am providing this macro simply as a way to "give you some ideas and a good starting point" . It is NOT a polished, finished macro intended for release to the general public. It is not the cleanest macro I have ever written, but if you open up the KM Variables Panel, and step through the macro in the KM Debugger, you should be able to understand what is being done. If after some study and testing, you are really stumped, then I'll try to answer a few questions.

In the end it is probably best if you write your own macro, drawing from my example macro as needed. This was a challenging macro to write, and it required numerous trial-and-error steps on my part to get it done.

Good luck.

This was the missing piece! As silly as it may sound, the word "Variable" has intimidated me until my recent change of heart and new willingness to take on coding. As part of being intimidated, I've been avoiding doing or looking at anything with the word "variables" nearby. It was kinda like how the word "Voldemort" was treated in Harry Potter's world. An outcome of this is while I've read or scanned most of the basic materials including scanning thru all of KM's preferences MULTIPLE times, I was looking with a deliberate blind eye. The "Variable Preference Pane" had no meaning for me and I scurried by (throwing in a silent blessing and crossing myself for good measure) as quickly as possible without giving it a thought. Oh the things us unwashed, ignorant heathen do to get thru our wretched lives...

BTW searching for "KM Variables Panel" or just "Variables Panel" in the wiki doesn't get you there. You have to search for "Variable Preference Pane". A small tweak that allows the use of Pane or Panel would have helped this newbie.

Now that I'm on speaking terms with Mr. Variables I was free to do a search on the old man and found this coolness:

My coding world had gotten a bit bigger and brighter thanks to this exchange. Thank you for hanging in with this drama queen as I slowly pry these very stubborn old eyes open. I now have a new tool to follow your lead:


1 Like

5 posts were split to a new topic: Understanding Variables

@DanThomas: Done.

Good point. I have revised the KM Wiki Article on Variables – View Your Entire List of Variables TBD, and now a search on “Variables Panel” will find this.