[Tex/LaTex] embed a terminal command in a beamer presentation

beamermac

I know this may be extravagant, but I was wondering if it was possible to embed a terminal command in a beamer pdf that can be launch by clicking on it. My idea is that during my presentation, I could just click on a button displayed on my slide to launch a program. This way, I don't have to quit the presentation and open a terminal window.

I'm using mac OSX and I need the view the pdf with Adobe Reader. Is there a way to do it without using php forwarding like in here?

Edit:

I tried this code:

\begin{filecontents*}{shell.command}
#!/usr/bin/env sh
echo "Hello, World!"
\end{filecontents*}

\documentclass{beamer}

\begin{document}

\begin{frame}
    \href{run:shell.command}{here}
\end{frame}
\end{document}

And Adobe Reader says it can't find an app to open the file. I also tried opening the file with Safari and it shows the script file, it doesn't run it. This is annoying because if I click on the shell.command file through the Finder, it simply runs it, as it should.

Best Answer

Doing this in combination with listings gives you a pretty nice solution:

\documentclass{article}
\usepackage{filecontents,hyperref,listings}

\begin{filecontents*}{script.bat}
@echo off
echo "Hello, World!"
pause
\end{filecontents*}

\begin{document}
\lstinputlisting[language=csh,float,caption={A Windows batch file}]{script.bat}

Click \href{run:script.bat}{here} to run the script.
\end{document}

output

This should also work with Beamer.


Edit

If you're working with a UNIX system, note that bat is Windows-only, since Windows doesn't use a 'real' shell. With UNIX, give it a .sh extension (if you want to give it one at all). You might also need to do a few more things:

  • Make sure the file is executable: run chmod +x myscript.sh
  • Give the file a shebang line:

script.sh:

#!/usr/bin/env sh
echo "Oh, the wonderful possibilities of UNIX shells."

Please note that, as of now, this solution may not always work on Windows.

Note also that the handling of run: protocols can vary from viewer to viewer. Some may not support it at all due to security risks, but it has been confirmed that Evince and Skim support it.