Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

PaletteNotebook[
 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"],
   action[InputNotebook[]],
   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? –  caya 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 :) –  caya Nov 24 '12 at 11:11

1 Answer 1

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:

PaletteNotebook[
 Button[Style["Blog it", 12, FontFamily -> "Times"],
  Module[{
    nb = InputNotebook[],
    out = NotebookCreate[],
    retval
    },
   SelectionMove[nb, Before, Notebook];
   retval = NotebookFind[nb, "Input", Next, CellStyle];
   NotebookWrite[out,
    Switch[retval,
     _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. –  caya Nov 24 '12 at 5:43

Your Answer

 
discard

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.