Get Mouse Position, Run Other Actions, Then Return Mouse Pointer to Same Position

That's the problem with making KM simpler - it can lead to complications and I must assuage my feelings of guilt for posting something too simple!

Here's something to consider: suppose you have two macros that both use those saving/restoring actions. What happens when both macros run simultaneously? They might interfere with each other because they both use the same global variable for the mouse position. So while one macro runs and saves the mouse position, the other macro comes along and also saves the mouse position; then when the first macro restores the mouse position it actually uses the position set by the second macro. In other words: WRONG!!

To prevent this from happening you have to understand the difference between global and local variables: when you use a global variable called "X", say, in a macro, that same "X" can be seen, used and updated in any other macro. This makes global variables useful for passing information between macros. Global variables exist separately from macros and even retain their values when your Mac reboots!

Local variables on the other hand are "hidden" from every macro except the one they are first used in. In simple terms a local variable called "Local_Y" in one macro is completely independent from the same-named local variable in any other macro. So changing "Local_Y" in a macro does not affect the value of any other "Local_Y" in any other macros you have. And when your macro finishes, or your Mac reboots, "Local_Y" disappears...as do every other local variable.

How does that affect the mouse saving/restoring actions I previously posted? Well, here is a matching pair that works safely without interfering with any others in any of your other macros.

image

Local Save-Restore Mouse Position.kmactions (941 Bytes)

The only difference is that they now use local variables meaning you can sprinkle these actions liberally around your macros without them interfering with each other.

Sometimes you might actually want to save the mouse position and use it in another macro - in which case use the global variable version of these actions I originally posted.

This whole post illustrates a peculiarity that perhaps escapes most users of KM: even when KM provides simple actions you most likely still need to have a detailed understanding to make full use of them. And that's where @Nige_S's brilliantly straightforward summary in post 18 comes into its own for those just starting out with KM.

3 Likes