Dynamically position pointer in a window

A newbie here. Once I get new pointer coordinates for a position in a window, say a Finder window, I save them to variables mX and mY. But when I paste the variables into a Move action, they don’t work to place the pointer at the same position in window, hopefully even if the window has moved. Could any of you share an example with me to get something similar to work? Thanks

Do you have it set to the front windows corner? Use the get button and then move the pointer to where you want it after clicking the window you are using. You can choose from the top left, right, bottom left and right as well.

image

image

Yes, I have it situated for the Top Lift Corner of the Front Window.

Maybe upload your macro to have a look at what you are trying to do.

See if this answers your question.

Group.kmactions (2.0 KB)

Keyboard Maestro Export

I use the first macro to find mouse coordinates in a Finder window that I want to save. I set them to the variables mX and mY and then display them.
I use the second macro to return to the saved mouse position in the same Finder window, even if it has moved. Doesn’t work.
KM Programming Macros.kmmacros (7.0 KB)

KM Programming Macros.kmmacros (7.0 KB)

What you have done works for me on the front Finder window so I am not sure what is happening for you. It moves to the cordinates you get from the first macro and then the second macro moves the cursor to that same position in the front Finder window.

Also I think you uploaded the same thing twice since both of those contain both macros.

Some progress here. I notice when I position the Finder window at the top left corner of the screen but below the menubar, the first macro ALMOST finds the right pointer coordinates. But the Y axis is lower than it should be by about 10 pixels when I run the second macro. Any way to make the first macro find more exact coordinates?

Also, none of this works if I fail to position the Finder window in the very top left corner of the screen. Is that always necessary, or can I get the first macro to work when the target window is somewhere besides the top left corner? Thanks for all your help.

I looked at your macros and see you're using the CurrentMouse token, and, unless I'm mistaken, I think that can only retrieve the absolute position of the cursor. But I don't think this should be a problem for you if you don't need to move the window before the next macro runs; you would just need to change the Move or Click Mouse action relative to absolute position.

(It's also possible to simplify your macro by deleting the two actions setting the mX and mY variables and plug the variable directly into the Move or Click Mouse action instead — unless you need those variables for something else of course, this is just an 'fyi'.)

I think that's the problem: They state the window will be at a different location. And that's the problem, because you're saving coordinates based on the top left of the display, and then telling the cursor to move that far from the top left of the window, wherever it may be.

If you really want this to work, you need to either save the location of the mouse relative to the front window, or adjust the position when moving to account for the new window location—I don't think there's an easier way, but I might be wrong.

To do that, regardless of which one you adjust, you'll have to take the window's current location relative to the display into account, then figure out the offset mouse coordinates from that top left corner location. The %WindowFrame% and/or %WindowPosition% tokens would be useful here.

-rob.

Oh whoops, I missed that part

Thanks for the fyi about getting rid of intermediate variables. Works well for me. Just one question. How do I change the coordinates? I’ve tried deleting the Move action an inserting a new one, but it still keeps going back to the coordinates or position it was in before.

Yes absolutely, nailed it.

I might be missing what you are asking, but you can type in the coordinates you want without retrieving any. If you are trying to click somewhere, specifically see my above example to add to or subtract from if you need to.

If I'm understanding your question correctly...

You can enter the same calculation you used to set your mX and mY variables directly into the Move or Click Mouse action in place of where you have those variables (I'm not sure if that makes more or less sense). I included an image of the action in my previous post, though maybe you can't see it in the email? But essentially this inside your move mouse action:

mouseCoordinates[1]
mouseCoordinates[2]

In case you missed @griffman's spot-on diagnosis amongst the discussion about variables -- when you get the mouse position the coordinates are "absolute", where 0,0 is the top-left corner of your main display. So you also need to get the absolute position of the top-left corner of the front window then do a bit of maths to get coordinates "relative" to the frontmost window's position.

This is the concise version:

image

...and you'll find links to MOUSEX(), MOUSEY(), and WINDOW() on the Wiki's Functions page.

Here's your "getter" and "setter" macros:

Mouse position getter.kmmacros (3.9 KB)

Image

Mouse position setter.kmmacros (3.1 KB)

Image

1 Like

Sorry if I was unclear. I am simply using "Move and Click at mouseCoordinates[1] and mouseCoordinates[2] relative to front window’s corner (top left)". It worked well the first time I used it. I’m not using mX or mY coordinates at all. However, when I want to change mouseCoordinates[1] and [2], I’ve tried several things, but the coordinates won’t change. The action keeps taking me back to the original window coordinates.

Are you re-running the first macro where you define your mouseCoordinates variable? If not, then the variable will still have the previously saved data. You'd need to run the first macro again to store new coordinates in the variable; or, alternatively, include the action that defines your mouseCoordinates variable in the second macro as well (or instead of if you don't need to have two separate macros).

I downloaded the macro that Nige_S provided. I placed my pointer in a Finder window then ran the "Mouse position gutter”. It returned some coordinates. What should I do with them?

I ran my simple Move and click macro again with the actions mouseCoordinates[1] and mouseCoordinates[2] relative to the front-application top left corner. It still placed my pointer back to the first position.

Can you use the "Get" button in the macro action and just use the numbers it returns and not have two macros as mentioned?

image

Are you trying to change the position frequently and need to run one macro before the other? What are you trying to click on specifically, or why do you need your mouse pointer moved to a specific location on the front window, and perhaps there is another workaround that might help you.

Yes, but then if I move the window and run the macro again, it places the pointer absolutely rather than relative to the window’s top left corner.

I think I will just go back to running things in two steps using mX and mY variables, then using then in the second step. The one step way with no intervening saved variables just doesn’t seem to work for me when I want to change coordinates. Thanks for all your help.