I'm not sure how you are running the script e.g. Automator or in an Applescript. Because there are several steps involved I have just done it all in a shell script…
To use it copy & paste into a plain text file (TextWrangler is a decent free code editor with syntax highlighting - it makes life easier).
Save the file as paginator.sh
Make it 'executable' (only need to do this once). In Terminal…
chmod +x paginator.sh
Now you can run it by using…
paginator.sh
Obviously use the full path to the file in those commands (drag & drop the file to get Terminal to autofill it).
This is the script…
#!/bin/sh
# Output file
OUTFILE="${HOME}/list.csv"
# source folder with images
FILES="${HOME}/test/"
# Files for temporary storage of filename lists
FRONTLIST="${HOME}/list-front.txt"
BACKLIST="${HOME}/list-back.txt"
# list front & back into text files
find "${FILES}" \( -iname '*.png' -or -iname '*.jpg' \) -and -iname front\* -exec basename {} \; | sort -n > "${FRONTLIST}"
find "${FILES}" \( -iname '*.png' -or -iname '*.jpg' \) -and -iname back\* -exec basename {} \; | sort -n > "${BACKLIST}"
# Setup CSV with headings
echo "front,back" > "${OUTFILE}"
# Merge 2 list files with a comma
pr -m -t -s\, "${FRONTLIST}" "${BACKLIST}" >> "${OUTFILE}"
# strip pdf, jpg, png suffixes from the output file
sed -i '' -e 's/\.jpg//g' -e 's/.pdf//g' -e 's/\.png//g' "${OUTFILE}"
# remove temp files
rm "${FRONTLIST}" "${BACKLIST}"
In short it finds the front & back files (sorts then & saves that in two text files).
Then it merges them via 'pr' and removes the suffixes in one pass of the output file. I have removed the last piece of the sed (I'm unsure what it was for).
It saves into ${HOME}/list.csv Which is the users home folder
There are is room for improvement on this but it creates the csv as desired…
- The sort will place front_10.jpg before front_1.jpg (You must use leading zeros on all your files - e.g. file_01.jpg, back_01.png…)
- This does not account for nested folders! It will find files at every level! (you can add '-maxdepth 1' to stop find going into nested folders).
- The output depends on the file names & if one file is missing the order goes out of sync (you should probably search for the correct 'back_' image & a pair them up correctly with extra logic).
- The paths are 'hardcoded' (you can make it accept input so that you can pass the 'files folder' and the 'output file' name into it).
If this is all too complex you could try to re-purpose into an Automator or Applescript, but I think if you are calling repeated 'do shell script' actions it is a sign you could be using the shell itself. It can be wrapped up if you need to make it run like an application - or just give it a '.command' suffix & it should just launch Terminal & run when you double click the file.
Let me know what you think.