Navigate notebook contents

Introduction

This code reads the structure of a notebook, finds the headings, and displays them as a tree structure. When you select a leaf node, the notebook is scrolled to that point. Although this is really just a sample program, it is in fact extremely useful when working with large notebooks.

Code

Needs["SuperWidgetPackage`"];

find˘function[x_]:=Module[{str,nb=the˘notebook,cc},
str=Cases[x,Tree˘Node[_,_,1],∞][[1,1]];
SelectionMove[nb,Before,Notebook];
While[True,
cc=NotebookFind[nb,str,Next];
If[cc ===$Failed,Break[]];
SelectionMove[nb,All,Cell];
cc=NotebookRead[nb];
If[cc[[1]]==str,Break[]];
]
];

setup˘tree[nb_]:=Module[{nbs},
the˘notebook=nb;
the˘name="WindowTitle"/.NotebookInformation[nb];

nbs=NotebookGet[nb][[1]]//. {(Cell[x_,___]/;(Head[x]=!=String&&Head[x]=!=CellGroupData)):>Sequence[],Cell[CellGroupData[{},___],___]:>Sequence[],Cell[_,"Print",___]:>Sequence[],Cell[_,"Output",___]:>Sequence[],Cell[_,"Text",___]:>Sequence[]};
the˘tree=nbs//.{Cell[CellGroupData[x_,___],___]:>Tree˘Node[x[[1,1]],0,0,Rest[x]],Cell[x_String,___]->Tree˘Node[x,0,0],Tree˘Node[x_,a_,b_,{}]->Tree˘Node[x,a,b]};
If[Length[the˘tree]==1,
the˘tree=the˘tree[[1]],
the˘tree=Tree˘Node["Notebook",0,0,the˘tree]
];
];

cfn[kk_]:=Module[{k=Position[nb˘names,kk][[1,1]]},
the˘notebook=all˘notebooks[[k]];
setup˘tree[the˘notebook];
UpdateWidgetValue[the˘tree];
]

Display˘Contents[]:=Module[{nb=SelectedNotebook[],i,x,a,b,kn},
all˘notebooks=Notebooks[];
nb˘names=Map[("WindowTitle"/.NotebookInformation[#])&,all˘notebooks];
setup˘tree[nb];
SuperWidgetFrame[fr1,{(Subscript[¶, 10]),{(Subscript[¶, 5]),{SuperWidgetLabel[Null,Image˘File[SuperWidgetPackagePath[]<>"/ExampleFiles/book.gif"]],
(Subscript[¶, 10]),SuperWidgetComboBox[kn,nb˘names,ChangeFunction->cfn],(Subscript[¶, 0])},(Subscript[¶, 5]),SuperWidgetTree[the˘tree,SelectionFunction->find˘function]},(Subscript[¶, 10])},
Title->"Contents of "<>("WindowTitle"/.NotebookInformation[nb])]//SuperGUIRun;
];

Test

This displays the graph. If you set the argument to True, the notebook will be concealed while the graph is on display – thus avoiding any confusion if the Java window is obscured by the notebook.

Display˘Contents[]