0
$\begingroup$

I coded a compact toolbar in its own file nbtaskmate.nb for various common notebook tasks which I would like to load onto any notebook I might have open with a simple command in a cell without having to copy all of the actual code onto a new cell in that notebook. I am not sure how to go about it.

I do not use $Path directories for my coded notebooks. Instead I use a directory in Google Drive which I call mathematica. I wish to load nbtaskmate.nb from a subdirectory mathematica\resources. By the way I am using Windows 10.

For clarity my open notebook would exist in mathematica\notebook.nb and my toolbar generating function would exist in mathematica\resources\nbtaskmate.nb

I tried using Get and FilePrint and both generated $Failed. I do not know why. I am confident it is an easy task as Mathematica is equipped with many useful low-level functions for loading code and manipulating notebooks. It is just too bad I do not understand them well.

enter image description here

The following is the code for my docked cells toolbar. No worries about loading it. There is a button for closing it.

nbTaskmate[] := Block[
   {nb, bgcolor, evalInitCells, deleteOutput, tooltipOps, brandstyle, 
    datestyle, brandOps, buttonstyle, buttonOps, arrowstyle, xstyle, 
    xOps, butgridOps, bargridOps, dockcellOps, brand, tasks, grid, 
    bar},
   nb = EvaluationNotebook[];
   bgcolor = RGBColor[0, 0, .5, 1];
   
   (* styles and options here for consistency *)
   
   tooltipOps[string_ : "tooltip"] := 
    Sequence[Tooltip -> string, TooltipDelay -> .5, 
     TooltipStyle -> {FontSize -> 12, Background -> LightYellow, 
       CellFrameColor -> bgcolor, 
       CellFrameMargins -> {{10, 10}, {5, 5}}}];
   brandstyle = Sequence[FontFamily -> "Helvetica", 12, Bold];
    datestyle = Sequence[FontFamily -> "Verdana", 12, Green];
   brandOps[] := 
    Sequence[Appearance -> None, ContentPadding -> False, 
     ImageSize -> Full, ImageMargins -> 0];
   buttonstyle = Sequence[FontFamily -> "Arial", 10, bgcolor];
   buttonOps[width_ : 50] := 
    Sequence[Appearance -> "Palette", Background -> LightBlue, 
     Alignment -> Center, ImageSize -> {width, 16}];
   arrowstyle = Sequence[FontFamily -> "Courier", 16, Bold, Black]; 
   xstyle = Sequence[FontFamily -> "Arial", 10, Bold, White];
   xOps = 
    Sequence[Appearance -> "Palette", Background -> Darker[Red], 
     Alignment -> Center, ImageSize -> {14, 12}];
   butgridOps[sp_ : .2, gap_ : 1.5] := 
    Sequence[
     Spacings -> {{0, sp, sp, gap, sp, gap, sp, sp, gap, sp, gap}, 0}];
   bargridOps[] := 
    Sequence[ItemSize -> {{Scaled[.3], Scaled[.7]}, Automatic}, 
     Spacings -> 0, Alignment -> {{Left, Right}, Center}, 
     Background -> bgcolor, BaseStyle -> {White, 10}];
   dockcellOps[] := 
    Sequence[Background -> bgcolor, CellFrame -> False, 
     CellFrameMargins -> {{12, 3}, {5, 5}}];
   
   (* left side of toolbar *)
   
   brand = 
    Button[Row[{Style["NB", brandstyle, LightBlue], 
       Style["TASKMATE", brandstyle, Red],
       Style["  \[RuleDelayed]  ", brandstyle, White],
       Style[Dynamic[DateString[], UpdateInterval -> 1], datestyle]
       }], Evaluate[brandOps[]], 
     tooltipOps[
      "NBTASKMATE 1.0 by Jules Manson\nplease submit questions,\n\
suggestions, or bugs to\[email protected]"]
     ];
   
   (* right side of toolbar *)
   
   tasks = Grid[
     {{
       Button[Style["Save", buttonstyle], 
        FrontEndTokenExecute["Save"], buttonOps[], 
        tooltipOps["Save Notebook"]],
       Button[Style["Save As", buttonstyle], 
        FrontEndTokenExecute["SaveRename"], buttonOps[], 
        tooltipOps["Save As Notebook"]],
       Button[Style["Revert", buttonstyle], 
        FrontEndTokenExecute["Revert"], buttonOps[], 
        tooltipOps["Revert to last Save"]],
       Button[Style["Eval Init", buttonstyle], 
        FrontEndExecute[FrontEndToken["EvaluateInitialization"]], 
        buttonOps[], tooltipOps["Evaluate Initialization Cells"]],
       Button[Style["Eval NB", buttonstyle], 
        NotebookEvaluate[EvaluationNotebook[], InsertResults -> True],
         buttonOps[], tooltipOps["Evaluate Notebook"], 
        Method -> "Queued"],
       Button[Style["ClearAll", buttonstyle], ClearAll["Global`*"], 
        buttonOps[], 
        tooltipOps[
         "Clear All Definitions, Attributes, and Messages\nCaution: \
after using some buttons may not work.\nReload toolbar to fix this."]],
       Button[Style["\[DeleteKey] Output", buttonstyle], 
        FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]], 
        buttonOps[], tooltipOps["Delete All Output"]],
       Button[Style["\[DeleteKey] Cache", buttonstyle], 
        ClearSystemCache["Symbolic"], buttonOps[], 
        tooltipOps["Clear System Cache"]], 
       Button[Style["\[UpArrow]", arrowstyle], 
        FrontEndTokenExecute["ScrollNotebookStart"], buttonOps[20], 
        tooltipOps["Scroll to Top"]],
       Button[Style["\[DownArrow]", arrowstyle], 
        FrontEndTokenExecute["ScrollNotebookEnd"], buttonOps[20], 
        tooltipOps["Scroll to Bottom"]],
       Button[Style["x", xstyle], 
        Function[Null, 
         SetOptions[EvaluationNotebook[], DockedCells -> Inherited, 
          TaggingRules -> Inherited, CellContext -> Inherited]], xOps,
         tooltipOps["Remove Toolbar"]]
       }},
     butgridOps[]
     ];
   
   (* putting it all together *)
   
   bar = Grid[{{brand, tasks}}, bargridOps[]];
   SetOptions[EvaluationNotebook[], 
    DockedCells -> 
     Cell[BoxData[ToBoxes[bar]], "DockedCell", 
      Evaluate[dockcellOps[]]]]];

nbTaskmate[];
$\endgroup$
2
  • 1
    $\begingroup$ Get @ FileNameJoin[{NotebookDirectory[], "resources", "nbtaksmate.nb"}];, but first save nbtaskmate as a .m file. $\endgroup$
    – Kuba
    Oct 22, 2020 at 6:31
  • $\begingroup$ @Kuba thanks for the code. This probably saved me an entire day of researching documentation.The thing is that Mathematica does so many things similarly (and well) that a novice can find it confusing deciphering which methods to use. I learned today that it all boils down to a simple lesson in package development (which I read recently). Now i have to figure out how namespacing in mathematica works and how to actually install a package but that shouldn't be too difficult. By the way my code took no perceivable time to execute (less than a second) but then again my projects are barely widgets. $\endgroup$ Oct 23, 2020 at 5:30

1 Answer 1

1
$\begingroup$

Make in your new notebook notebook.nb two cells. First:

NotebookEvaluate[
 FileNameJoin[{NotebookDirectory[EvaluationNotebook[]], "resources", 
   "nbtaskmate.nb"}]]

Then evaluate it and wait till it finishes. After that evaluate the second cell, which is

nbTaskmate[]

I use this approach for other purposes, unfortunately for some reasons (which are unclear for me, may be some timeouts?) if your will try to to evaluate both cells simultaneously the second one sometimes will not be evaluated (at least in my case, when nbtaskmate.nb takes some time to execute). The draw back that cell labels and $Line is affected (can reset them in the end of nbtaskmate.nb)

$\endgroup$
1
  • $\begingroup$ thank you this worked but I made a slight change. I did as Kuba suggested by first saving the function in an m file and the following code reflects this: NotebookEvaluate[ FileNameJoin[{NotebookDirectory[EvaluationNotebook[]], "resources", "nbtaskmate.m"}]] $\endgroup$ Oct 22, 2020 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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