Manipulating Notebooks from the Kernel

If you want to do simple operations on Wolfram System notebooks, then you will usually find it convenient just to use the interactive capabilities of the standard Wolfram System front end. But if you want to do more complicated and systematic operations, then you will often find it better to use the kernel.

Notebooks[]a list of all your open notebooks
Notebooks["name"]a list of all open notebooks with the specified name
InputNotebook[]the notebook into which typed input will go
EvaluationNotebook[]the notebook in which this function is being evaluated
ButtonNotebook[]the notebook containing the button (if any) which initiated this evaluation

Functions that give the notebook objects corresponding to particular notebooks.

Within the Wolfram Language kernel, notebooks that you have open in the front end are referred to by notebook objects of the form NotebookObject[fe,id]. The first argument of NotebookObject specifies the for the front end in which the notebook resides, while the second argument gives a unique serial number for the notebook.

Here is a notebook named .

3.gif

This finds the corresponding notebook object in the front end.
In[1]:=
Click for copyable input
Out[1]=
This gets the expression corresponding to the notebook into the kernel.
In[2]:=
Click for copyable input
Out[2]=
This replaces every occurrence of the string by "Text".
In[3]:=
Click for copyable input
Out[3]=
This creates a new modified notebook in the front end.
In[4]:=
Click for copyable input

5.gif

Out[4]=
NotebookGet[obj]get the notebook expression corresponding to the notebook object obj
NotebookPut[expr,obj]replaces the notebook represented by the notebook object obj with one corresponding to expr
NotebookPut[expr]creates a notebook corresponding to expr and makes it the currently selected notebook in the front end

Exchanging whole notebook expressions between the kernel and front end.

If you want to do extensive manipulations on a particular notebook you will usually find it convenient to use NotebookGet to get the whole notebook into the kernel as a single expression. But if instead you want to do a sequence of small operations on a notebook, then it is often better to leave the notebook in the front end, and then to send specific commands from the kernel to the front end to tell it what operations to do.

The Wolfram Language is set up so that anything you can do interactively to a notebook in the front end you can also do by sending appropriate commands to the front end from the kernel.

Options[obj]give a list of all options set for the notebook corresponding to notebook object obj
Options[obj,option]give the option setting
AbsoluteOptions[obj,option]give the option setting with absolute option values even when the actual setting is Automatic
CurrentValue[obj,option]give and set the value of option
SetOptions[obj,option->value]set the value of an option

Finding and setting options for notebooks.

This gives the setting of the WindowSize option for your currently selected notebook.
In[5]:=
Click for copyable input
Out[5]=
This changes the size of the currently selected notebook on the screen.
In[6]:=
Click for copyable input

6.gif

Out[6]=
Alternatively, use CurrentValue to directly get the value of the WindowSize option.
In[7]:=
Click for copyable input
Out[7]=
This changes the option using CurrentValue with a simple assignment.

Within any open notebook, the front end always maintains a current selection. The selection can consist for example of a region of text within a cell or of a complete cell. Usually the selection is indicated on the screen by some form of highlighting. The selection can also be between two characters of text, or between two cells, in which case it is usually indicated on the screen by a vertical or horizontal insertion bar.

You can modify the current selection in an open notebook by issuing commands from the kernel.

SelectionMove[obj,Next,unit]move the current selection to make it be the next unit of the specified type
SelectionMove[obj,Previous,unit]move to the previous unit
SelectionMove[obj,After,unit]move to just after the end of the present unit of the specified type
SelectionMove[obj,Before,unit]move to just before the beginning of the present unit
SelectionMove[obj,All,unit]extend the current selection to cover the whole unit of the specified type

Moving the current selection in a notebook.

Characterindividual character
Wordword or other token
Expressioncomplete subexpression
TextLineline of text
TextParagraphparagraph of text
GraphicsContentsthe contents of the graphic
Graphicsgraphic
CellContentsthe contents of the cell
Cellcomplete cell
CellGroupcell group
EvaluationCellcell associated with the current evaluation
ButtonCellcell associated with any button that initiated the evaluation
GeneratedCellcell generated by the current evaluation
Notebookcomplete notebook

Units used in specifying selections.

Here is a simple notebook.

8.gif

This sets to be the notebook object corresponding to the current input notebook.
In[9]:=
Click for copyable input
This moves the current selection within the notebook to be the next word.
This extends the selection to the complete first cell.
This puts the selection at the end of the whole notebook.
NotebookFind[obj,data]move the current selection to the next occurrence of the specified data in a notebook
NotebookFind[obj,data,Previous]move to the previous occurrence
NotebookFind[obj,data,All]make the current selection cover all occurrences
NotebookFind[obj,data,dir,elems]search in the specified elements of each cell, going in direction dir
NotebookFind[obj,"text",IgnoreCase->True]
do not distinguish uppercase and lowercase letters in text

Searching the contents of a notebook.

This moves the current selection to the position of the previous occurrence of the word .
The letter does not appear in the current notebook, so $Failed is returned, and the selection is not moved.
In[14]:=
Click for copyable input

16.gif

Out[14]=
CellContentscontents of each cell
CellStylethe name of the style for each cell
CellLabelthe label for each cell
CellTagstags associated with each cell
{elem1,elem2,}several kinds of elements

Possible elements of cells to be searched by NotebookFind.

In setting up large notebooks, it is often convenient to insert tags which are not usually displayed, but which mark particular cells in such a way that they can be found using NotebookFind. You can set up tags for cells either interactively in the front end, or by explicitly setting the CellTags option for a cell.

NotebookLocate["tag"]locate and select cells with the specified tag in the current notebook
NotebookLocate[{"file","tag"}]open another notebook if necessary

Globally locating cells in notebooks.

NotebookLocate is typically the underlying function that the Wolfram Language calls when you follow a hyperlink in a notebook. The Insert Hyperlink menu item sets up the appropriate NotebookLocate as part of the script for a particular hyperlink button.

NotebookWrite[obj,data]write data into a notebook at the current selection
NotebookApply[obj,data]write data into a notebook, inserting the current selection in place of the first that appears in data
NotebookDelete[obj]delete whatever is currently selected in a notebook
NotebookRead[obj]get the expression that corresponds to the current selection in a notebook

Writing and reading in notebooks.

NotebookWrite[obj,data] is similar to a Paste operation in the front end: it replaces the current selection in your notebook by data. If the current selection is a cell NotebookWrite[obj,data] will replace the cell with data. If the current selection lies between two cells, however, then NotebookWrite[obj,data] will create an appropriate new cell or cells.

Here is a notebook with a word of text selected.

18.gif

This replaces the selected word by new text.
This moves the current selection to just after the first cell in the notebook.
This now inserts a text cell after the first cell in the notebook.
This makes the current selection be the next cell in the notebook.
This reads the current selection, returning it as an expression in the kernel.
In[19]:=
Click for copyable input

23.gif

Out[19]=

NotebookWrite[obj,data] just discards the current selection and replaces it with data. But particularly if you are setting up palettes, it is often convenient first to modify data by inserting the current selection somewhere inside it. You can do this using selection placeholders and NotebookApply. The first time the character "", entered as or EscsplEsc, appears anywhere in data, NotebookApply will replace this character by the current selection.

Here is a simple notebook with the current selection being the contents of a cell.
This replaces the current selection by a string that contains a copy of its previous form.
SelectionEvaluate[obj]evaluate the current selection in place
SelectionCreateCell[obj]create a new cell containing just the current selection
SelectionEvaluateCreateCell[obj]evaluate the current selection and create a new cell for the result
SelectionAnimate[obj]animate graphics in the current selection
SelectionAnimate[obj,t]animate graphics for seconds

Operations on the current selection.

This makes the current selection be the whole contents of the cell.
This evaluates the current selection in place.

SelectionEvaluate allows you to take material from a notebook and send it through the kernel for evaluation. On its own, however, SelectionEvaluate always overwrites the material you took. But by using functions like SelectionCreateCell you can maintain a record of the sequence of forms that are generatedjust like in a standard Wolfram System session.

This makes the current selection be the whole cell.
This creates a new cell and copies the current selection into it.
This wraps Factor around the contents of the current cell.
This evaluates the contents of the current cell and creates a new cell to give the result.

Functions like NotebookWrite and SelectionEvaluate by default leave the current selection just after whatever material they insert into your notebook. You can then always move the selection by explicitly using SelectionMove. But functions like NotebookWrite and SelectionEvaluate can also take an additional argument which specifies where the current selection should be left after they do their work.

NotebookWrite[obj,data,sel]write data into a notebook, leaving the current selection as specified by sel
NotebookApply[obj,data,sel]write data replacing by the previous current selection, then leaving the current selection as specified by sel
SelectionEvaluate[obj,sel]evaluate the current selection, making the new current selection be as specified by sel
SelectionCreateCell[obj,sel]create a new cell containing just the current selection, and make the new current selection be as specified by sel
SelectionEvaluateCreateCell[obj,sel]evaluate the current selection, make a new cell for the result, and make the new current selection be as specified by sel

Performing operations and specifying what the new current selection should be.

Afterimmediately after whatever material is inserted (default)
Beforeimmediately before whatever material is inserted
Allthe inserted material itself
Placeholderthe first in the inserted material
Noneleave the current selection unchanged

Specifications for the new current selection.

Here is a blank notebook.
This writes into the notebook, making the current selection be what was written.
This evaluates the current selection, creating a new cell for the result, and making the current selection be the whole of the result.
This wraps FactorInteger around the current selection.
This evaluates the current selection, leaving the selection just before the result.
This now inserts additional text at the position of the current selection.
Options[obj,option]find the value of an option for a complete notebook
Options[NotebookSelection[obj],option]
find the value for the current selection
SetOptions[obj,option->value]set the value of an option for a complete notebook
SetOptions[NotebookSelection[obj],option->value]
set the value for the current selection

Finding and setting options for whole notebooks and for the current selection.

Make the current selection be a complete cell.
Put a frame around the cell that is the current selection.
CreateWindow[]create a new notebook
CreateWindow[options]create a notebook with specified options
NotebookOpen["name"]open an existing notebook
NotebookOpen["name",options]open a notebook with specified notebook options
SetSelectedNotebook[obj]make the specified notebook the selected one
NotebookPrint[obj]send a notebook to your printer
NotebookPrint[obj,"file"]send a PostScript version of a notebook to a file
NotebookPrint[obj,"!command"]send a PostScript version of a notebook to an external command
NotebookSave[obj]save the current version of a notebook in a file
NotebookSave[obj,"file"]save the notebook in a file with the specified name
NotebookClose[obj]close a notebook

Operations on whole notebooks.

If you call CreateWindow[] a new empty notebook will appear on your screen.

By executing commands like SetSelectedNotebook and NotebookOpen, you tell the Wolfram System front end to change the windows you see. Sometimes you may want to manipulate a notebook without ever having it displayed on the screen. You can do this by using the option setting Visible->False in NotebookOpen or CreateWindow.