Automatic printing of multiple Mathematica files

Roeland Merks, University of Amsterdam, 2001

In the lab course on simulation and modelling I am assisting, the students have to turn in their work electronically as a Mathematica notebook. The reports are then printed and corrected with hand-written remarks.

This notebook will help you with printing all the Mathematica notebooks in a directory. It converts all Mathematica notebooks in a directory to PostScript files.

The following function prints out all the files in the list "files". The directory of these files should be set in "dir"

\(\(\(PrintAllNotebooks[dir_, files_] := 
        Module[{report}, \(Table[
 (*\ 
                Open\ a\ notebook\ *) 
report = 
                NotebookOpen[
                  ToFileName[dir, files[\([i]\)]]]; 
 (*\ 
                Open\ all\ cells\ *) 
FrontEndExecute[{FrontEndToken[
                    report, "SelectAll"]}]; 
FrontEndExecute[{FrontEndToken[
                    report, "SelectionOpenAllGroups"]}]; 
 (*\ 
                Print\ to\ a\ PostScript\ file\ *) 
NotebookPrint[report, 
                ToFileName[dir, 
                  files[\([i]\)] <> ".ps"]]; 
 (*\ 
                Close\ the\ notebook\ *) 
NotebookClose[
                report];
, {i, 1, 
                Length[files]}];\)];\)\(
\)\)\)

Below, set the directory where mathematica can find your notebooks. Do this in the following format.

reportdir = 
        ToFileName[{$HomeDirectory, "tmp", "nakijk"}];

If the files are in the directory from which you started Mathematica, evaluate the following cell.

reportdir = {$InitialDirectory};

Evaluate the following cell to get a list of the files

SetDirectory[reportdir]; reportfiles = FileNames["*.nb"]

You can off course also enter the filenames manually.

Please evaluate the following cell to check whether this list indeed contains only and all the files you want to print.

reportfiles

By evaluating the following cell, each notebooks in the list is opened. Then, all subgroups are opened and the files is printed to a PostScript file called "notebook_name".ps. Then, the file is closed and the next file is converted into PostScript.

PrintAllNotebooks[reportdir, reportfiles]