Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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[]]
share|improve this answer
4  
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. –  Sjoerd C. de Vries Jun 26 '13 at 21:56
    
I agree with @SjoerdC.deVries. If you want the list of cells you could also do First@NotebookGet –  Rojo Jun 27 '13 at 21:23
    
@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. –  Liam William Jun 27 '13 at 22:57
    
An option could be =-NotebookTools`NotebookScan[InputNotebook[], nb \[Function] CurrentValue[NotebookSelection[nb], Background] = Green] –  Rojo Jun 27 '13 at 23:07
    
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 –  Rojo Jun 27 '13 at 23:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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