# Dynamic docked toolbar that appears only in the Working screen environment

I have created a toolbar that, among other things, has an action menu button that gives me a table of contents list to navigate, see the picture bellow:

Now I'm trying to make this toolbar appear only in Working environment and disappear in printout environment. Since toolbar is a cell that has an evaluating command I wasn't able to put it directly in the stylesheet, so that I'm setting the toolbar with next command:

SetOptions[EvaluationNotebook[], DockedCells -> toolbarCell]


Usually when dealing with static DockedCells I would do this in the deffinitions of Working and Printout style cells like this:

Cell[StyleData["Notebook", "Working"], DockedCells->{toolbarCell}]
Cell[StyleData["Notebook", "Printout"], DockedCells->{}]


But, as said earlier, toolbarCell has code that searches whole notebook for section cells and makes a list from them in order to create ActionMenu buton. One option that I unsuccessfully tried was to put toolbarCell code in MiscExpressions.tr file in location C:\Program Files\Wolfram Research\Mathematica\11.0\SystemFiles\FrontEnd\TextResources and call it with command:

DockedCells -> {FEPrivateFrontEndResource["FEExpressions","MasterToolbar"]}]


This is the way that presentation toolbar is called. But I can't seem to get it to work.

Another thing I tried was using Dynamic but still unsuccessful. Any help is more than welcome. I tried to present the problem conceptually since putting in all the code would be an overdue.

EDIT:
The main part of the code goes like this:

{Cell[
BoxData[
ToBoxes[
(*creating list of action menu item names and list of commands*)
Inner[
RuleDelayed,
(*1 create list of names*)
Composition[
Map[
(*1.3. Extract modified input text into list*)
First@FrontEndExecute@FrontEndExportPacket[#, "InputText"] &],
(*1.2. read objects into an expression and replace CounterBox["typeOfCell"] with counter values*)
ToBoxes@CurrentValue[#, {"CounterValue", style}]) &],
(*1.1. extract list of section cells objects to put in TOC*)
Cells[#,
CellStyle -> {"AbstractSection", "Section", "Subsection",
"Subsubsection", "ReferenceSection",
"EndnoteSection"}]&
]@EvaluationNotebook[],

(*2. create list of commands*)
Map[
Unevaluated@NotebookFind[EvaluationNotebook[], #, All, CellID] &,
CurrentValue[
Cells[CellStyle -> {"AbstractSection", "Section",
"Subsection", "Subsubsection", "ReferenceSection",
"EndnoteSection"}], CellID], 1], List]]]]]}


Now when I put the above written code as a docked cell in stylesheet I cannot close the cell when pressing ctr+shift+e and mathematica makes a beep sound while pressing the mentioned combination of keys.

• If you make the whole ActionMenu dynamic there should be no issue with it regenerating when notebooks are changed, I think. Alternatively make it dynamic, populating the actions from a symbol, then update that symbol on mouseover. No need to add it to a tr, really. If you want it as a tr make sure to use FrontEndFlushTextResourceCaches[] to get it to load. – b3m2a1 Jul 17 '17 at 1:51
• 'searches whole notebook' - When is this action performed? – Kuba Jul 17 '17 at 7:08
• @Kuba I am trying to build a generic toolbar that will work in any notebook without any additional work. In other words It's performed on button click, i.e. I press the menu button and it triggers the delayed command. I will update the question with the representative code to make it little less abstract. – Milos Cipovic Jul 17 '17 at 16:36
• @b3m2a1 Thank you for your response. One thing I tried was to make a cell dynamic by assigning it value {} when in Print environment and the above action menu value from above code example when in Working environment. I did it like this: toolbarCell=Dynamic[If[CurrentValue[ScreenStyleEnvironment] == "Working",codeFroExample,{}]]; SetOptions[EvaluationNotebook[],DockedCells->toolbarCell] This works, however, it only works when the output from command is visible on screen. – Milos Cipovic Jul 17 '17 at 18:31
• @b3m2a1 continuation: Although the stylesheet solution would be perfect I would be satisfied with some event driven solution. However I have no idea how to accomplish this, I am not quite shore that I understand how to update it on mouseover, did you mean Mouseover[ActionMenu[...]]? – Milos Cipovic Jul 17 '17 at 18:38

Per my comment you can use a global symbol to track this and have that update on "MouseEntered":

Try:

SetOptions[EvaluationNotebook[],
DockedCells ->
Cell[BoxData[
ToBoxes[
EventHandler[
Dynamic[
"Toc",(*creating list of action menu item names and list of \
commands*)
Replace[$inputNotebookTOC, Except[_List] -> {} ] ] ], "MouseEntered" :> Set[$inputNotebookTOC,
Inner[RuleDelayed,(*1 create list of names*)
Composition[
Map[(*1.3.Extract modified input text into list*)
First@FrontEndExecute@
FrontEndExportPacket[#,
"InputText"] &],(*1.2.read objects into an expression \
and replace CounterBox["typeOfCell"] with counter values*)
CounterBox[style_] :>
ToBoxes@
CurrentValue[#, {"CounterValue",
style}]) &],(*1.1.extract list of section cells \
objects to put in TOC*)
Cells[#,
CellStyle -> {"AbstractSection", "Section", "Subsection",
"Subsubsection", "ReferenceSection",
"EndnoteSection"}] &]@
InputNotebook[],(*2. create list of commands*)
Map[Unevaluated@
NotebookFind[InputNotebook[], #, All, CellID] &,
CurrentValue[
Cells[CellStyle -> {"AbstractSection", "Section",
"Subsection", "Subsubsection", "ReferenceSection",
"EndnoteSection"}], CellID], 1], List]
]
]]
]
]
]


Every time you mouse over the thing it'll fire off an update event on the current InputNotebook

• Thanks. This is great. I must admit now that I didn't understand your comment as a consequence of my unfamiliarity with some basic concepts of front end. I'll test this after work. – Milos Cipovic Jul 18 '17 at 6:00
• @MilosCipovic the main question seems to about hiding it in slide show env. Accepting answer which does not do that is confusing. Could you update the question so it's clear what is your main goal? – Kuba Jul 18 '17 at 9:40
• @Kuba You are absolutely right. The main question was about making the toolbar disappear inside Printing environment. The b3m2a1 answer did actually contribute to the solution and gave me some ideas but still I was to quick in clicking acceptance button. I undid the heresy :). – Milos Cipovic Jul 18 '17 at 12:41

To make the answer complete I will post the whole solution supplemented with b3m2a1's answer.

The main problem was that I wanted to make docked cell toolbar with dynamic elements that appears in Working environment and disappears in Printout environment.

By dynamic element, in my case, I meant an ActionMenu that gives a list of hyperlinks to sections (subsections, subsubsections,.., see the picture from the question) and therefore gives you an easy way to navigate your notebook. The solution that follows is in regards with that ActionMenu.

Although @b3m2a1 did solve the problem of dynamically updating the list in action menu for which I am grateful to him, as @Kuba commented, his answer was not an adequate one for the question posted.

Currently there seems to be no stylesheet based solution? so that we proceed with the solution that uses SetOptions command and is quite simple. Conceptually, in order to make cell, called toolbarCell, appear as docked in Working and disappear in Printout environment you would proceed as follows:

SetOptions[
EvaluationNotebook[],
DockedCells->
Dynamic[
If[CurrentValue[EvaluationNotebook[],ScreenStyleEnvironment] === "Working",
toolbarCell,
{}
]
]
]


But there is a catch, you will have to replace toolbarCell with an expression for your cell, that is, in order to make this to work you can not assign cell to some variable and then pass that variable in place of toolbarCell. You must replace toolbarCell with the whole expression. At least that solved my problem. The whole solution:

SetOptions[
EvaluationNotebook[],
DockedCells ->
Dynamic[
If[CurrentValue[EvaluationNotebook[], ScreenStyleEnvironment] === "Working",
Evaluate@Cell[
BoxData[
ToBoxes[
EventHandler[
Dynamic[
(*creating list of action menu item names and list of commands*)
Replace[$inputNotebookTOC, Except[_List] -> {}]]], "MouseEntered" :> Set[$inputNotebookTOC,
Inner[RuleDelayed,
(*1 create list of names*)
Composition[
Map[
(*1.3.Extract modified input text into list*)
First@FrontEndExecute@FrontEndExportPacket[#,
"InputText"] &],
(*1.2.read objects into an  expression and replace CounterBox["typeOfCell"] with counter values*)
CounterBox[style_] :> ToBoxes@
CurrentValue[#, {"CounterValue", style}]) &],
(*1.1.extract list of section cells objects to put in TOC*)
Cells[#,
CellStyle -> {"AbstractSection", "Section", "Subsection", "Subsubsection", "ReferenceSection", "EndnoteSection"}] &]@
InputNotebook[],
(*2. create list of commands*)
Map[Unevaluated@NotebookFind[InputNotebook[], #, All, CellID] &,
CurrentValue[
Cells[CellStyle -> {"AbstractSection", "Section",
"Subsection", "Subsubsection", "ReferenceSection",
"EndnoteSection"}], CellID], 1], List]]]]]]

, {}]]]
`