Get text cursor position

Is there some cool way to figure out the position of the cursor within the current line? My goal is to remember it so I can get back to it after formatting the current line.

I’m specifically working in Evernote, but would like it for other text apps also. Thx!

Unless the app has very good AppleScript support, no. In general there is no way to determine where the text cursor is.

What I do in a macro where I want to preserve the position in Xcode is basically this sequence:

 [Assumes there is no current selection, just an insertion point]
 Insert Text by Typing "H3R3 " (text version of "here" that will never appear in the editor).
 …
 Set Find Pasteboard to "H3R3 "
 Command G
 Command E
1 Like

Nice trick, I will try it!

[quote=“reesd, post:1, topic:527”]
Is there some cool way to figure out the position of the cursor within the current line?[/quote]

Hey David,

Not unless the app you’re working in specifically allows for scripting the selection. (BBEdit and TextWrangler would be notable examples of this.)

So Peter’s method is the only general way to get it done, although it won’t work unless the app supports ‘Find’ and uses the Find Pasteboard.

-Chris

Depending on what your doing you can Xcode commands "Set Mark" and "Swap With Mark". These are basically set a bookmark and jump back to bookmark. You can use the KeyBindings to assign hotkeys and send them in KM.

This could be very handy - sadly for me it appears to crash Xcode fairly repeatably. Oh well, maybe the next version of Xcode will have this fixed.

I found the crash too in both xCode 9.x and 10.x beta. I sent the crash log and instructions to reproduce to Apple. The crash will happen if the column swapWithMark is trying to set the mark too doesn't exist. This was happening to me when I cut a line that was longer than the line below it and I had the insertion point at the end of the line. When the line below moves up to take the place of the cut line swapWithMark tries to put the insertion point beyond the end of the line and CRASHES. Of course it should put it at the end of the line in this case.

OK, that matches where I was seeing it. I was doing something like this:

  • Select a word in the middle of a line (with a blank line above it)
  • Set Mark
  • Go to the top of the file
  • Add a link
  • Swap Mark <-- Crash.

I believe the mark is not noticing that the added line has pushed the mark down a line, and so it is, going to the position on the previous line, which, being blank, is shorter than the position, hence crashing in the same way.

Yep, if I duplicate my line and select the word in the middle of the second line and perform the same actions above, no crash happens and the word on the first line is selected.

So as well as your crash, it seems the marks do not follow the code when you change code above them. Which is pretty lame!

Lame, that’s Xcode