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.

In my quest to a custom "Save as HTML" (please see context here) I am now able to iterate through the notebook, grab Input cells decorated with a CellTag that, by convention, is going to be a file name of a CDF file. The CDF needs to come from the corresponding Output cell if that Input cell. For simplicity, I am assuming it is the next one. So, I am trying to SelectionMove[nb, Next, Cell] (* 1 *) but my debugging in (* 2 *) doesn't work and the actual export in (* 3 *) fails. See below

PaletteNotebook[
 Button[Style["Blog it", 12, FontFamily -> "Times"],
  Block[{
    nb = InputNotebook[],
    out = NotebookCreate[],
    fpath = NotebookDirectory[InputNotebook[]],
    generateCDF,
    generateCDFs,
    exportHTML
    },
   generateCDF[nb_, name_] := Module[{sel},
     sel = SelectionMove[nb, Next, Cell]; (* 1 *)

     Message[generateCDF::info, name];
     NotebookWrite[out, CurrentValue[{sel, Cell}]]; (* 2 *)

     Export[
      FileNameJoin[{fpath, name}],
      CurrentValue[sel, Cell],
      "CDF"]; (* 3 *)

     SelectionMove[nb, "Input", Next, CellStyle]
     ];
   generateCDF::info = "Generating CDF `1`";
   generateCDFs[nb_] := Module[{sel, curID},
     SelectionMove[nb, Before, Notebook];
     sel = NotebookFind[nb, "Input", Next, CellStyle];
     While[ MatchQ[sel, _NotebookSelection],
      curID = CurrentValue[sel, CellID];
      sel = Switch[CurrentValue[sel, CellTags],
        _String, generateCDF[nb, CurrentValue[sel, CellTags]],
        _, NotebookFind[nb, "Input", Next, CellStyle]
        ];
      ];
     ];
   exportHTML[nb_] := False; (* yet to be done, ignore for now *)

   generateCDFs[nb];
   exportHTML[nb];
   ],
  Appearance -> "Palette"]
 ]

The Message is printing properly in the message window.

share|improve this question

1 Answer 1

Have you any indication that CurrentValue[{sel,Cell}] should work? I didn't find any such statement in the documentation so I think in this case you need to use NotebookRead instead of CurrentValue. Note that even CurrentValue[nb,"SelectionData"] won't get you the selected cell but rather seems to be addressing dynamic selection with the mouse. Another thing you need to change is that Export[...,"CDF"] doesn't accept Cell expressions but wants Notebook expressions. Alltoghether this works for me:

PaletteNotebook[
 Button[
  Style["Blog it", 12, FontFamily -> "Times"],
  Module[{
    nb = InputNotebook[],
    fpath = NotebookDirectory[InputNotebook[]],
    out = NotebookCreate[],
    generateCDF, generateCDFs, exportHTML
    },
   generateCDF[nb_, name_] := Module[{sel},
     sel = SelectionMove[nb, Next, Cell];(*1*)
     Message[generateCDF::info, name];
     NotebookWrite[out, NotebookRead[nb]];(*2*)
     Export[FileNameJoin[{fpath, name}], Notebook[{NotebookRead[nb]}],
       "CDF"];(*3*)
     SelectionMove[nb, "Input", Next, CellStyle]
     ];
   generateCDF::info = "Generating CDF `1`";
   generateCDFs[nb_] := Module[{sel, curID},
     SelectionMove[nb, Before, Notebook];
     sel = NotebookFind[nb, "Input", Next, CellStyle];
     While[MatchQ[sel, _NotebookSelection],
      curID = CurrentValue[sel, CellID];
      sel = Switch[CurrentValue[sel, CellTags],
        _String, generateCDF[nb, CurrentValue[sel, CellTags]],
        _, NotebookFind[nb, "Input", Next, CellStyle]
        ];
      ];
     ];
   generateCDFs[nb];
   ],
  Appearance -> "Palette"]
 ]

I have also replaced Block with Module as I think it is more appropriate here and I've seen some subtleties with Block in dynamic code, as is explained here.

share|improve this answer

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.