9
$\begingroup$

How can a notebook be saved without including the front end version?

If you open a .nb file, you'll see something like

FrontEndVersion -> "9.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (November 20, 2012)"

close to the end of the Notebook expression. I would like to simply not have this option.

Use case: Mathematica will issue a warning when opening a notebook that was created in a newer version (for example when using version 8 to open a notebook created in version 9). This applies to palettes as well. When distributing a palette (such as the SE image uploader) to people with both version 8 and version 9, I need to make sure that the palette doesn't have FrontEndVersion that indicates version 9, otherwise people with v8 get a warning. I would like to keep the flexibility to generate the palette in v9 though, and just test with v8 occasionally.

My current options are either editing out the version info manually from the .nb or generating the palette only with v8.

$\endgroup$
6
  • $\begingroup$ Isn't there an option to switch this message off? Possibly "InsufficientVersionWarning" ? $\endgroup$ Commented Dec 18, 2012 at 20:45
  • $\begingroup$ Maybe there is, but I don't want to switch it off for all people who install the palette. Anyway, until I find a good solution that I fully trust, I'll keep using 8 to generate the palette. $\endgroup$
    – Szabolcs
    Commented Dec 18, 2012 at 20:55
  • $\begingroup$ This is a very good question. By the way: I installed the latest palette on MMA8 and it worked very well, even with GraphicsGrid. $\endgroup$
    – magma
    Commented Dec 19, 2012 at 9:28
  • $\begingroup$ They could at least add 'don't show that again' for the warning dialog. I could live with this and leave it up to the user to decide. $\endgroup$
    – Kuba
    Commented Apr 12, 2017 at 9:58
  • $\begingroup$ @Kuba My current solution: If you make a package and want it to be compatible with version x.y, you should test it on x.y, therefore you should have access to x.y. So just save the notebook with version x.y. Of course, this assumes an ideal world and limitless resources (in access to Mathematica, different Mathematica versions and disk space to install them). That's not how most open source projects work. $\endgroup$
    – Szabolcs
    Commented Apr 12, 2017 at 10:01

2 Answers 2

2
$\begingroup$

Exporting the notebook-expression as the FullForm string seems to solve at least some of the problems that saving the InputForm has. Of course FullForm is quite verbose and it will also uncompress Graphics as Szabolcs has mentioned. For the purpose of supplying palette files in a version independent way this might still be good enough. Here is an example of how to do it:

filename = ToFileName[{$HomeDirectory, "Desktop"}, "tst.nb"]
nb = CreateDocument[{Button["Test", Print["hallo"]], Defer[1 + 1]}];
nbexpr = DeleteCases[NotebookGet[nb], 
   Verbatim[Rule][FrontEndVersion, _]];
Export[filename, 
  ToString[FullForm[nbexpr], PageWidth -> 78], {"Text", "String"}];
NotebookClose[nb]

It probably is worth noting that a notebookfile generated like this won't open in Player Pro or the free CDF player as it misses the correct cache information. What is somewhat surprising is that when only the version string is changed slightly (it must be at least the same length), we can achieve to create notebooks which will open with older versions of both Mathematica and Player Pro and even can at least be looked at with the CDF Player. Create a notebook:

nb = CreateDocument[{a + b, Defer[1 + 1]}];
NotebookSave[nb, filename];
NotebookClose[nb]

and string-manipulate it:

Export[filename, StringReplace[
  Import[filename, {"Text", "String"}],
  StartOfLine ~~ "FrontEndVersion->\"" ~~ $Version ~~ "\"," -> 
       "FrontEndVersion->\"6.0" <> StringTake[$Version, {4, -1}] <> "\","
  ], {"Text", "String"}]

open it with older versions of Mathematica, Player Pro or CDF-Player. None of the versions I tried seems to have a problem with a version 6.0 built in November 2012 :-). I don't know exactly why and when this works, it might well be a bug and change in future versions.

$\endgroup$
8
  • $\begingroup$ Why not tell people to do SetOptions[$FrontEndSession, MessageOptions -> {"InsufficientVersionWarning" -> False}] before opening the palette? $\endgroup$ Commented Dec 19, 2012 at 0:02
  • 1
    $\begingroup$ @RolfMertig: presumably because it is more effort and more confusing than telling them to klick OK in the dialog that pops up? I think the goal is to provide a palette that just works and does not need any of those extra actions from users... $\endgroup$ Commented Dec 19, 2012 at 14:07
  • $\begingroup$ Right. Since I don't really like the default setting anyway I like to put this into an installer. E.g. : Import@("http:" <> "//dl.dropbox.com/u/66256747/installseuploader.m") will do SetOptions[$FrontEnd, MessageOptions-> {"InsufficientVersionWarning" ->False}] $\endgroup$ Commented Dec 19, 2012 at 20:26
  • $\begingroup$ @RolfMertig: while it doesn't exactly answer the original question your idea to provide an installer is of course a good alternative solution to the underlying problem. Why don't you provide it as an answer? $\endgroup$ Commented Dec 19, 2012 at 21:00
  • 1
    $\begingroup$ @TomDeVries: is there a good reason why you don't save those notebooks in Version 8? I think that at least testing them with the version your students use would be a very good idea in the first place. Remember that you can have both versions installed and operating on the same computer, and AFAIK you can always run older versions with licenses for the newer ones... $\endgroup$ Commented May 2, 2013 at 9:37
1
$\begingroup$

Start by saving the notebook the usual way. In the following examples I'll save it to "/tmp/test.nb".

Direct approach Read in the notebook as text and change $Version into something suitable:

rawnb = Import["/tmp/test.nb", {"Text", "String"}];
Export["/tmp/test_nfev.nb", 
  StringReplace[rawnb,
    $Version -> "1996 \[LongDash] Microsoft FrontPage 97 (version 2)"],
  {"Text", "String"}];

This has the advantage that you can fool it into thinking it's made with whatever version you want, for instance make it into "8.0 for Linux x86 (64-bit) (October 10, 2011)" even when saving from v9

Limitations: If you actually want your version string in the notebook somewhere this will replace that too.

Roundabout half-working approach

Open a new notebook and run the following:

nb = NotebookGet[NotebookOpen["/tmp/test.nb"]];
Put[DeleteCases[nb, FrontEndVersion -> _], "/tmp/test.nb"]

It will give a warning about file being modified, ignore that or save to a different file, close "test.nb" without saving.

This has some limitations, for starters it just removes everything looking like FrontEndVersion->_ and doesn't care if it's not in the metadata, and it doesn't work with all notebooks.

It could be improved by making it a palette that does everything automatically.

$\endgroup$
8
  • $\begingroup$ I tried something like this, but it usually breaks the notebook. The reason is that the InputForm of e.g. a RowBox looks like this: RowBox[{"1", "+", "1"}] --> \(1+1\). The front end won't parse this in a .nb. $\endgroup$
    – Szabolcs
    Commented Dec 18, 2012 at 17:00
  • $\begingroup$ @Szabolcs I was just playing around with Notebook* looking functions, I'll dig some more $\endgroup$
    – ssch
    Commented Dec 18, 2012 at 17:01
  • $\begingroup$ Wait ... maybe I was wrong and Put behaves differently than other export functions. $\endgroup$
    – Szabolcs
    Commented Dec 18, 2012 at 17:02
  • $\begingroup$ Yes, unfortunately it does break more complex notebooks ... try e.g. with the image uploader palette. $\endgroup$
    – Szabolcs
    Commented Dec 18, 2012 at 17:07
  • $\begingroup$ @ssch: I think you could make the text replacement somewhat more robust by using e.g. StartOfLine ~~ "FrontEndVersion" ~~ WhitespaceCharacter ... ~~ "->" ~~ WhitespaceCharacter ... ~~ "\"" ~~ $Version ~~ "\"" ~~ WhitespaceCharacter ... ~~ "," ~~ WhitespaceCharacter ... ~~ EndOfLine -> "" , where you could of course replace this with another version option, too... $\endgroup$ Commented Dec 18, 2012 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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