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
Terminalapp, 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 theTermimalapp launches. - Depending on your
PreferencesforTerminalyou may need to close theTerminalwindow that launches. - Executing the script (by clicking on the links in the output PDF) will overwrite
~/TempFile.txteven though I have specified/tmp/TempFile.txt. Not sure why this is the case. - The
MyScript.commandfile 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):
pdflatexthe MWE- Locate the file
MyScript.commandin Finder and selectFile/Get Infoand make sure it is set to open withTerminal.app. This did not seem necessary for me once I changed the file name extension from.shto.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}