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.
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[];
Get @ FileNameJoin[{NotebookDirectory[], "resources", "nbtaksmate.nb"}];
, but first save nbtaskmate as a .m file. $\endgroup$