[Tex/LaTex] Run a shell script (with parameters) from a PDF generated by LaTeX

hyperrefparametersscriptsshell-escapespacing

I want to have a link in my PDF generated by pdflatex which executes a shell script, and this part works great.

The problem I am running into is that I need the link in the PDF to be able to pass parameters to the shell script. I think has to do with escaping characters but I don't know how to resolve it.

Warnings

  • If you are not totally familiar with Unix shell scripts using the Terminal app, you may not want to run this. Not that I am aware of any dangers in running this as is, but just don't want to alarm anyone when the Termimal app launches.
  • Depending on your Preferences for Terminal you may need to close the Terminal window that launches.
  • Executing the script (by clicking on the links in the output PDF) will overwrite ~/TempFile.txt even though I have specified /tmp/TempFile.txt. Not sure why this is the case.
  • The MyScript.command file will get created in the directory where you run pdflatex, so you may need to manually delete it once done with this test case.

To run MWE:

To be able to use this MWE on Mac OS (can't comment on other platforms):

  1. pdflatex the MWE
  2. Locate the file MyScript.command in Finder and select File/Get Info and make sure it is set to open with Terminal.app. This did not seem necessary for me once I changed the file name extension from .sh to .command, but am documenting it here in case some one else has different settings or wants to use a different file extension.
  3. Then go back to the PDF generated by the MWE and try clicking on the links.

The PDF generated looks like:

enter image description here

Clicking on the first link will open up the file TempFile.txt containing:

Start of additional parameters


End of additional parameters

Clicking on the second link should produce (once it works):

Start of additional parameters
xyz
abc
End of additional parameters

Notes:

  • At this time, I am only interested in MacOS, but non-Mac solutions are welcome in case others want to do something like this, or I need to get this working on Windows at some other time.

References:

MWE

\documentclass{article}
\usepackage[colorlinks=true]{hyperref}

%\usepackage{filecontents}% Commented to prevent overwriting files

\newcommand*{\TempFile}{/tmp/TempFile.txt}%
\newcommand*{\MyExecutableScript}{MyScript.command}%
\newcommand*{\MyExecutableScriptRunTimeParameters}{abc xyz}%

\begin{filecontents*}{\MyExecutableScript}
    #!/bin/bash
    echo "Start of additional parameters"  > \TempFile
    echo $2 >> \TempFile
    echo $1 >> \TempFile
    echo "End of additional parameters"  >> \TempFile
    open \TempFile
    exit 0
\end{filecontents*}


\begin{document}
\immediate\write18{chmod +x \MyExecutableScript}%   make file executable  
      
\href{run:\MyExecutableScript}{Click to execute \MyExecutableScript}% <-- This works

\href{run:\MyExecutableScript\space \MyExecutableScriptRunTimeParameters}%
        {Click to execute \MyExecutableScript\space with parameters}% <-- This not so much :-(

\end{document}

Best Answer

According to the PDF specification for PDF 1.7 (ISO 32000-1:2008), you are out of luck. The action dictionary for Launch actions defines the following keys for options:

Win dictionary (Optional) A dictionary containing Windows-specific launch parameters (see Table 204).
Mac (undefined) (Optional) Mac OS-specific launch parameters; not yet defined.
Unix (undefined) (Optional) UNIX-specific launch parameters; not yet defined.

For Windows, the parameters can be specified after # in the launch specification of \href:

\href{run:\MyExecutableScript#\MyExecutableScriptRunTimeParameters}{...}

A clumsy workaround for the other operating systems would be to encode the parameters in the script name and the script looks for its name and decodes the arguments. An example, where I have implemented this way, can be seen in project vpe.

Related Question