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 Cell
s seems quite limited.
1 Answer
$\begingroup$
$\endgroup$
6
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[]]
-
4$\begingroup$ But
Cells
in v9 returns completely different objects as in v8 with yourCells
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$– RojoJun 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$– WilliamJun 27, 2013 at 22:57 -
1$\begingroup$ An option could be =-
NotebookTools`NotebookScan[InputNotebook[], nb \[Function] CurrentValue[NotebookSelection[nb], Background] = Green]
$\endgroup$– RojoJun 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 yourNotebookRead
). In this case, setting the background of the current selection to green $\endgroup$– RojoJun 27, 2013 at 23:09