2
$\begingroup$

I want to delete current and previous cells.So I make a custom function:

dd := (SelectionMove[EvaluationCell[], Previous, CellGroup]; 
  NotebookDelete[]; NotebookDelete[EvaluationCell[]])

But I'm confused why examples below do not work:

  • First

    dd := (NotebookDelete[
       SelectionMove[EvaluationCell[], Previous, CellGroup]]; 
      NotebookDelete[EvaluationCell[]])
    
  • Second

    dd := (obj1 = SelectionMove[EvaluationCell[], Previous, CellGroup];
      obj2 = EvaluationCell[]; NotebookDelete[{obj1, obj2}];)
    

I can not check any error in my code. And I think the usage is supported by Mathematica:

$\endgroup$
  • $\begingroup$ I don't know what exactly is your point but notice that SelectionMove does not return CellObject (or anything in general). $\endgroup$ – Kuba Sep 22 '16 at 8:45
  • $\begingroup$ @Kuba You are right maybe.So how to return that CellObject in current selection? $\endgroup$ – yode Sep 22 '16 at 9:05
  • $\begingroup$ SelectedCells[] $\endgroup$ – Kuba Sep 22 '16 at 9:06
  • $\begingroup$ @Kuba Thanks very much.You help me again. $\endgroup$ – yode Sep 22 '16 at 9:07
1
$\begingroup$

Notice that SelectionMove does not return CellObject (or anything in general) that would be suitable for other FrontEnd related functions.

This is a problem with your examples, obj1 is really Null.

But you can get to know which cell was selected after SelectionMove, use SelectedCells[].

Here is a minimal example how would it work, create two dummy cells:

1

2

and evaluate

SelectionMove[First @ Cells[], All, Cell];
obj1 = SelectedCells[];
obj2 = PreviousCell[]; 
NotebookDelete[Flatten @ {obj1, obj2}];
$\endgroup$

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.