Creating Palettes

Palettes are like extensions of your keyboard. They can be used to perform many actions in the Wolfram System, including entering typesetting characters, styling notebooks, and performing evaluations. As with any notebook, creating palettes can be done with a menu item or programmatically.

Palette creation tools.

Palette notebooks have a unique property. They can be a currently selected notebook but cannot normally be an (RefLink[InputNotebook, paclet:ref/InputNotebook]). This allows the palette to perform operations on the notebook into which keyboard input in the front end is directed. See "Manipulating Notebooks from the Kernel" for further information.

Generating a Palette Programmatically

(RefLink[CreatePalette, paclet:ref/CreatePalette])[expr] creates an unsaved palette notebook containing expr and opens it in a separate window. To save the palette, close the window and save the file when the save dialog appears or save the palette notebook programmatically using (RefLink[NotebookSave, paclet:ref/NotebookSave]).

Below is a palette that uses (RefLink[PasteButton, paclet:ref/PasteButton]) to paste characters into the input notebook. (RefLink[PasteButton, paclet:ref/PasteButton]) is specifically designed to work with palettes. It pastes its contents directly into the input notebook at the insertion point. When used in conjunction with (RefLink[RawBoxes, paclet:ref/RawBoxes]), (RefLink[PasteButton, paclet:ref/PasteButton]) will paste uninterpreted boxes into the input notebook.

CreatePalette[Grid[Partition[PasteButton[Style[#,12],RawBoxes[#],ImageSize->24]&/@{"π","E","I","∞","°","×","÷","->",":>","==","≠","≤","≥","∈","∍","¬","∧","∨","⋃","⋂"},5],Spacings->{0,0}]];

Alternatively, use (RefLink[PaletteNotebook, paclet:ref/PaletteNotebook]) to create an in-cell representation of a palette notebook. A separate window containing the notebook can be opened in the front end by clicking the  button or wrapping (RefLink[CreatePalette, paclet:ref/CreatePalette]) around the (RefLink[PaletteNotebook, paclet:ref/PaletteNotebook]) expression.

PaletteNotebook[Grid[Partition[PasteButton[Style[#,12],RawBoxes[#],ImageSize->24]&/@{"π","E","I","∞","°","×","÷","->",":>","==","≠","≤","≥","∈","∍","¬","∧","∨","⋃","⋂"},5],Spacings->{0,0}]]

Since (RefLink[RawBoxes, paclet:ref/RawBoxes])[boxes] inserts boxes verbatim without further interpretation, the characters can be used as part of an input expression or textual cell.

Box structure comparisons.

Input Cell:

πEI∞°×÷->:>==≠≤≥∈∍¬∧∨⋃⋂

Text Cell:

If the box structure for "π" is interpreted as a string in the palette, then regardless of the cell style, the front end will always treat "π" as a string.

Input Cell:

"π"

Text Cell:

"π"

This next palette inserts styled cells into the active notebook using front end tokens. Front end tokens perform front end commands from within the kernel that would normally be done using the menus. For a complete listing of front end tokens, see "Front End Tokens".

CreatePalette[Column[Button[Style[#,12,FontFamily->"Times"],FrontEndTokenExecute[InputNotebook[],"Style",#],Appearance->"Palette",ImageSize->120]&/@{"Title","Subtitle","Subsubtitle","Section","Subsection","Subsubsection","Text","Code","Input"},Spacings->0]];

The palette below links to the Wolfram Language Documentation Center, allows the deletion of all output cells in the input notebook, and can also quit the kernel.

f[a_,b_]:=Button[Style[a,12,FontFamily->"Times"],FrontEndTokenExecute[b],Appearance->"Palette",ImageSize->156]

g[a_,b_]:=Hyperlink[Style[a,12,FontFamily->"Times"],b,Appearance->"Palette",BaseStyle->{FontColor->Black},ImageSize->156]

PaletteNotebook@Column[Flatten[{g@@@{{"Documentation Center","paclet:guide/Mathematica"},{"Alphabetical Function Listing","paclet:guide/AlphabeticalListing"}},f@@@{{"Delete Output","DeleteGeneratedCells"},{"Quit Kernel","EvaluatorQuit"}}}],Spacings->0]

(RefLink[CreatePalette, paclet:ref/CreatePalette]) was used to create several Wolfram System palettes, including the Basic Math Assistant palette.

Generating a Palette from a Selection

Palettes can also be created by selecting a cell or its contents and choosing (MenuGrayLink[Palettes, paclet:guide/PalettesMenu]) ▶ (MenuGrayLink[Generate Palette from Selection, paclet:ref/menuitem/GeneratePaletteFromSelection]). To save the palette, close the window and save the file when the save dialog appears.

For example, consider the grid below, which pastes input templates into the input notebook.

Grid[Partition[PasteButton[RawBoxes[#],ImageSize->{80,40},Appearance->"FramedPalette"]&/@{"(Sqrt[\[SelectionPlaceholder]])","(\[SelectionPlaceholder]^(1/\[Placeholder]))","∫\[SelectionPlaceholder]\[DifferentialD]\[Placeholder]","(∫_\[Placeholder]^\[Placeholder])\[SelectionPlaceholder]\[DifferentialD]\[Placeholder]","(∂_\[Placeholder]) \[SelectionPlaceholder]","(∂_(\[Placeholder], \[Placeholder]))\[SelectionPlaceholder]","(∑_(\[Placeholder] = \[Placeholder])^\[Placeholder])\[SelectionPlaceholder]","(∏_(\[Placeholder] = \[Placeholder])^\[Placeholder])\[SelectionPlaceholder]","(\!\(\*GridBox[{{\[SelectionPlaceholder], \[Placeholder]},{\[Placeholder], \[Placeholder]}
}]\))","\[SelectionPlaceholder]:=\[Piecewise]\!\(\*GridBox[{{\[Placeholder], \[Placeholder]},{\[Placeholder], \[Placeholder]}
}]\)"},2],Spacings->{0,.1}]

Create a palette by selecting the grid and choosing (MenuGrayLink[Palettes, paclet:guide/PalettesMenu]) ▶ (MenuGrayLink[Generate Palette from Selection, paclet:ref/menuitem/GeneratePaletteFromSelection]). The unsaved palette will appear in a separate window.

Although this can sometimes be useful for temporary or experimental purposes, this is not the best method for palette development. Using (RefLink[CreatePalette, paclet:ref/CreatePalette]) allows you to write a program that, when evaluated, can fully automate the creation of palettes. The menu item requires manual intervention, which can be subject to errors. This can lead to the creation of palettes that will be difficult to recreate if you wish to tweak them or fix bugs in them.

Finalizing and Saving the Palette

(RefLink[CreatePalette, paclet:ref/CreatePalette]) and (MenuGrayLink[Palettes, paclet:guide/PalettesMenu]) ▶ (MenuGrayLink[Generate Palette from Selection, paclet:ref/menuitem/GeneratePaletteFromSelection]) by default create a notebook with options set so as to be suitable for the appearance and behavior of a typical palette. However, options such as (RefLink[Magnification, paclet:ref/Magnification]) and DynamicUpdating may also be useful.

To save the palette, click the close button in the palette window and you will be prompted to save the palette or save the palette notebook programmatically using (RefLink[NotebookSave, paclet:ref/NotebookSave]).

Installing the Palette into the Wolfram System

A palette must be installed on the (RefLink[PalettePath, paclet:ref/PalettePath]) for it to appear in the main Palettes menu.

Dynamic[ToFileName/@AbsoluteCurrentValue[$FrontEnd,PalettePath]]

The simplest way install a palette is to choose (MenuGrayLink[Palettes, paclet:guide/PalettesMenu]) ▶ (MenuGrayLink[Install Palette, paclet:ref/menuitem/InstallPalette]), provide the source file, palette name, and installation directory to the Install Mathematica Item dialog, and click Finish.

Palettes can also be installed as part of a Wolfram System application. To add a palette to your application, create a FrontEnd\Palettes\ directory within the application folder and save the palette to this location. For example, the palette MyPalette.nb would be placed in MyApplication\FrontEnd\Palettes\.

Extended Examples

Dependent Opener Views

The second argument of (RefLink[Dynamic, paclet:ref/Dynamic]) can be used to control the state of an opposite opener view so that only one remains open at any given time.

CreatePalette[DynamicModule[{opener1=True,opener2=False},
Column[{OpenerView[{"First Opener","text"},Dynamic[opener1,(opener1=#;opener2=!opener2)&]],OpenerView[{"Second Opener","text"},Dynamic[opener2,(opener2=#;opener1=!opener1)&]]}]],WindowTitle->"Palette"];

Palette states can also be tracked and controlled using (RefLink[TaggingRules, paclet:ref/TaggingRules]). Unlike dynamic variables, (RefLink[TaggingRules, paclet:ref/TaggingRules]) are automatically cached by the front end so that installed palettes can remember their control object states across multiple front end sessions, even if they cannot be saved. This topic is beyond the scope of this document, but a detailed discussion and set of examples can be found in "Storing and Tracking Palette States".

Applying and Evaluating Functions

Palettes can run evaluations and return the results to a notebook.

PaletteNotebook@Column[Button[Defer[#[\[SelectionPlaceholder]]],Inherited,BaseStyle->"Evaluate",Evaluator->None,Appearance->"Palette"]&/@{Expand,Factor,Apart},Spacings->0.1]

Consider the following polynomial. Highlight the expression and choose (RefLink[Expand, paclet:ref/Expand])[■] from the palette above. The Wolfram Language will apply the function to the selection and evaluate the resulting expression in place.

Before

(((4 (x^4)+8 (x^2)))^4)

After

4096 (x^8)+8192 (x^10)+6144 (x^12)+2048 (x^14)+256 (x^16)