Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Jens Dec 27 '12 at 18:23
    
I noticed that too, looks like a cocoa error. –  Christina Dec 27 '12 at 21:03
    
@Christina I had more success using a slightly different #! line to yours... –  cormullion Dec 27 '12 at 22:48
add comment

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
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? –  Christina Dec 27 '12 at 20:55
1  
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. –  Jens Dec 27 '12 at 21:38
    
Superb! Works perfectly. Many thanks. –  Christina Dec 27 '12 at 21:40
    
Great - I just added the bug tag to your question because I think your original approach shouldn't have produced an error. –  Jens Dec 27 '12 at 21:57
add comment

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

share|improve this answer
    
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... –  Jens Dec 27 '12 at 23:08
    
And the hanging is not due to any error in the test-notebook.nb (I removed the legend for compatibility)... –  Jens Dec 27 '12 at 23:16
    
Update: with version 9 of Mathematica, this does work under OS X Lion. –  Jens Dec 28 '12 at 1:20
    
@Jens - nice to know they fixed a bug! :) –  cormullion Dec 28 '12 at 8:42
    
@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. –  Itai Seggev Dec 31 '12 at 7:24
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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