5

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)

NoEscape
  • 842
  • 4
  • 14
  • 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...? – Mr.Wizard Oct 18 '12 at 09:37
  • 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. – NoEscape Oct 18 '12 at 10:02
  • 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? – Mr.Wizard Oct 18 '12 at 10:04
  • The simplest application is just saving the insertion point and setting it back later! – NoEscape Oct 18 '12 at 10:06
  • 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. – Mr.Wizard Oct 18 '12 at 10:17
  • The cursor position is tracked in the Devceloper`CellInformation. Have a look at my implementation of the code highlighter in this answer to see how it can be practically used. – Leonid Shifrin Oct 19 '12 at 04:49
  • @LeonidShifrin
    1. Cannot find your code
    2. Can CellInformation be used to SET something?
    – NoEscape Oct 19 '12 at 07:20
  • Have a look at http://library.wolfram.com/infocenter/Conferences/7246/ to learn about tagging and moving to tagged cells; as well as CellIDs (not the same as CellSerialNumber from CellInformation). – Vladimir Mar 25 '13 at 10:07

1 Answers1

5

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • unique token... VERY good point! – NoEscape Oct 18 '12 at 09:51
  • 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] – NoEscape Oct 18 '12 at 10:11
  • @NoEscape, sorry, I guess should have said that in my answer. – Mr.Wizard Oct 18 '12 at 10:12
  • 6
    With Developer\CellInformation` you get the cursor position within the selected cell, and the selected cell serial number whatever that means – Rojo Oct 18 '12 at 23:35