Mathematica Asked on December 12, 2020
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 mathematicaresources
. By the way I am using Windows 10.
For clarity my open notebook would exist in mathematicanotebook.nb
and my toolbar generating function would exist in mathematicaresourcesnbtaskmate.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 Mansonnplease submit questions,n
suggestions, or bugs [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 MessagesnCaution:
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[];
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)
Correct answer by user18792 on December 12, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP