3
$\begingroup$

All,

I am looking for help to create a notebook to be executed via terminal in MAC OSX. The notebook uses the front end.

This is what I have so far

#!/Applications/Mathematica.app/Contents/MacOS/Mathematica -script

UsingFrontEnd[NotebookEvaluate["/Users/MyFolder/MyFile.nb"]]

When I run the script in snow leopard Mathematica opens a new notebook containing the above code displayed in quotes, when run in lion I get a window saying the notebook cannot be opened.

I will be very grateful to anyone who can point me in the right direction as to how to proceed.

Many thanks

$\endgroup$
3
  • $\begingroup$ I think this really is a bug. So my answer can be seen as a workaround, but I've been using it for so long that I never noticed the problem you encountered with the UsingFrontEnd approach. When I tried your example, I get a Terminal message saying that CFURLCreateWithString was passed an invalid string. $\endgroup$
    – Jens
    Commented Dec 27, 2012 at 18:23
  • $\begingroup$ I noticed that too, looks like a cocoa error. $\endgroup$
    – Christina
    Commented Dec 27, 2012 at 21:03
  • $\begingroup$ @Christina I had more success using a slightly different #! line to yours... $\endgroup$
    – cormullion
    Commented Dec 27, 2012 at 22:48

2 Answers 2

10
$\begingroup$

What I do is the following.

Save the following code as a text file in a permanent location under the name MathematicaLauncher.scpt:

tell application "System Events"
 try
    get process "Mathematica"
    on error -- Not running, launch and run
       launch application "Mathematica"
              -- May need to wait until application finishes launching
                 repeat while "Mathematica" is not in name of processes
                              delay 0.5
                                    end repeat
                                    end try
end tell
tell application "Mathematica"
 activate
 do script "notebook0=NotebookOpen[\"~/MyFile.nb\"];Clear[notebook0];NotebookClose[EvaluationNotebook[]]"
end tell

On the last line before end tell, replace the name and path of MyFile.nb with the location of your actual Mathematica notebook, of course!

Now from the Terminal, just type

osascript MathematicaLauncher.scpt

and the notebook will open. If you want to do additional things after opening the notebook, add the corresponding notebook manipulation commands after NotebookOpen[\"~/MyFile.nb\"];.

As an additional layer, you can now save the osascript terminal command in a shell script if needed - for example:

#!/bin/sh
osascript MathematicaLauncher.scpt

Save this in the same directory as the scpt file and make it executable, and you're done.

Explanation

The launcher is written in AppleScript, and the reason I do this is so that I have more flexibility of doing other things in addition to launching the notebook. What the script does is to create a temporary notebook0 from which the target notebook MyFile.nb is opened. After opening it, you can for example add additional lines to MyFile.nb before evaluating it. That can be done using SelectionMove and NotebookWrite.

In particular, you just wanted to evaluate your notebook. In that case you'd put your command NotebookEvaluate[\"~/MyFile.nb\"]; in the string following do script.

More info

As mentioned in the comments, you have to first start Mathematica normally by clicking on the Application, and disable the splash screen that appears at startup by default. It blocks execution of the script that AppleScript is trying to send it. That screen is the first thing I disable whenever I get a new version of Mathematica, so I almost forgot it exists.

Other than that, I've been using this method to launch and manipulate notebooks almost every day without problems, across updates to both Mathematica and OS X. I actually prefer to save the AppleScript not as a shell script but as an Application using AppleScript Editor, because then I can put its icon in the dock and launch the process with a mouse click. So instead of saving the script at the top in a text file, you could paste it into AppleScript Editor and go from there.

$\endgroup$
5
  • $\begingroup$ Many thanks for the answer, it never occurred to me to write an AppleScript. I'm having a little trouble however, I get an error error, '"Mathematica got an error: Connection is invalid." number -609', and Mathematica quits. Any ideas? $\endgroup$
    – Christina
    Commented Dec 27, 2012 at 20:55
  • 1
    $\begingroup$ Yes, that happens if Mathematica tries to show the startup-screen - the small modeless window with the various "What do you want to do" options. It seems to wait for user input and prevents the script from being executed. For this method to work, you have to disable that screen by un-checking the box on the lower right of the start-up screen. $\endgroup$
    – Jens
    Commented Dec 27, 2012 at 21:38
  • $\begingroup$ Superb! Works perfectly. Many thanks. $\endgroup$
    – Christina
    Commented Dec 27, 2012 at 21:40
  • $\begingroup$ Great - I just added the bug tag to your question because I think your original approach shouldn't have produced an error. $\endgroup$
    – Jens
    Commented Dec 27, 2012 at 21:57
  • $\begingroup$ Does any one know if this still works for the current version Mathematica 12.2 on OSX Majove and upward? $\endgroup$
    – boobami
    Commented Jun 12, 2021 at 9:49
5
$\begingroup$

It works OK for me here on Mac OS X Mountain Lion, Mathematica 9, given a few minor changes:

Mathematica Script saved as math-script.math:

#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script

Print["loading"]
UsingFrontEnd[NotebookEvaluate["/tmp/test-notebook.nb"]]
Print["quitting"]
Quit[]

Mathematica Notebook saved as /tmp/test-notebook.nb

p = Plot[{Sin[x]}, {x, 0, 2 Pi},
       Epilog -> {Text[DateList[], {5, 1}]},
       PlotLegends -> "Expressions"]
Export["/tmp/plot.png", p];

Running the script:

 $ date
 Thu 27 Dec 2012 22:34:57 GMT
 $ ./math-script.math 
 "loading"
 "quitting"
 $ 

The result:

simple plot

Perhaps the contents of your notebook are causing problems...

$\endgroup$
6
  • $\begingroup$ I can only test it on version 8 under OS X Lion currently, and there the script hangs, even though two Mathematica kernels start running. It prints only the "loading" part, and no other output (neither in terminal or Mathematica). Anyway, +1 just for the prospect that the future will be brighter... $\endgroup$
    – Jens
    Commented Dec 27, 2012 at 23:08
  • $\begingroup$ And the hanging is not due to any error in the test-notebook.nb (I removed the legend for compatibility)... $\endgroup$
    – Jens
    Commented Dec 27, 2012 at 23:16
  • $\begingroup$ Update: with version 9 of Mathematica, this does work under OS X Lion. $\endgroup$
    – Jens
    Commented Dec 28, 2012 at 1:20
  • $\begingroup$ @Jens - nice to know they fixed a bug! :) $\endgroup$
    – cormullion
    Commented Dec 28, 2012 at 8:42
  • $\begingroup$ @Jens--to explain further: the Mathematica executable is the FrontEnd executable; it doesn't read from standard input and is not scriptable. You wanted either MathKernel or (better yet) MathematicaScript, as expalined here. $\endgroup$ Commented Dec 31, 2012 at 7:24

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.