A Notebook I was working on was saved as "Package" (with the extension ".m"). Upon opening this file, everything is unreadable.

How do I convert this file back to the Notebook format (with the extension ".nb")?

enter image description here

enter image description here

Check the top bar. It says Xcode instead of Mathematica.

Opening with mathematica

  • $\begingroup$ I wonder if Notebook/.{".m"->".nb"} would work? $\endgroup$ – bill s Oct 1 '16 at 14:39
  • $\begingroup$ Guys I have aded the picture of the content of the file. $\endgroup$ – Physkid Oct 1 '16 at 14:40
  • 1
    $\begingroup$ Open the file with Mathematica... $\endgroup$ – JungHwan Min Oct 1 '16 at 14:40
  • $\begingroup$ @Xavier Yes it is a mathematica file. $\endgroup$ – Physkid Oct 1 '16 at 14:40
  • $\begingroup$ @Xavier I still get gibberish. $\endgroup$ – Physkid Oct 1 '16 at 14:43

Here is how you could auto-extract separately the images and the text from your "Package" file.

First we need to uncomment the commented lines in order to be able to extract the complete Mathematica expressions:

text = Import["OSF Lab Report 1.m", "Lines"];

text2 = StringJoin[StringTrim[#, "(*" | "*)"] <> "\n" & /@ text];

Now we match complete expressions with Head Image using the regexp developed by Xavier in this post (I've simplified it for matching only balanced square braces []):

images = StringCases[text2, 
   "Image" ~~ RegularExpression["(?P<a>\\[([^\\[\\]]|(?P>a))*\\])"]];

Now you can convert them directly into images:

ToExpression /@ images

All images except the last are converted successfully. The ColorProfileData of the last image contains damaged CompressedData, hence we have to remove the ColorSpace information completely in order to obtain a valid image:

code = ToHeldExpression[images[[-1]]];
Delete[code, {1, 3}] // ReleaseHold

The text without images can be obtained using the same regexp:

text3 = StringReplace[text2, 
 "Image" ~~ RegularExpression["(?P<a>\\[([^\\[\\]]|(?P>a))*\\])"] -> ""];

Now you can make an editable Cell containing all the text with things like \[CapitalDelta]d already properly formatted:


(but things like Subscript[L, 1] you still have to format manually).

An alternative is to export it as a TXT file:

Export["OSF Lab Report 1 - Text Only.txt", text3]

The rest is up to you.

  • $\begingroup$ Am I to open a new mathematica notebook for this? $\endgroup$ – Physkid Oct 2 '16 at 1:25
  • $\begingroup$ Thank you very much.. It still leaves work for be organised but otherwise the 'word texts' are still intact. You guys are the heroes and best wishes! $\endgroup$ – Physkid Oct 2 '16 at 1:32
  • $\begingroup$ However I see a "debug" on the toolbar of my file. Does that mean anything? $\endgroup$ – Physkid Oct 2 '16 at 1:35
  • $\begingroup$ @Physkid Yes, you should create a new Notebook and manually paste fragments of text and images into it in order to re-create the original look of the Notebook you have lost. I have added some additional code at the bottom of the post which potentially can simplify your work. $\endgroup$ – Alexey Popkov Oct 2 '16 at 5:58

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.