Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to invoke Mathematica's diff functionality from the command line?

TortoiseSVN (as well as other Tortoise clients) include a script to diff notebook files in Mathematica. Diff functionality for Mathematica is implemented in the AuthorTools package (perhaps there is something better?)

The script currently works by creating a small notebook file in the temp directory, and opening it in the front end. The notebook has a big button that will do the diff and has the file names to be diffed hard coded.

A disadvantage is that the notebook with the diff code will be left in the temp directory, and won't be cleaned up. It also seems unnecessary to have an auxiliary notebook open every time we do a diff.

Is it possible to launch the diff functionality from the command line to avoid going through the temporary notebook? Or is there any other robust way to improve this process and avoid littering the temp folder with auxiliary notebooks?

Any suggestions to improve the diffing experience are welcome!

Note that since TortoiseSVN is a Windows program, I am primarily interested in Windows-based solutions.


Here's an example notebook that the script generates. I realize it's in need of cleanup, but last time I checked it worked in version 5 too (!), so I did not want to touch it unnecessarily (without visibly improving something).

Notebook[{ 
  Cell[BoxData[ButtonBox["\<\"Compare Notebooks\"\>", 
       ButtonFrame->"DialogBox", Active->True, ButtonEvaluator->Automatic,
       ButtonFunction:>(Needs["AuthorTools`"]; NotebookPut[Symbol["NotebookDiff"]["one.nb", "two.nb"]])
  ]], NotebookDefault] },
  Saveable->False, Editable->False, Selectable->False, WindowToolbars->{}, 
  WindowFrame->ModelessDialog, WindowElements->{}, 
  WindowFrameElements->CloseBox, WindowTitle->"Diff", 
  ShowCellBracket->False, WindowSize->{Fit,Fit}
]
like image 766
Szabolcs Avatar asked Nov 29 '11 15:11

Szabolcs


People also ask

How do I run a script in Terminal Mathematica?

Writing and Executing Scripts You can create a script from Mathematica with the File > New > Script menu item and execute it by typing its name in the command line (prepending ./ on Linux and Mac systems) or by double-clicking its icon in a file explorer.

How do you run a command in Mathematica?

After you have completed typing an Input, you Evaluate the Input (i.e., make Mathematica execute the mathematical command you entered) by hitting Shift and Enter (carriage return). You can evaluate an entire notebook all at once by choosing Evaluation and then Evaluate Notebook from the Kernel menu.

How do I run Mathematica on Linux?

To set up Mathematica on your Linux machine, you first need to download Mathematica and its documentation from the Wolfram User Portal, install both on your machine, then activate Mathematica or sign in using your organization's credentials. Note: You may choose to install Mathematica without its documentation.


1 Answers

Here's a simple example of producing the notebook diff using a Mathematica script.

Save the following as diff.m

Needs["AuthorTools`"]
If[Length[$ScriptCommandLine]>=3, 
    {f1, f2} = $ScriptCommandLine[[{2,3}]], 
    {f1, f2} = {"one.nb", "two.nb"}]
diff = FileNameJoin[{$TemporaryDirectory, "diff.nb"}]
Put[NotebookDiff[f1, f2], diff]
Run["Mathematica " <> diff]
DeleteFile[diff]
Exit[]

Then call it from the command line using MathematicaScript -script diff.m "one.nb" "two.nb". This works on my system (Ubuntu 11.10, Mathematica 8.0.1) and should be platform independent. If you're using a version of Mathematica older than v8, then you'd have to use MathKernel -noprompt -run < diff.m instead of MathematicaScript and the default values for {f1, f2} will be used.

like image 159
Simon Avatar answered Sep 28 '22 07:09

Simon