Dynamically position pointer in a window

Nothing -- you had a "Display" action in your macro, so I left it in so you could see the coordinates that were being returned. Delete the action if you don't want it.

If you run the "getter" it gets the current pointer position relative to the frontmost window. If you move the window then run the "setter" it will put the pointer in that same relative position in the frontmost window.

Thanks. That works very nicely. It is, however, a two-step operation, like where I started with saving variables in the first step and using them in the second step. But I like your solution better and it is more accurate than the Current Mouse variable I was using before.

It's a two macro operation -- one macro to get the pointer position, the second to set it. And it should be just as accurate as using %CurrentMouse% -- it's just a different way of getting the same information.

From your OP it sounded like you wanted to get the pointer position, move the window, then reposition the pointer -- perhaps when moving a window from one display to another -- but you gave no info about how you're moving the window. That means using two macros, along with a global variable, to get and set the pointer since there's no way to know when you've started, or stopped, moving the window.

If what you're actually doing is finding a particular place in a window once -- a button position, for example -- and using that all the time in your macro you don't need the "getter". The KM Editor has a "Mouse Display", available from the Window menu, which shows you the mouse coordinates relative to any of display or front window corners. Get the number from there when the top-left window anchor is selected and you can use the numbers directly in the "Move Mouse" action with the "relative to the front window's top-left corner" setting.

Thanks again. This was the silver bullet I have been looking for.

Also if you are just trying to relocate Finder windows you can easily record that with ScriptEditor or here is an idea for a starting AppleScript. Sounds like you already have what you are looking for though.

tell application "Finder"
  activate
  
  -- Get screen size
  set screenBounds to bounds of window of desktop
  set screenWidth to item 3 of screenBounds
  set screenHeight to item 4 of screenBounds
  
  -- Calculate window size
  set windowWidth to screenWidth / 3
  set windowHeight to screenHeight / 2
  
  -- Define folders to open
  set foldersToOpen to {"/Library/LaunchAgents", "/Library/LaunchDaemons", "~/Library/LaunchAgents", "/Library/LaunchAgents - Archive", "/Library/LaunchDaemons - Archive", "~/Library/LaunchAgents - Archive"}
  
  -- Close existing windows
  close every window
  
  -- Open and position Finder windows
  repeat with i from 1 to count foldersToOpen
    try
      set folderPath to item i of foldersToOpen
      
      -- Handle paths with tilde
      if folderPath begins with "~" then
        set folderPath to (POSIX path of (path to home folder)) & text 3 thru -1 of folderPath
      end if
      
      set col to (i - 1) mod 3
      set row to (i - 1) div 3
      
      set leftEdge to col * windowWidth
      set topEdge to row * windowHeight
      set rightEdge to (col + 1) * windowWidth
      set bottomEdge to (row + 1) * windowHeight
      
      try
        set newWindow to make new Finder window to POSIX file folderPath
        set bounds of newWindow to {leftEdge, topEdge, rightEdge, bottomEdge}
      on error
        display dialog "Could not open folder: " & folderPath buttons {"OK"} default button "OK"
      end try
    end try
  end repeat
end tell

tell application "Finder"
  tell every window to set toolbar visible to false
end tell

skillet, thanks for this info. I will study it. It will likely help me understand better how KM works. Surely appreciate all your help. You and others make this a truly great forum. I’ve found none better.

1 Like

I am doing what you describe in paragraph 3. Using the Mouse Display works even better. But I like your getter for what it teaches me about coordinates.

I now have another question. If using Mouse Display gives me accurate coordinates relative to a front window in a Move action, why doesn’t a Get do the same? If the answer is too technical, I’ll just accept that it doesn’t work. Thanks again for your splendid support.

I am guessing that you are just moving your mouse and not clicking the window after you press Get. That is how Keyboard Maestro knows the front windows corner. If that isn't the case then I am not sure why it is not working for you but it does for me if you have Front Window Corner selected before you press Get.

image

Spot on. Thank you.

1 Like

Thank you. Issue resolved with this last message.