2
$\begingroup$

I am trying to write a wolframscript script to produce PDF versions of notebooks. I understand that to do this, I have to open the notebook, and only a frontend can open a notebook. However, depending on exactly how I try, I either get an immediate return (having taken no action) or a message like:

FrontEndObject::notavail: A front end is not available; certain operations require a front end.

I can run other scripts successfully, just none using notebooks. I can run the interpreter from the command line, just not accessing notebooks.

I have tried various approaches as described in

https://superuser.com/questions/381807/convert-mathematica-to-pdf-via-command-line -- and -- generating pdf using UseFrontEnd -- and -- UseFrontEnd is not working the same in version 10.4

These include:

  • Using JLink
  • Using UseFrontEnd
  • Using UsingFrontEnd
  • Running /Applications/Mathematica.app/Contents/MacOS/MathKernel directly
  • Running wolframscript either with -script or not
  • Having a shebang line #!/usr/bin/env wolframscript -print all or not
  • Setting $FrontEndLaunchCommand and evaluating ConnectToFrontEnd[]
  • Export or NotebookPrint

Here is the simplest case that I think should work in file bar.wls:

#!/usr/bin/env wolframscript -print all
notebook = UseFrontEnd@NotebookOpen["appendices.nb"];
UseFrontEnd@NotebookPrint[notebook, "print.pdf"];

invoked with "wolframscript bar.wls".

One interesting observation is that regardless of what I try Print[$BatchInput] always results in "False".

I am running Mathematica 11 on a MacBook.

I would appreciate insights and workarounds.

$\endgroup$
3
  • $\begingroup$ Why are you confined to performing your task with wolfram script? Why can't you do it with a notebook that manipulates other notebooks? $\endgroup$
    – m_goldberg
    Commented May 15, 2018 at 1:29
  • $\begingroup$ I certainly could run it from a notebook, but the task is to create PDFs from a large collection of notebooks. This feels like a batch process to me. I believe that what I am trying to do is supposed to be supported, so I would like to understand better how I can accomplish it. $\endgroup$ Commented May 15, 2018 at 22:02
  • $\begingroup$ Notebooks are perfectly capable of doing batch processing of other notebooks. A notebook can consist solely of code cells or even a single code cell, so can be very script-like. Code in notebook is much easier to debug that command line scripts. Further, I'm not aware that the kind of support you suggest is actually exists, but I'm not an expert in wolfram script. All I can really say is that I can't help you, but I hope someone else with the requisite expertise can an will. $\endgroup$
    – m_goldberg
    Commented May 16, 2018 at 4:51

1 Answer 1

3
$\begingroup$

Here is a straightforward solution. The key is that the filenames have to be absolute:

#!/usr/bin/env wolframscript -print all
#
# Produce a PDF version of the notebook $1 into file $2
#   Input argument must be a full path names
#

UsingFrontEnd[
  notebook = NotebookOpen[$CommandLine[[4]]];
  NotebookPrint[notebook, $CommandLine[[5]]];
  NotebookClose[notebook];
]
$\endgroup$

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.