8

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.

  • @Papiro The linked doesn't work in Evince at least, but mine does. – Sean Allred Jul 24 '13 at 17:56
  • 2
    Although this is technically a duplicate, both of the solutions seem only to work on Windows. I would prefer to repopen this an get a solution on the record that works with Linux/OS X. – Alan Munn Jul 24 '13 at 20:22
  • @AlanMunn Agreed -- I'm thinking there might be a difference in how they each handle application calls. Perhaps someone can test it within Evince on Linux, since that worked on Windows? – Sean Allred Jul 24 '13 at 22:38

2 Answers2

11

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.

Sean Allred
  • 27,421
  • I just tried that and adobe reader pops up an error saying it can't find a program to run shell.bat. – user2482876 Jul 24 '13 at 17:57
  • @user2482876 Are you on Windows, or a UNIX variant (eg Mac)? If you're using UNIX, rename shell.bat to shell.sh; Windows uses the bat extension to indicate BATch scripts, where UNIX variants usually use sh to indicate SHell scripts. (Technically, the Bourne shell.) – Sean Allred Jul 24 '13 at 17:58
  • ah! yes i'm on OSX. It makes way more sense! – user2482876 Jul 24 '13 at 17:59
  • I tried again using .sh this time and it gave me the same error. Should I make an actual shell script file and use run:./shell.sh ? – user2482876 Jul 24 '13 at 18:06
  • The filecontents* environment should be writing it... check the directory to make sure it exists and, probably more importantly, ensure that it is executable. (chmod +x shell.sh) – Sean Allred Jul 24 '13 at 18:13
  • the shell.sh file is created and I made it executable, but adobe reader still says it can't find a program to open it. – user2482876 Jul 24 '13 at 18:21
  • Are @echo off and pause commands for the latex compiler? – user2482876 Jul 24 '13 at 18:24
  • @echo off and pause are batch commands; neither are truly necessary, it just makes the shell look nicer. Hmm. I'll edit my answer for you, since this is particular to UNIX stuffs. If you take a look at the linked question though, I'm not sure if Reader allows this. – Sean Allred Jul 24 '13 at 18:24
  • I also tried with Preview (the mac pdf viewer) without more luck. – user2482876 Jul 24 '13 at 18:26
  • Try that -- particularly the shebang line. – Sean Allred Jul 24 '13 at 18:28
  • I tried it without luck. I also tried to make it more like an executable, by changing .sh to .command and giving it the permissions and that didn't do it either. – user2482876 Jul 24 '13 at 19:02
  • 2
    @user2482876: It works with Skim, though (if the x bit is set). Apparently, every PDF viewer implements the run: handler differently on OS X :-( – Daniel Jul 25 '13 at 15:47
  • @Daniel: Yeah, after some research I realized that too. I'll try skim. Thanks. – user2482876 Jul 25 '13 at 15:51
  • @Daniel nice observation; I've noted the disparity in the answer. – Sean Allred Jul 25 '13 at 16:20
  • I am getting The application can’t be opened.error – alper Jan 14 '23 at 14:38
  • @alper you are unlikely to get help from a ten-year-old question; please ask a new one (and link to this old question in your new post) :-) – Sean Allred Jan 14 '23 at 21:32
  • Actually it works in Skim pdf viewer :-) – alper Jan 15 '23 at 00:13
2

Ok here's how to do it on OSX. Big thanks to everybody, especially to Sean Allred and Daniel.

Step 1

Your .tex file should follow this structure:

\begin{filecontents*}{shell.command}
#!/usr/bin/env sh
echo "This finally works!"
\end{filecontents*}

\documentclass{beamer}

\begin{document}

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

Using .command instead of .sh will make the script executable by the system.

Step 2

Go in terminal, cd into the directory and give the script the permission to run with:

chmod +x shell.command

Step 3

Use Skim to read the .pdf file because different readers seem to interpret differently the run: command.

  • I should note that Skim probably isn't the only one that works, but it has (obviously) been confirmed to do so. Evince works in Windows, so it will likely also work on OSX (and other *nix/bsd flavors). As a side note, isn't this basically the content in an existing answer? – Sean Allred Jul 25 '13 at 16:22
  • @SeanAllred It seems that the crucial distinction is that the file must have the .command or the .tool extension. See What is the difference between .command, .tool, and .sh file extensions? – Alan Munn Jul 25 '13 at 16:23
  • @AlanMunn I still don't believe that should make a difference, but I'll take your word for it until I can play with it some myself. It's weird that the extension would matter, since the default on OSX is to run sh extensions in Terminal with bash. (At least, it's been set as the default so long on my computer that I don't remember setting it. ;-) I'll have to see.) – Sean Allred Jul 25 '13 at 16:28
  • 1
    @SeanAllred The issue is that since the run: command interfaces with the GUI part of the OS (not the underlying BSD part), it needs to execute a file that the GUI part of the OS understands, and for OS X this is files named .command or .tool, i.e. double clickable shell scripts. A regular .sh file is only executable from within the Terminal, but not by double clicking in the Finder, and run: behaves as if it's in the Finder. – Alan Munn Jul 25 '13 at 16:30
  • 1
    I did try using .sh and it opened the file in a text editor (Xcode by default on my computer). – user2482876 Jul 25 '13 at 16:31
  • 2
    Just to be clear for others who might encounter this answer, the filecontents part of the answer is not required; the .command file simply needs to be in the same folder as the .pdf file, and can be created in any way. – Alan Munn Jul 25 '13 at 16:43
  • Gotcha. I must have changed it on mine, then. – Sean Allred Jul 25 '13 at 16:52
  • Only downside, it still doesn't work with Adobe Reader, which I need to use because of animations in my beamer presentation. – user2482876 Jul 25 '13 at 16:59