Move The Text Cursor Back to Where It Was Before Triggering a Macro

I'm just trying to build a macro that makes adding tags to a plain text file in my text editor a bit easier. The macro is supposed to move the text cursor to the first line in the document (that's where the tags are), then insert the tag (that I choose by a user prompt) and then move the text cursor back to the position where it was when I triggered the macro.

Since mouse cursor and text cursor are different, I assumed that using the "Move or Click Mouse" action (with restoring the position afterwards) wouldn't work.

I instead tried to use the "Click at Found Image" action: the macro inserts a unique text string at the original position of the cursor, then it does the tagging business, then the text cursor is supposed to return to the original position through clicking next to an image of the unique text string. I can't get this to work, because the macro never finds the image. Changing "Unique" to "Best" in the action or playing around with fuzziness don't make it work. I've had this issue with other macros using the "Click at Found Image" action as well – trying to avoid this action s much as possible...

Does anyone know how to solve this issue?

This is how I do it:

  1. Store current mouse pointer position
  2. Macro does its stuff
  3. Restore mouse pointer position stored in step 1.
  4. Click mouse button without moving/dragging etc. (which puts the text insertion point back to where it was)

You can see how to save the mouse pointer position here Cannot get cursor to return to original location after move

No need for special text strings and/or images.

Hope that helps.

Hi tiffle!

Thanks for the interesting suggestion (I learnt new stuff), but unfortunately it doesn't solve my problem. What I am interested in is the position of the text cursor (indicated by the blinking vertical line on the screen when you type something), not the mouse cursor (usually indicated by a pointer). Your suggestion only seems to work for the mouse cursor.

For my suggestion to work the mouse pointer and the text insertion point both have to be in the same place at the start. It sounds like that is not the case for you so it's back to the drawing board!

Yes, exactly – they are almost never in the same place when I trigger the macro. When you work in a text editor, usually you click somewhere to start writing, and from then onwards the text cursor moves, whereas the mouse cursor stays where you started to write.

You can't really do what you want here, unless the text editor has a scripting interface.

However, if you're using an editor that supports bookmarks (I think BBEdit calls them "Jump Points?"), you could perhaps set a bookmark first, then go to the begging of the document and do your stuff, then return to the bookmark, then delete the bookmark.

If that makes any sense.

Hm, I almost feared that... And do you have any idea why the approach with clicking on the image doesn’t work?

Images are tricky things. Why they work sometimes and not others is hard to tell. But hey, why don't you use your editor's "Find" to return you to the spot? If you're already inserting some unique string, it shouldn't be too hard.

We might be able to offer more help if you can provide the name of the app you are using. If it is a non-standard app, please provide a link to its web site.

@JMichaelTX: It's The Archive. The developer intends to add scriptability in the future, so might have to wait a bit. This problem is not really a big deal anyway – so don't put too much effort into it! My question was more out of curiosity than necessity.

@DanThomas: I think I can't manage to get the cursor to jump out of the search field to the search result in the document (without having to rely on an image again) – but was worth a try...

OK, then I will withdraw here. It would be appreciated if you would advise us in you OP of when you are just curious.

Thanks.

Really? That sounds like a strange way to implement a Search function. I would think there's got to be a keystroke that moves you back to the document itself, or closes the search window, or something like that. But whatever. Good luck. :smile:

@DanThomas: Not as far as I can see, but will ask the developer. Thanks for your ideas!

@JMichaelTX: I sense a bit of reproachfulness in your reply. As I wrote earlier, it wasn't just curiosity – obviously it would have some benefit to make it work (otherwise I wouldn't have tried it and not put the effort of posting here into it). Little things that make the workflow a bit smoother but aren't absolutely necessary are nice – that's what's KM is mostly used for, isn't it? It was only when you offered to look into my text editor more closely when I thought that that's probably not worth it for now and you probably have bigger fish to fry.

3 Likes

The method you’ve shown - with key strokes - has got to be slower than an API-based approach. Let’s hope your developer get’s a move on.

I am chiming in here wondering if a solution has since been implemented. This would be a HUGE timesaver for me. Imagine this workflow: I am watching a coding tutorial on one screen, while attempting to follow along on another screen. To enhance this workflow, I currently use keyboard maestro to handle certain screen switching events (pause/fast forward/rewind on the video screen), and then I return my cursor back to where it was on the original screen. HOWEVER, the text cursor does not return to where it was. This would be incredibly useful for me so that I can follow along with coding videos/any videos more efficiently. I am using VSCode as my coding editor, and I'm sure it has some scripting capabilities.

I do a lot of typing while referencing video for my work as well, and the best solution I've found for controlling video playback without losing typing position isn't a Keyboard Maestro macro, but an app called Movist Pro that makes it easy to use your keyboard's media keys to pause, rewind, and fast forward video even if Movist Pro is in the background. It's not free, but it is inexpensive ($8 USD) and available on Setapp if you're subscribed to that.

VSC is not externally scriptable on the Mac.

-Chris

Does it have a command line? And a key combo to bring up the command line?

And any way to bring up a menu system and navigate?

(I'll confess that, while I had VS Code installed on my last Mac, I use BBEdit and - to a small extent - Sublime Text.)

VS Code is an Electron app, correct @ccstone ?, so AppleScript won't work

1 Like

Right. But see my comment just above yours.

Clutching at straws, there might still be other ways.