Move Mouse to FrontMost Window When Window Changes

Continuing the discussion from KM Engine Crash Report:

This macro is one of the most useful/helpful that I have built.
Conceptually, it is very simple: When a new window receives focus (like from the App Switcher), OR the window is moved, move the mouse to a preset location relative to that window.

I have two very large (27-in) monitors. Although I have set my mouse movement/acceleration to high speed, it is still a chore to move it quickly and accurately from one monitor to the other. So as soon as I open (or move) a window somewhere else, the mouse it immediately moved to a consistent location for that window.

This is even helpful moving between apps/windows on one large monitor.

I have two main options for its behavior:
Move the mouse to:

  1. A preset location relative to the window for each app
  2. The last position of the mouse for each app

I don't think I'm quite ready to publish this macro yet. There are some edge cases that can give it fits, and I'm still making some adjustments and giving it a thorough testing. Here are my Release Notes (which may not be 100% up-to-date). Maybe they will help your understanding.

Feel free to ask questions.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Release Notes

Author.@JMichaelTX

PURPOSE:

  • Move the Mouse to Follow Activation of Different Window

HOW TO USE:

  1. ENABLE MACRO
  2. Macro Group -- Put in a Global Macro Group, available to ALL Apps
  3. Activate a Different Window (other than by Mouse Click)
  • For Each App, you will be prompted to Set Mouse Position for Preset Mode.
    • You can set Mouse Position Relative to any corner of the Window
    • You can choose "None" -- Mouse will NOT be moved when in Preset Mode
    • You can choose "Off" -- Mouse will NOT be moved in either Preset or Last Mode
  1. Toggle MMF_Method ([PRESET] / [LAST]) -- Trigger maco by hot key ^⌥END

  2. Toggle Macro On/Off -- Trigger Macro by Name

    • Similar to actual Enable/Disable, except the first few Macro Actions still run
    • Then Macro is Canceled
  3. Set PRESET Mouse Position for an App -- Trigger macro by ^⌥HOME

  • This actually triggers a Sub-Macro: "@MOU Update List of PRESET Mouse Positions Relative to Window"

WHAT IT DOES:

  1. NORMAL CASE
    IF a Different Window is Activated by any Method OTHER than Mouse Click
  • IF DND_MMF__Method =
    • "[PRESET]"
      • Move Mouse to Preset Location for that App, unless App Setting is "None" or "Off"
    • "[LAST]"
      • Move Mouse to Last Location for that App, unless App Setting is "Off"
  1. EXCEPTIONS
  • IF Mouse MoveTo Position is outside of Window, set to Center of Window
  • Window Activated by Mouse Click -- Do NOT Move Mouse
  • Macro is Off (DND_MMF__On = FALSE) -- Do NOT Move Mouse
  • Macro is Canceled for Special Cases
    (see Comment "CANCEL FOR SPECIAL CASES")
    • Non-standard Window
    • Window changes too fast
    • Certain Windows for Google Chrome
    • Window is In-Process of being Manually Moved/Resized
1 Like

Thanks Jim. This is what I thought it might be, and then some. Looking forward to testing it out whenever it’s ready. I can see this being useful with pre-positioning the mouse in the most common areas of the screen for different apps, which could cut down on the amount of wrist movement required.

Just when I think I’ve ironed out all of the edge cases, another pops up.
Today I just fixed (hopefully) the case when dragging a window to resize.

Of course the macro triggers on every minute change, but it should not move the mouse until the dragging stops. I think I have fixed this, and I leave the mouse pointer where the user stopped dragging, in case he/she wants to drag some more.

If I don’t have any more issues, I’ll publish tomorrow. :wink:

1 Like

This sounds very useful! Is the macro stable now and can be released?

@cfriend and @cands:

My sincere apologies for the very long delay in publishing this macro.

It has been over 2 years, and I have been using it every day, and for much of that time without any further issues. However, if you have an app that uses highly non-standard windows, then this macro could fail.

Knowing that, if either/both of you would like to be my final Beta testers, I'll be glad to share with you. In fact, I will go ahead and post below. If you choose to download/install it on your Mac, you will do so knowing it is STLL IN BETA. I don't think there could be any hard to your KM or Mac, but one never knows for sure.

I hope you find them useful, and I look forward to your feedback.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BETA 1 Release of Move Mouse Macros

This is actually a macro set of two macros:

  1. @MOU Move Mouse to FrontMost Window When Window Changes
  2. @MOU Update List of PRESET Mouse Positions Relative to Window

Key Notes to Keep in Mind

  1. Both of these macros were uploaded with Macro Triggers in place, although the Macros were disabled.
  2. Since Macro #1 is triggered by any change of windows, be prepared for that.
  3. A red circle will flash briefly showing the new location of the Mouse
  4. In the beginning, the first Macro will be a bit of a PITA, since it needs data for every unique Window Name of every app. When you first bring a new window to front, it will prompt you for the data it needs.
  5. So, before you enable these macros, please set aside a few minutes (at least 10-15 min) to go through all of your App and app Windows to complete the setup phase of the Macro.
  6. OR, If you prefer, you can keep Macro #1 disabled, and take your time visiting all of apps and windows.
    • When each new Window is frontmost, trigger the companion Macro (default trigger: ⌃⌥HOME) to collect/set the data that is needed for Macro #1.
    • Then, when you are ready, you can enable Macro #1 to start having the Mouse move to each window as it is activated.
  7. Finally, as I wrote these macro over two years ago, they are not my best work.
    • So you may find some approaches less than optimal
    • I have learned much about KM, and KM has greatly evolved, since then, so were I to write these macros today they would probably look a lot different.
    • But they seem to run well-enough now, at a reasonable speed, so I'll leave them alone for now except to find any issues that are found.

As always, please feel free to post any questions, issues, comments, and/or suggestions concerning this macro.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Setup Global Variables

  • You need to create a global KM Variable "DND__PauseStd" and set it to "0.1".
    You can do this in the KM Editor Preferences > Variables panel.

MACRO #1:   @MOU Move Mouse to FrontMost Window When Window Changes

~~~ VER: 2.2    2019-06-01 ~~~

DOWNLOAD:

@MOU Move Mouse to FrontMost Window When Window Changes.kmmacros (138 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Move the Mouse to Follow Activation of Different Window

This Macro is one of two Macros in a Companion Set:

  1. @MOU Move Mouse to FrontMost Window ... (this macro)
  2. @MOU Update List of PRESET Mouse Positions Relative to Window

HOW TO USE:

  1. ENABLE MACRO

  2. Macro Group -- Put in a Global Macro Group, available to ALL Apps

  3. Activate a Different Window (other than by Mouse Click)

  • For Each App, you will be prompted to Set Mouse Position for Preset Mode.
    • You can set Mouse Position Relative to any corner of the Window
    • You can choose "None" -- Mouse will NOT be moved when in Preset Mode
    • You can choose "Off" -- Mouse will NOT be moved in either Preset or Last Mode
  1. Toggle MMF_Method ([PRESET] / [LAST]) -- Trigger maco by hot key ^⌥END

  2. Toggle Macro On/Off -- Trigger Macro by Name

  • Similar to actual Enable/Disable, except the first few Macro Actions still run
  • Then Macro is Canceled
  1. Set PRESET Mouse Position for an App -- Trigger macro by ^⌥HOME
  • This actually triggers a Companion Macro: "@MOU Update List of PRESET Mouse Positions Relative to Window"

WHAT IT DOES:

  1. NORMAL CASE
    IF a Different Window is Activated by any Method OTHER than Mouse Click
  • IF DND_MMF__Method =
    • "[PRESET]"
      • Move Mouse to Preset Location for that App, unless App Setting is "None" or "Off"
    • "[LAST]"
      • Move Mouse to Last Location for that App, unless App Setting is "Off"
  1. EXCEPTIONS
  • IF Mouse MoveTo Position is outside of Window, set to Center of Window
  • Window Activated by Mouse Click -- Do NOT Move Mouse
  • Macro is Off (DND_MMF__On = FALSE) -- Do NOT Move Mouse
  • Macro is Canceled for Special Cases
    (see Comment "CANCEL FOR SPECIAL CASES")
    • Non-standard Window
    • Window changes too fast
    • Certain Windows for Google Chrome
    • Window is In-Process of being Manually Moved/Resized

TAGS: @Mouse @Window

USER SETTINGS:

• Any Action in magenta color is designed to be changed by end-user
• This macro uses Google Search and Google Chrome, but can be easily changed

ACTION COLOR CODES

• To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows:
• GREEN -- Key Comments designed to highlight main sections of macro
• MAGENTA -- Actions designed to be customized by user
• YELLOW -- Primary Actions (usually the main purpose of the macro)
• ORANGE -- Actions that permanently destroy Varibles or Clipboards

REQUIRES:
(1) Keyboard Maestro Ver 7.3+
(2) Yosemite (10.10.5)+


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO #2:   @MOU Update List of PRESET Mouse Positions Relative to Window

~~~ VER: 2.1    2018-12-13 ~~~

DOWNLOAD:

@MOU Update List of PRESET Mouse Positions Relative to Window.kmmacros (29 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • This is a supporting, companion, Macro to "@MOU Move Mouse to FrontMost Window When Window Changes"
  • This Macro Provides Two Functions, based on the Hot Key used:
    • ⌃⌥Home -- sets the PRESET location for Mouse Position for a given App/Window
    • ⌃⌥⌘End -- Toggles the Enable/Disable of the Main Macro, "@MOU Move Mouse to FrontMost Window When Window Changes"

HOW TO USE:

Set Preset Location for Mouse Position

  1. Activate (make FrontMost) the Target App/Window
  2. Trigger this Macro with the ⌃⌥Home Hot Key
  3. Follow directions on Prompt

Enable/Disable Main @MOU Macro

  1. From any app or window, Trigger this Macro using the ⌃⌥⌘End Hot Key

TAGS: @Mouse @Window @Location

USER SETTINGS:
• Any Action in magenta color is designed to be changed by end-user
• This macro uses Google Search and Google Chrome, but can be easily changed

ACTION COLOR CODES
• To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows:
• GREEN -- Key Comments designed to highlight main sections of macro
• MAGENTA -- Actions designed to be customized by user
• YELLOW -- Primary Actions (usually the main purpose of the macro)
• ORANGE -- Actions that permanently destroy Varibles or Clipboards

REQUIRES:
(1) Keyboard Maestro Ver 7.2.1+
(2) Yosemite (10.10.5)+


image

2 Likes

Thank you very much for providing this macro! I have used it for about three months now, and I find it to be most helpful. In my experience it is working quite well, with the exception that it does not always move the mouse as expected, to the preset option I have set (i.e. for the same app in most cases but not all it works as expected). Maybe it is due to that the macro is canceled for special cases as you have noted under “Exceptions”. I have not been able to figure out the reason for why the macro is not fully reliable on my machine (macOS 10.14.6, Keyboard Maestro 9.0.2). Any thoughts what could be causing this behavior?

If it is mostly with the same app, then that would suggest that the app is using some non-standard windows. Which app is it?

When I was developing this macro, I found Google Chrome to be a big problem, as it uses many non-standard windows. The "Exceptions" Group Action is largely based on Chrome.

Without some very specific details, about all I can suggest is to invoke the KM Debugger when that App triggers the Macro, and step through each Action to see if you can identify the issue.

I don't think this Macro will ever be perfect. There will always be a few Apps that cause an issue.
Occasionally the Mouse pointer will not move when I switch apps, and I find that if I switch back to the original app (or another app) and then back to the app of interest, then the Macro will work properly.

I hope this helps.

The app that is mostly affected is Airmail 3, but it occasionally happens for other apps as well. I've also noted the behavior you mention, i.e. that occasionally the mouse pointer will not move when I switch apps. However, even with these occasional glitches the macro is very helpful, and now I would not want to be without it :smiley: So thank you again. When I have time I'll try to do a more thorough testing with the KM debugger as you suggest. I'll post any interesting observations I may make here.

Sorry, I don't have/use this app, so I can't test.

I'm glad you find it helpful (warts and all :wink: ).

That would be great.

1 Like

IT seems to be working as expected. However, every time the macro launches, I get this notification:

image image

Action Failed
Pause failed to evaluate DND__PauseStd in macro
"@MOU Move Mouse to FrontMost Window When
Window Changes" (while executing Pause for D...

Sorry about that. I should have stated this in the ReleaseNotes.

You need to create a global KM Variable "DND__PauseStd" and set it to "0.1".
You can do this in the KM Editor Preferences > Variables panel.

That did the trick!! Thanks!!

I'm running Outlook through Parallels, and when i mouse over certain elements in Outlook, it causes those hint bubbles to display, which then causes this macro to move the mouse to the bubble. I know there is a way to ignore these, but not sure how to accomplish with this macro. Hints?

Hi Michael,

Thank you for the script! This is exactly what I was looking for.

I have installed two scripts, but it does not work as expected.

My mouse cursor blinks with green color when I change focus, but it does not move. It only works when I focus on Keyboard Maestro window.

Could you think of any possible causes?

Hey @cfriend ,

First of all, I would like to thank you all for providing this macro. I am looking for something similar but not as complicated and would be grateful if you could help me out with it.

My current setup:

Right now, whenever I switch the window, I move move pointer to (1, 1) position from the center of the front window.

What I want:

I would like to keep the mouse pointer at the same location relative to the frontmost window.

P.S. - I just started using this app and still learning my way around it.

Thanks,
Naman

Hello @nbansal and welcome to the forum. This is actually JMichaelTX's macro, and unfortunately he died unexpectedly last year and can no longer help us here. I will blindly ping @ccstone --another KM big brain--assuming that if he cannot help you, he can ping someone who can :sweat_smile:

1 Like

Hey Naman,

You'd have to get the front window frame and the mouse pointer position and then do some math.

The trouble with this is that when Keyboard Maestro notices the window has changed you no longer have access to the old front window.

So – this sort of thing can be troublesome...

-Chris

I am using MS Teams latest build on Sonoma latest ga build. The window location doesn't seem to work, it just goes to center of window every time. Any known issues with Teams?