Re: Execute on notebook creation
- To: mathgroup at smc.vnet.net
- Subject: [mg117229] Re: Execute on notebook creation
- From: Jason Ledbetter <jasonbrent at gmail.com>
- Date: Sat, 12 Mar 2011 05:08:29 -0500 (EST)
I was working on a project that needed something similar to what you have asked. There isn't a method to automatically execute code when a notebook is created. Apparently it's a security feature. Options that are ~= close: A) 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 InitializationCells. B) 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) --snip-- foo[] := foo[Unique[foo]]; foo[fooID_] := Module[ { f = fooID }, f@"doc" = CreateDocument[]; SetSelectedNotebook[f@"doc"]; dockConfig = Panel@Row[ { Text["This is my ID:"], Dynamic[f], Text["Value: "], Dynamic[f@"value"] } ]; SetOptions[f@"doc", DockedCells -> Cell[BoxData@ToBoxes@dockConfig, Background -> LightGray]]; SelectionMove[f@"doc", After, Notebook]; NotebookWrite[f@"doc", Cell@BoxData@ToBoxes@Slider[Dynamic[f@"value"], {{"one", "two", "three"}}]]; ] --snip-- 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. -jbl On Fri, Mar 11, 2011 at 4:37 AM, Achilleas Lazarides < achilleas.lazarides at gmx.com> 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. > >