4

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

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Physkid
  • 680
  • 5
  • 13

1 Answers1

5

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:

CellPrint[Cell[text3]]

(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.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • Am I to open a new mathematica notebook for this? – Physkid Oct 02 '16 at 01:25
  • 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! – Physkid Oct 02 '16 at 01:32
  • However I see a "debug" on the toolbar of my file. Does that mean anything? – Physkid Oct 02 '16 at 01:35
  • @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. – Alexey Popkov Oct 02 '16 at 05:58