# Create a service using Automator to compile .tex file in Mac

I'm using Mac OS X 10.9.3, TeXLive-2013 and Scrivener to write my papers.

I'd like to use Apple's Automator in order to create a service (available from the Finder's right-click menu over a file) called "Compile TeX file" that runs a simple command using the selected file as an input.

Example:

1. Inside a folder (/Volumes/Data/TeX_docs/) I've an exemple.tex file created by Scrivener that is ready for compiling with LaTeX.
2. I'd like to right click this file and from the Finder's Service menu choose the option "Compile TeX file"
3. Once chosen, this service will execute the command `latexmk -pdf -bibtex exemple.tex` and so compile the .tex file inside the same folder, creating a .pdf file (This Terminal command is working perfectly right now, so the need to automate this action!)
4. After compiling the .tex file, a dialog box appears and asks if I want to delete all the temporary files with a `Yes` or `No` option. If `Yes` is chosen, it deletes all TeX temp files (.aux,.bbl,.bcf,.fdb_latexmk,.fls,.log,.out,.run.xml) and keep only the original exemple.tex and the new exemple.pdf file. Otherwise, it does nothing more and I have all the files in the end!

This is not the optimal solution, but it's working for now. Here is the Automator window:

The first shell script compiles the `.tex` file using the sequence `pdflatex - biber - pdflatex` and contains the following code:

``````for f in "\$@"
do
file_ext=\${f##*.}
if [ "\$file_ext" = "tex" ]
then
foldername=\$(dirname "\$f")
export foldername
filename=\$(basename "\$f")
filesimple=\${filename%.tex*}
cd \$foldername
/usr/local/texlive/2013/bin/x86_64-darwin/pdflatex "\$filename"
/usr/local/texlive/2013/bin/x86_64-darwin/biber "\$filesimple"
/usr/local/texlive/2013/bin/x86_64-darwin/pdflatex "\$filename"
else
echo \$f n\'est pas un fichier TeX
fi
done
``````

The second shell script deletes all temp files and opens the `.pdf` file. It contains the following code:

``````for f in "\$@"
do
file_ext=\${f##*.}
if [ "\$file_ext" = "tex" ]
then
foldername=\$(dirname "\$f")
filename=\$(basename "\$f")
filesimple=\${filename%.tex*}
cd \$foldername
rm \$filesimple.aux
rm \$filesimple.bbl
rm \$filesimple.bcf
rm \$filesimple.blg
rm \$filesimple.log
rm \$filesimple.run.xml
open \$filesimple.pdf
else
echo \$f n\'est pas un fichier TeX
fi
done
``````

I'm still waiting for a better solution using the `latexmk` command!

This takes the file as input and runs your latexmk command, then asks for confirmation before deleting all files with matching extensions.

``````for f in "\$@"
do
/path/to/latexmk -pdf -bibtex "\$f"
done
``````
``````find \${@%/*} -name "*.aux" -o -name "*.bbl" -o -name "*.bcf" -o -name "*.fdb_latexmk" -o -name "*.fls" -o -name "*.log" -o -name "*.out" -o -name "*.run.xml" -delete
``````
• I've done exactly that but I'm still getting an error at the end and the .tex file doesn't compile! The full command line is: `/usr/local/texlive/2013/bin/x86_64-darwin/latexmk —cp -pdf -bibtex -silent "\$f"`. The error message is just `Run Shell Script failed - 1 error`. When I run this command in Terminal, it works perfectly! May 24, 2014 at 10:13
• @LuCouto what is your PATH in the Terminal? May 24, 2014 at 19:27
• @Mark : the PATH in Terminal is `/Applications/B1FreeArchiver.app/Contents/MacOS:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin` May 24, 2014 at 21:41