Problem with accessibility keyboard using iMac and appleScript driven custom keyboard buttons on iPad: how to get and restore mouse position?

Hello, I have an iMac and am using an iPad to display a custom panel within the accessibility keyboard. The custom (by me) accessibility keyboard contains multiple buttons which each run an appleScript.

Everything is working perfectly except for an annoying problem. Every time I tap on a button on the iPad, the AppleScript runs perfectly BUT the cursor relocates to the iPad, and I am stuck manually repositioning it on my iMac main screen which requires a somewhat tortuous and repetitive hand movement.

What I would like to achieve it the ability to

1- get cursor position

2- run the initially intended appleScript

3- restore cursor position

obviously all within one script

For example, I configured a button to run the following script to open reddit:

tell application “Chrome”
open location “
end tell

Is there a ‘simple’ way to modify that script to first ‘get’ or ‘capture’ (I hope that the wording is correct) the cursor position on the main screen → open reddit in Chrome → restore the cursor position on the main screen ?

thanks in advance very much for your time and help

When you make the initial tap to trigger the button on the panel, that would become the initial in-focus location for the pointer, so at the point the script is triggered, it has no access to where the pointer was.

The cursor position is associated with a particular application, and, assuming the particular AppleScript does not interfere, would be in the same location when the app is returned to. Just like the pointer position, the control panel would not be aware of what the previous app was, but if you could trigger a CMD+Tab, capture some unique window identifier, and then CMD+Tab back for the previous AppleScript processing, you might be able to force a switch back to a particular app window, and with the cursor in the same position. Depending upon what the AppleScript is doing, just a CMD+Tab or two at the end may suffice for this without resorting to trying to find precise windows.

1 Like

You have an outstanding reputation and I am most grateful that you responded to my post.

Considering your expertise, I am sure that you are right, and your reasoning makes sense.

The impetus for my question is that fact the 2 solutions I found with keyboard maestro worked as per screenshot below. In both cases I simply copied the appleScript macro trigger into the accessibility keyboard button. The first solution puts the cursor somewhere on the main screen and the second solution puts the cursor back where it was (at least I think so).

In reference to my initial question, could I write the Cmd-Tab into my script, to save me having to Cmd-Tab each time I tap a button ?

thanks again very much

That first KM macro is clicking a specific coordinate at the end. The second is capturing the position and re-setting it at the end.

The first one regardless of where you trigger it should always do the same thing assuming Scrivener isn’t brought to the foreground into that location.

If on the second, you start in app X, tap on app Y to trigger the macro, KM will detect the mouse pointer as being in app Y and so return the mouse pointer there at the end. Not to app X.

In terms of using CMD+TAB, that is cycling/switching back to the app with the previous focus. If you are triggering Keyboard Maestro from your panel, I would add the CMD+TAB pressing into Keyboard Maestro. I find it quicker and easier to maintain and experiment with over making amendments in the embedded scripts. I would say it is also easier to control timings in KM. But overall, I dare say that it is simply a personal preference.

1 Like

very good advice that I will implement.

thank you very much !