A Cell Tags Button

John M. Novak
novak@wolfram.com

[Graphics:../Images/tricks_gr_231.gif]

The [Graphics:../Images/tricks_gr_232.gif] mechanism provides a way to associate keywords or tags to notebook cells. Version 3.0 supports hyperlinks to cells with specified tags. Cell tags can also be used to compile an index for a notebook. Here is a simple button that adds selected text in a cell to the [Graphics:../Images/tricks_gr_233.gif] of that cell. (The button can be turned into a palette using Generate Palette from Selection under the File menu.)

[Graphics:../Images/tricks_gr_234.gif]

To see the code that defines this button, select the button's cell and use Show Expression under the Format menu. This is what you should see:

[Graphics:../Images/tricks_gr_235.gif]

Here is a brief explanation of the code. First we read the selected text from the [Graphics:../Images/tricks_gr_236.gif].

[Graphics:../Images/tricks_gr_237.gif]

(This implementation is not optimal because [Graphics:../Images/tricks_gr_238.gif] should be a local symbol, not in the global context.) If the selection is not a [Graphics:../Images/tricks_gr_239.gif], we convert it to one.

[Graphics:../Images/tricks_gr_240.gif]

We then move the selection to be the entire cell.

[Graphics:../Images/tricks_gr_241.gif]

Lastly we modify [Graphics:../Images/tricks_gr_242.gif] for the cell to include [Graphics:../Images/tricks_gr_243.gif].

[Graphics:../Images/tricks_gr_244.gif]

We have to check if there are any existing [Graphics:../Images/tricks_gr_245.gif] and also if the [Graphics:../Images/tricks_gr_246.gif] is already present. [Graphics:../Images/tricks_gr_247.gif] is used to eliminate duplicate tags. Note that a single cell tag is saved as a [Graphics:../Images/tricks_gr_248.gif], while multiple tags are stored as a [Graphics:../Images/tricks_gr_249.gif].

There are a good many pitfalls in the design of button functions. The most likely problems involve button function argument handling, the syntax that the front end understands, and the distinction between [Graphics:../Images/tricks_gr_250.gif] and [Graphics:../Images/tricks_gr_251.gif]. Also, option handling is irregular (which is part of the reason for the slightly eccentric code above).

Presently, button programming is not well documented. To learn more about programming buttons, users should have a look at the examples in the help browser under "Getting Started/Demos." Look especially at the palette demos Polyhedron Explorer, Periodic Table, and Physical Constants.

Document converted by Mathematica of Wolfram Research