In case this is helpful to anyone stumbling upon this thread this as I did, here is simpler code to select all cells from the end of a notebook to the current evaluation cell.
endAtID = First @ EvaluationCell[];
SelectionMove[EvaluationNotebook[], After, Notebook];
SelectionMove[EvaluationNotebook[], Previous, Cell];
While[
SelectedCells[][[1, 1]] =!= endAtID,
FrontEndTokenExecute[EvaluationNotebook[], "SelectPreviousLine"];
]
This can be modified to start from the cell containing the current insertion point, here is a button which will do this.
Button["press to select all cells from insertion point to end of notebook",
SelectionMove[InputNotebook[], All, Cell];
If[
SelectedCells[InputNotebook[]] === {},
SelectionMove[InputNotebook[], Next, Cell]
];
endAtID = First @ SelectedCells[InputNotebook[]][[1]];
SelectionMove[InputNotebook[], After, Notebook];
SelectionMove[InputNotebook[], Previous, Cell];
While[
SelectedCells[InputNotebook[]][[1, 1]] =!= endAtID,
FrontEndTokenExecute[InputNotebook[], "SelectPreviousLine"];
]
]
The code inside the button above should work fine when triggered from a keyboard shortcut or palette button or wherever you want. To make it delete the selected cells, simply add
NotebookDelete[InputNotebook[]]
to the end of the code inside the button.
AMENDMENTS:
To make the selection go from the insertion point to the end of the notebook, you can modify the code as such
Button["press to select all cells from insertion point to end of notebook",
SelectionMove[InputNotebook[], After, Notebook];
SelectionMove[InputNotebook[], Previous, Cell];
endAtID = First@SelectedCells[InputNotebook[]][[1]];
SelectionMove[EvaluationCell[], All, Cell];
While[
SelectedCells[InputNotebook[]][[-1, 1]] =!= endAtID,
FrontEndTokenExecute[InputNotebook[], "SelectNextLine"];
]
]
I like Albert Riley's suggestion in the comments to use FixedPoint so much I thought we should add it here
SelectionMove[InputNotebook[], All, Cell];
FixedPoint[
(
FrontEndTokenExecute[InputNotebook[], "SelectNextLine"];
SelectedCells[InputNotebook[]]
) &,
{}
]
SelectionMoveconfirm: "SelectionMove can only select multiple cells as part of a CellGroup unit specification." – Mr.Wizard Oct 30 '12 at 21:40Read the FAQs! 3) When you see good Q&A, vote them up byclicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem,by clicking the checkmark sign` – chris Oct 31 '12 at 10:15