Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am in the process of creating a palette for a specialised "Save as HTML". For context please see this.

How can I grab the CellTags from a Cell[...] object? This is my first iteration, aka testing code:

 Module[{action, create},
  create := NotebookCreate[];
  action[nb_] := Module[{found, out, cell},
    out = create;
    SelectionMove[nb, Before, Notebook];
    found = NotebookFind[nb, "Input", Next, CellStyle];
    If[found != $Failed,
     cell = NotebookRead[nb];
     NotebookWrite[out, OptionValue[cell, CellTags]] (* 1 *),
     NotebookWrite[out, "Not found"]
  Button[Style["Blog it", 12, FontFamily -> "Times"],
   Appearance -> "Palette"]

At the moment it just dumps data into a temporary empty notebook if it finds the Input cell, but the code at (* 1 *) does not print the CellTags the cell has in the InputNotebook[]. I am thinking that OptionsPattern[] may be the answer, but I am not sure how to use it. I tried

cell = NotebookRead[nb];
tags = cell /. Cell[__, OptionsPattern[]] :> OptionValue[CellTags];
NotebookWrite[out, tags],

but it doesn't work either.

share|improve this question
Oh dear, @Rojo posted an answer and got removed in few mins! Why? – carlosayam Nov 23 '12 at 23:53
He did it himself. May have been a bug in it or so. – Sjoerd C. de Vries Nov 24 '12 at 9:20
Actually it worked. However @Rojo himself gave +1 to Albert's, so I just accepted that one... "Why?" A mystery :) – carlosayam Nov 24 '12 at 11:11
up vote 3 down vote accepted

As you already have the selection set to the cell and NotebookFind returns that selection, you could just use CurrentValue on it as here:

 Button[Style["Blog it", 12, FontFamily -> "Times"],
    nb = InputNotebook[],
    out = NotebookCreate[],
   SelectionMove[nb, Before, Notebook];
   retval = NotebookFind[nb, "Input", Next, CellStyle];
     _NotebookSelection, CurrentValue[retval, CellTags],
     _, "Not found"
  Appearance -> "Palette"

as already mentionend here I'd strongly suggest to read the joy of tagging if you intend to do more advanced work with CellTags, which seems to be a reasonable idea for what you plan.

share|improve this answer
+1, way better... – Rojo Nov 24 '12 at 0:42
Albert, the answer is more Mathematica style, thanks. I will have a look a the pointer. – carlosayam Nov 24 '12 at 5:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.