4
$\begingroup$

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.

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

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.

$\endgroup$
  • $\begingroup$ Albert, the answer is more Mathematica style, thanks. I will have a look a the pointer. $\endgroup$ – carlosayam Nov 24 '12 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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