1

I can select the first cell like this:

nb = CreateDocument[{a, b, c}, WindowTitle -> "Other"];
SelectionMove[nb, Next, Cell]

But how to select the first and the second cell?

rhermans
  • 36,518
  • 4
  • 57
  • 149
yode
  • 26,686
  • 4
  • 62
  • 167

2 Answers2

2

Ah, now I recall why I emphasized "non adjacent" in Programmatically selecting non adjacent Cells

It is because you can select adjacent cells by mimicking what FE does when you use keyboard shortcuts:

Item[KeyEvent["Down", Modifiers -> {Shift}], "SelectNextLine"], 

-KeyEventTranslations.tr

So in our case:

  • Select the first cell for a set you want to select

  • Repeat token execution

SelectionMove[First @ Cells[], All, Cell]
FrontEndExecute @ FrontEndToken["SelectNextLine"]
Kuba
  • 136,707
  • 13
  • 279
  • 740
0

You could create the Notebook with tags and later evaluate those. Notebooklocate also works with stored notebooks. Consider that the Cells maybe only are evaluateable if the type is set to "Input".

nb1 = CreateDocument[{Cell["a=2", "Input", CellTags -> "myTag"], 
   Cell["b"], Cell["c=3d", "Input", CellTags -> "myTag"]}, 
  WindowTitle -> "myCreation"]    

SetSelectedNotebook[nb1]
NotebookLocate["myTag"]
SelectionEvaluate[nb1]
crx
  • 41
  • 2
  • No one said anything about evaluation :) – Kuba Sep 22 '16 at 10:32
  • Well, if one drops the last Part (SelectionEvaluate) it will only select the cells. I didn't thougth someone would only want to select severel cells without processing them. – crx Sep 22 '16 at 11:03
  • I see, but one may want to delete them, or change a background. Maybe you are right but I'm afraid the question is more general, so e.g. OP may want to select cells 1,2, do something, and the select cells 1,3. – Kuba Sep 22 '16 at 11:06