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 theTermimal
app launches. - Depending on your
Preferences
forTerminal
you may need to close theTerminal
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 runpdflatex
, 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):
pdflatex
the MWE- Locate the file
MyScript.command
in Finder and selectFile/Get Info
and make sure it is set to open withTerminal.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. - Then go back to the PDF generated by the MWE and try clicking on the links.
The PDF generated looks like:
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:For Windows, the parameters can be specified after
#
in the launch specification of\href
: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
.