MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Execute on notebook creation

  • To: mathgroup at
  • Subject: [mg117229] Re: Execute on notebook creation
  • From: Jason Ledbetter <jasonbrent at>
  • Date: Sat, 12 Mar 2011 05:08:29 -0500 (EST)

I was working on a project that needed something similar to what you have

There isn't a method to automatically execute code when a notebook is
created. Apparently it's a security feature.

Options that are ~= close:


1) Create a notebook with a defined InitializationCell that will add the
docked cells to the evaluation notebook.
2) Save that notebook to disk.
3) Open that notebook
4) Click "yes" to the popup(!!) that asks about evaluating


1) Write a function called "newNotebook[]" (or whatever)
that programmatically, creates a notebook using CreateDocument[] and
NotebookWrite[] with a docked cell then SelectionMoves[] past the docked
cell into the normal notebook (SelectionMove+dockedCells are ~= broken in
Mathematica 7, works in Mathematica 8).
2) Load the .m with newNotebook[] defined in your init.m.
3) run newNotebook[] any time you want a new notebook with the docked cell
instead of file->new->notebook.

That code would look something like this: (this is a skeleton from some test
work I was doing so you'll have to clean it up but it should run as is)


foo[] := foo[Unique[foo]];
foo[fooID_] := Module[
   f = fooID

  f@"doc" = CreateDocument[];

  dockConfig =
      Text["This is my ID:"],
      Text["Value: "],

   DockedCells -> Cell[BoxData@ToBoxes@dockConfig, Background ->
  SelectionMove[f@"doc", After, Notebook];
   Cell@BoxData@ToBoxes@Slider[Dynamic[f@"value"], {{"one", "two",


There may be other options, but if one wants to futz with specific contexts
and such, DockedCells begins to be much more of a pain than a feature...

Hope this helps.


On Fri, Mar 11, 2011 at 4:37 AM, Achilleas Lazarides <
achilleas.lazarides at> wrote:

> Hi,
> Thanks for the reply. That is what I do too at the moment (ie have a
> button in a palette I always display), but I was thinking more along the
> lines of what one can do with init.m for the kernel (ie, define stuff
> that one uses constantly every time the kernel is restarted).
> On 10 Mar 2011, at 22:00, David Annetts wrote:
> > Hi Achilleas,
> >
> > It probably is possible from the menu, but I create new notebooks with
> > pre-docked cells using a palette.  I'd rather not play with this
> > stuff.
> >
> > Essentially, it's a call to CreateDocument[] in which you can do
> > pretty
> > much what you want.
> >
> > D.

  • Prev by Date: Re: Learning maths with mathematica
  • Next by Date: Re: FindFit power law problem
  • Previous by thread: Re: Execute on notebook creation
  • Next by thread: Re: Execute on notebook creation