Techniques Modifying Cells M8.0

2

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.

William

Posted 2013-06-26T21:34:04.927

Reputation: 7 329

Answers

3

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[]]

William

Posted 2013-06-26T21:34:04.927

Reputation: 7 329

4But 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 – 2013-06-26T21:56:29.640

I agree with @SjoerdC.deVries. If you want the list of cells you could also do First@NotebookGet – Rojo – 2013-06-27T21:23:47.750

@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. – William – 2013-06-27T22:57:24.333

1An option could be =-NotebookTools`NotebookScan[InputNotebook[], nb \[Function] CurrentValue[NotebookSelection[nb], Background] = Green] – Rojo – 2013-06-27T23:07:08.523

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 – 2013-06-27T23:09:07.500

I haven't seen the \[Function] operator before, therefore I kinda prefer NotebookTools\NotebookScan[InputNotebook[], Function[CurrentValue[NotebookSelection[#], Background] =Green;]]` – William – 2013-06-28T20:40:10.297