#!/bin/ksh # convert_this_please.ksh # Listen for email Downloads and convert as required echo "" > ${HOME}/Downloads/convert_this_please.out nthan="${HOME}/Downloads/convert_this_please.out" cmdsuff="" if [ -f "/Library/TeX/Root/bin/x86_64-darwin/pdflatex" ]; then cmdsuff=" --pdf-engine=/Library/TeX/Root/bin/x86_64-darwin/pdflatex" elif [ -f "/Library/TeX/texbin/pdflatex" ]; then cmdsuff=" --pdf-engine=/Library/TeX/texbin/pdflatex" else cmdsuff="" #" --pdf-engine=pdflatex" fi while [ 0 -lt 1 ]; do find ${HOME}/Downloads -name 'convert_this_please_*.*' -mtime -1m -newer ${nthan} -type f >> ${HOME}/Downloads/convert_this_please.lst #2> /dev/null if [ ! -f "${HOME}/Downloads/convert_this_please.lst" -o -z "${HOME}/Downloads/convert_this_please.lst" ]; then nthan="${HOME}/Downloads/convert_this_please.out" else echo "" > ${HOME}/Downloads/convert_this_please.out echo "" > ${HOME}/Downloads/convert_this_please.alt nthan="${HOME}/Downloads/convert_this_please.alt" file="${HOME}/Downloads/convert_this_please.lst" # Thanks to https://www.cyberciti.biz/faq/ksh-read-file/ while IFS= read -r line do # display line or do somthing on $line if [ `echo "${line}" | tr -d ' '` != `echo "${line}"` ]; then one=`echo "${line}"` two=`echo "${line}" | sed '/ /s///g'` mv "${one}" "${two}" line="${two}" fi toext=`echo "${line}" | cut -f 4 -d '_' | cut -f 1 -d '.'` if [ ! -z "${toext}" ]; then nonext=`echo "${line}" | cut -f 1 -d '.' | tr -d '_'` ext=`echo "${line}" | cut -f 2 -d '.' | cut -f 1 -d '-' | cut -f 1 -d '_' | cut -f 1 -d ' ' | cut -f 1 -d '(' | cut -f 1 -d '0' | cut -f 1 -d '1' | cut -f 1 -d '2' | cut -f 1 -d '3' | cut -f 1 -d '4' | cut -f 1 -d '5' | cut -f 1 -d '6' | cut -f 1 -d '7' | cut -f 1 -d '8' | cut -f 1 -d '9'` if [ "`echo ${toext} | wc -c`" == "3" -o "`echo ${toext} | wc -c`" == "2" -o "`echo ${toext} | wc -c`" == "1" ]; then toext="${toext}" else if [ "${toext}" == "pdf" ]; then pandoc -f ${ext} "${line}" -t ${toext} -o ${nonext}.${toext} ${cmdsuff} > pandoc.out 2> pandoc.err else textutil -convert ${ext} "${line}" -output ${nonext}.${toext} > textutil.out 2> textutil.err fi if [ -f "${nonext}.${toext}" ]; then open ${nonext}.${toext} fi fi fi done < $file echo "" > ~/Downloads/convert_this_please.lst find ${HOME}/Downloads -name 'convert_this_please_*.*' -mtime -1m -newer ${nthan} -type f > ${HOME}/Downloads/convert_this_please.lst #2> /dev/null echo "" > ${HOME}/Downloads/convert_this_please.alt fi sleep 10 done exit