How to Determine if a Window is Being Moved?

Is there any way to determine whether or not, at the moment that I press a hotkey, a window is being dragged by the mouse?

I don't want a macro that will drag a window. I want to sometimes press a hotkey while I'm doing nothing with the mouse and sometimes press the hotkey to do the same action, but differently because there's a window involved. It would be nice to make that distinction by the fact that the window was being dragged by the mouse at the time I pressed the hotkey.

I am not finding anything in the KM doc, nor in the AppleScript doc, but I haven't looked very far in AppleScript.

KM does have an IF condition of the mouse button being pressed. Maybe all I need is for the mouse to be held down when I press the hotkey with my other hand.

Any ideas?

Hey August, my first thought is to use the focused window trigger and set it to focused window's frame changes... but the implementation might be really weird/complicated. I wouldn’t even know where to begin.

Screen Shot 2022-02-13 at 07.41.06

I have an idea that should work 99% of the time. Is 99% good enough? Your macro can simply check if the X,Y coordinates of the front window are changing over a very short period of time. One way to do that is examine the value of the %FrontWindowPosition% token.

So the macro would take a measurement of that token as soon as it starts, pause for 0.1 seconds, then take another measurement. If the values differ, then the user is dragging the window.

This would only work if the user is not holding his mouse still, but actually moving the mouse.

Thanks Chris, thanks Sleepy,

I've been exploring this idea that I mentioned in the OP:

The hotkey is for changing Desktop Workspaces. If I can trigger a change to a new desktop with a hotkey while I am holding a window in motion when I do, the window comes along with me to the new desktop. This works with the core Apple Mission Control hotkeys for desktop changing and it works with every other system I've found of changing desktops, as long as you can hold on the to app window with the mouse.

But I'm working on changing desktops based on a Prompt With List action, and there is no way that I can respond by picking from the PWL List without letting go of the app window that I'm trying to move. (The programming of PWL is so much simpler than the long way around the barn of creating a Conflict Palette by creating a separate macro for each choice in the List.)

So in my macro I want to be able to distinguish between whatever just happens to be the frontmost app window when I invoke the PWL and the times when I really want to be moving that frontmost app window to the new desktop. I think that simply determining the mouse state when the macro starts (is the left mouse button down?) is enough. I don't need to actually know that anything has been moved, I just need to know that it has been chosen.

In my current version of the macro, if the mouse button is down, then I give a 3 sec delay before moving, and tell the user to re-grab the window after the PWL List disappears. It works reasonably well.

But my KM perfectionism wants to be able to automatically re-grab the window, to save that step.

So, after telling the Desktop changing macro where I want to go, how do I get KM to click and hold the title bar of the previously selected window, after I've let go of the window to be able to make the PWL choice?

I can see two routes:

  1. If the mouse is down, determine the active window. After the PWL choice, bring that window to the front (or assume it already is) and find the title bar and click and hold. Then perform the PWL choice to move to the new desktop and release the mouse.

  2. If the mouse is down, remember the mouse location. After the PWL choice, move the mouse to that location again and click and hold. Then perform the PWL choice to move to the new desktop and release the mouse.

Just writing it out to explain what I'm trying to do has clarified my thinking. I had been working on the first idea, based on the "what", what I'm trying to do is hold the window by its title bar. Now I think the second idea might be simpler based on the "how", how I'm holding the title bar is by having the mouse button down and using the position of the mouse.

Any other ideas? Does anyone foresee any problems with either of these routes?