2
$\begingroup$

What techniques are there for modifying/organizing/selecting/deleting cells? I understand Mathematica 9.0 has a Cells function but what techniques exist in Mathematica 8.0? The documentation for effectively using and modifying Cells seems quite limited.

$\endgroup$

1 Answer 1

3
$\begingroup$

Techniques for modifying cells in Mathematica 8.0

Deletes all output Cells

FrontEndExecute[{
  FrontEnd`NotebookFind[nb, "Output", All, CellStyle, AutoScroll -> False], 
  FrontEnd`FrontEndToken["Clear"]
}];

Sets all Cells background to Green.

ModifyCells@Function[
  CurrentValue[#, Background] = Green;
]

retrieve a list of Cells

NotebookGet[][[1]]

Find cell based on CellTags

CellPrint[TextCell["abcd", "Text", CellTags -> "tag"]]
> abcd 
NotebookLocate["tag"]; NotebookRead[EvaluationNotebook[]]
> Cell["abcd", "Text", GeneratedCell -> True, CellAutoOverwrite -> True,CellTags -> "tag"]

Set TaggingRules for Notebook

SetOptions[EvaluationNotebook[],TaggingRules -> "InputFieldState" -> "initial string"]

Get TaggingRules for Notebook

Options[EvaluationNotebook[]], TaggingRules]
CurrentValue[EvaluationNotebook[], TaggingRules]

another way to retrieve list of Cells

NotebookTools`NotebookScan[InputNotebook[], Function[
   InputForm[NotebookRead[InputNotebook[]]]
   ]];

And the needed functions to get the examples to work:

ModifyCells[fun_] := (
  i = 0;
  length = Length[NotebookGet[EvaluationNotebook[]][[1]]] + 1;
  NotebookTools`NotebookScan[InputNotebook[], (
     If[i < length,
      fun[NotebookSelection[#], i++];,
      Null
      ]
     ) &
   ];
  i
  )

Will also retrieve a list of Cells

Cells[nb_] := (
  SelectionMove[nb, All, Notebook];
  content = NotebookRead[nb];
  SelectionMove[nb, All, Cell];
  content
  )

Cells[EvaluationNotebook[]]
$\endgroup$
6
  • 4
    $\begingroup$ But Cells in v9 returns completely different objects as in v8 with your Cells function, so I guess usage further on will differ considerably too. $\endgroup$ Jun 26, 2013 at 21:56
  • $\begingroup$ I agree with @SjoerdC.deVries. If you want the list of cells you could also do First@NotebookGet $\endgroup$
    – Rojo
    Jun 27, 2013 at 21:23
  • $\begingroup$ @Rojo I see what you mean how would I make the following code work Map[(SetOptions[#, Background -> Green]) &, First[NotebookGet[]]] Basically loop through all the cells in Mathematica 8.0 and set the background green. $\endgroup$
    – William
    Jun 27, 2013 at 22:57
  • 1
    $\begingroup$ An option could be =-NotebookTools`NotebookScan[InputNotebook[], nb \[Function] CurrentValue[NotebookSelection[nb], Background] = Green] $\endgroup$
    – Rojo
    Jun 27, 2013 at 23:07
  • $\begingroup$ The NotebookScan function is very similar to your function, but it allows you to do something else while the cell is selected (instead of your NotebookRead). In this case, setting the background of the current selection to green $\endgroup$
    – Rojo
    Jun 27, 2013 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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