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

Nige_S,

Thank you so much for the words of encouragement.

I'll study the manual, and look for at sample macros to learn more.

I appreciate the explanations of actions, tokens and functions, variables and such.

My needs are pretty simple, but I like to learn so this will be interesting for me.

Thanks again

As a wise man once said, "Gee, I wish there was a delay action :nerd_face:" (my next thing to learn)

You are totally right, a wise person would never say something like that :wink: What a wise person would actually say is, "Give a man as many predefined actions as possible, and he will use them to build the most complex things without having to learn other fundamentals, because these are the fundamentals."

That is exactly how it is. But simple truths cannot enter complex thinking brains.

And again you got that exactly right. You could explain to the pros that a simple action like "save current mouse position" doesn't take anything away from them, because KM still offers the possibility to solve it in a complex way, but they wouldn't like it even then.

This is really true. The friendly pros here unfortunately have no idea what beginners need, but they are always ready to help. And that's much more than you can say about other forums. So @Donald just ask and you will receive :slightly_smiling_face:

Well - you already created them, but here they are again for you...

image
Save Current Mouse Position.kmactions (592 Bytes)

image
Restore Saved Mouse Position.kmactions (583 Bytes)

Although this is a bit of a cheat, just import these two actions into KM and then add them to Favorites (KM Menu > Actions > Add to Favorites) so they'll be readily available whenever you want.

KM can be made simpler - you just have to put in the effort to become an unfortunate pro! Enjoy!

tiffle,

That is a great tip. I put those actions in my actions favorites. Thank you for uploading them so they can be downloaded by myself and others.

Thank you!

1 Like

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

Beautiful explanation. Now I got a clearer understanding.

What do you mean by 'disappears'?

Suppose I uninstall KM from my Mac. Install again, would the variable "Local_Y" retain its respective value in each macro?

Literally that. Local_Y only exists while the macro is executing.

In fact it goes further -- local variables are particular to each instance of a macro. So if you fire off "Macro A", then trigger it again before the first instance has completed, each instance of "Macro A" will have its own Local_Y with its own, segregated, value.

Have a look at the Variables section of the manual for more about the different types and their scope.

2 Likes

tiffle,

That is an excellent explanation of Global VS Local variables. I had no idea that variables could be passed from one macro to another. I was happily surprised to learn global macro values are retained, even when the computer is rebooted, and that local variables stay with the original macro, even if other macros are using local variables with the same name. I am amazed by this program, and even more amazed by your clear explanations. Thank you so very much.

Thank you for uploading the local mouse position actions. I'll add them to my favorites, and I will have a clear understanding of which action ( Global vs Local ) to use appropriately.

1 Like

Nige_S,

Thanks you for clarifying how local variables work.

It is amazing to me more than one macro can be running at the same time, and have variables with the same name in different macros not interfere with each other, as long as they are local variables. I can see how global variables with the same name and running at the same time would cause a real mess.

Thanks for your helpful information.

1 Like

Thank you, too, @Donald and @fluid_eye and thanks @Nige_S for appearing when I “disappeared”! Much appreciated.

1 Like