6
$\begingroup$

This question already has an answer here:

I had some work done (though it wasn't much, it was laid out nicely) in a cdf file. My computer decided to shut down on me randomly while I was working on it (some kind of error).

I can open the notebook but its all code. I don't understand it. Stuff like this:

(* Content-type: application/vnd.wolfram.cdf.text *)

(*** Wolfram CDF File ***)

(* http://www.wolfram.com/cdf *)

(* CreatedBy='Mathematica 10.0' *)
(*************************************************************************)

(*                                                                       *)

(*  The Mathematica License under which this file was created prohibits  *)

(*  restricting third parties in receipt of this file from republishing  *)

(*  or redistributing it by any means, including but not limited to      *)

(*  rights management or terms of use, without the express consent of    *)

(*  Wolfram Research, Inc. For additional information concerning CDF     *)

(*  licensing and redistribution see:                                    *)

(*                                                                       *)

(*        www.wolfram.com/cdf/adopting-cdf/licensing-options.html        *)

(*                                                                       *)

(*************************************************************************)

(*CacheID: 234*)

(* Internal cache information:

NotebookFileLineBreakTest

...

How do I turn that back into what I had before?

$\endgroup$

marked as duplicate by gwr, eyorble, MarcoB, Yves Klett, bbgodfrey Sep 17 '18 at 1:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ it's mostly comments. I don't think it can be saved. $\endgroup$ – Chen Stats Yu Dec 1 '14 at 9:33
  • $\begingroup$ I didn't paste it all. The code is massive. $\endgroup$ – Sultan of Swing Dec 1 '14 at 9:53
  • $\begingroup$ try just rename the file to anything.cdf, see if you can open it? $\endgroup$ – Chen Stats Yu Dec 1 '14 at 10:17
  • 2
    $\begingroup$ You probably already did it, but save a copy of the untouched file before trying anything on it. My experience with corrupted mma files is not good, but it should be possible to salvage user content by manually parsing the cell structures. And perhaps some gurus in here can help you recover - for instance - just the code, just the text without all that formatting overhead. $\endgroup$ – Peltio Dec 1 '14 at 10:18
  • 1
    $\begingroup$ @Johu I'd prefer not to close 92814, for it might look irrelevant afterwards. For your own post (181632), I would say that it is not a duplicate because it focuses on prevention which is, imho, another slightly different topic. Since all necessary links appear in the right side bar (under "Linked"), I think this is enough to guide future users. $\endgroup$ – Henrik Schumacher Sep 10 '18 at 19:07
7
$\begingroup$
Quiet[<<AuthorTools`]
NotebookRestore[]

Just copy and paste the above two lines in a new mathematica file. When you will execute it, a dialogue box will open. Choose the file which has been corrupted. Mathematica will automatically restore the program. Save it with some new name.

$\endgroup$
  • $\begingroup$ This actually helped save the majority of my work! Thank you. It created a file that was mostly what I had before, fixing a syntax error that I did not know how to fix by hand. $\endgroup$ – retrovius May 9 at 4:40
4
$\begingroup$

Below is a function that I use to open a cleaned out version of a notebook. The function deletes all output cells, almost all cell options, and all notebook options. I usually use this on working notebooks to clean out accumulated code shrapnel and have not attempted to use it to open a corrupted file, but you are welcome to try it and see if it helps your situation. Of course, if you are successful in opening the file, then you will need to reset any necessary cell or notebook options.

This function creates a new notebook object given the filename of the original notebook.

If the group has any suggestions for making this code more complete or robust, I welcome any input.

CleanNotebookOpen[nbfn_String] := 
 Block[{outputcelltypes, keptInputCellOptions, deleteCellOptions, nb, 
   nbnoopts, nbnoout, nbcellopts, nbobj},
  outputcelltypes = {"Output", "Message", "MSG", "Print", "Graphics"};
  keptInputCellOptions = {InitializationCell -> True};
  deleteCellOptions = {CellChangeTimes, CellOpen};
  nb = Import[nbfn];
  If[nb === $Failed, Return[$Failed]];
  (* First, manipulate the notebook as an expression *)
  (* Eliminate all notebook options *)
   nbnoopts = 
   nb /. Notebook[cells : {___}, opts___?OptionQ] :> Notebook[cells];
  (* Eliminate all output cells *)
  nbnoout = 
   DeleteCases[nbnoopts, 
    Cell[___, Alternatives @@ outputcelltypes, 
     opts___?OptionQ], \[Infinity]];
  (* Clean up cell options*)
  nbcellopts = nbnoout /. {
       (* Unevaluatable input cells get their content commented out.  
       Input cells are either BoxData (StandardForm) or String (RawForm) *)
       cell : 
         Cell[BoxData[___], "Input", ___?OptionQ, 
          Evaluatable -> False, ___?OptionQ] :> 
        MapAt[RowBox[{"(*", #1, "*)"}] &, cell, {1, 1}],
       cell : 
         Cell[_String, "Input", ___?OptionQ, 
          Evaluatable -> False, ___?OptionQ] :> 
        MapAt["(*" <> #1 <> "*)" &, cell, {1}]} /. {
     (* Now remove all options from input cells, except as noted. *)
     Cell[stuff___, "Input", opts___?OptionQ] :> 
       Cell[stuff, "Input", 
        Sequence @@ 
         Cases[Flatten[{opts}], 
          Alternatives @@ keptInputCellOptions]]} /. {
     (* Eliminate specific options from all cells. *)
     Cell[stuff___, type_String, opts___?OptionQ] :> 
      Cell[stuff, type, 
       Sequence @@ 
        FilterRules[Flatten[{opts}], Except[deleteCellOptions]]]};
  (* Create a new notebook object from the nb expression *)
  nbobj = NotebookPut[nbcellopts];
  FrontEndTokenExecute[nbobj, "DeleteGeneratedCells"];
  SelectionMove[nbobj, All, Notebook];
  FrontEndTokenExecute[nbobj, "SelectionOpenAllGroups"];
  nbobj
  ]
$\endgroup$
  • $\begingroup$ Didn't work: Syntax::sntue: Unexpected end of file (probably unfinished expression) $\endgroup$ – Jerry Guern Sep 2 '15 at 7:59
  • $\begingroup$ This solution is limited in that it assumes that the contents of the file has the structure of a valid Notebook expression. It can only be of help if the corruption is in a generated cell within the notebook, such as a faulty Dynamic expression. $\endgroup$ – Daniel W Sep 3 '15 at 12:03

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