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:


Inside a folder (/Volumes/Data/TeX_docs/) I've an exemple.tex file created by Scrivener that is ready for compiling with LaTeX.
I'd like to right click this file and from the Finder's Service menu choose the option "Compile TeX file"
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!)
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!