Is there a way to access (or even set) the cursor position in a Notebook?

(By Cursor I refer to the keyboard input postion, or "Insertion Point". For the Mouse we have CurrentValue["MousePosition"] or just MousePosition[])

Note that calling external tools is only the seconds best option for me. See Programmatically move the cursor to a desired location on the screen (which is also about the mouse)

  • $\begingroup$ One thing that makes this question difficult to answer is lack of specification of the expected position datum -- should it be X number of Cells into the Notebook, or a certain number of characters into the expression returned by NotebookGet, or...? $\endgroup$ – Mr.Wizard Oct 18 '12 at 9:37
  • $\begingroup$ You are right. I'm sure MMA has such a specification internally, but maybe it cannot be accessed!? So I was refering to that -unknown- internal spec. $\endgroup$ – NoEscape Oct 18 '12 at 10:02
  • $\begingroup$ Fair enough. Let me ask the question in a different way: what would you do with this internal spec if you had it? There is apparently no way to feed it to SelectionMove. What kind of interaction are you envisioning? $\endgroup$ – Mr.Wizard Oct 18 '12 at 10:04
  • $\begingroup$ The simplest application is just saving the insertion point and setting it back later! $\endgroup$ – NoEscape Oct 18 '12 at 10:06
  • $\begingroup$ That's just the problem though; I don't know if any way to say "move cursor to point X" but only "move cursor X units of type Y in direction Z." Perhaps you could use some marker and then do a text search for that. I don't think there are many tools provided for this kind of thing; it's typically much easier to work with the Notebook expression than with the GUI Notebook itself. $\endgroup$ – Mr.Wizard Oct 18 '12 at 10:17

Setting the position may be easier than retrieving it as there exists SelectionMove.

No record of the insertion point appears in the output of NotebookGet. At the moment the best I can think of is to use NotebookWrite to insert a unique token and then search the expression returned by NotebookGet for this token, but this changes the target Notebook.

Perhaps if you described your application I would be able to provide other suggestions.

  • $\begingroup$ unique token... VERY good point! $\endgroup$ – NoEscape Oct 18 '12 at 9:51
  • $\begingroup$ Aha, from experimenting with SelectionMove I come to the conclusion that a "Selection" can also be empty and then just represents the Insertion point. This is not at all evident from the naming of that function! (this happens e.g. on SelectionMove[nb, Next, Character] $\endgroup$ – NoEscape Oct 18 '12 at 10:11
  • $\begingroup$ @NoEscape, sorry, I guess should have said that in my answer. $\endgroup$ – Mr.Wizard Oct 18 '12 at 10:12
  • 6
    $\begingroup$ With Developer`CellInformation you get the cursor position within the selected cell, and the selected cell serial number whatever that means $\endgroup$ – Rojo Oct 18 '12 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.