10 Replies Latest reply: Jul 22, 2014 6:26 PM by kevnm67
kevnm67 Level 1 Level 1 (0 points)

I'm trying to to grab images or PDF's in a folder, add those containing "front" into the 1st column of a CSV, those containing "back" to the 2nd column and then zip/archive it upon completion.  So far I'm using the following script, which add's all PDF/png files to the CSV in a single column.


How do I create column headers and add files containing "front" to the first column and "back" to the second?


Thanks in advance for the help.


do shell script "find ~/Documents/csvConvert/pngFiles \\( -iname '*.png' -or -iname '*.jpg' \\) -exec basename {} \\; | sed -e 's/.pdf$//' -e 's/.jpg$//' -e 's/^.*$/\"&\"/' > $HOME/Documents/csvConvert/Groups/PDFfiles.csv"

Mac OS X (10.5.8)
  • Drew Reece Level 5 Level 5 (6,145 points)

    For the headers…

    Modify the > to a >>

    That means the data will be appended to the file.


    So the first step is to create a new file with the header…

    do shell script "echo front,back" > $HOME/Documents/csvConvert/Groups/PDFfiles.csv"
    do shell script "find ~/Documents/csvConvert/pngFiles \\( -iname '*.png' -or -iname '*.jpg' \\) -exec basename {} \\; | sed -e 's/.pdf$//' -e 's/.jpg$//' -e 's/^.*$/\"&\"/' >> $HOME/Documents/csvConvert/Groups/PDFfiles.csv"


    Then your current script appends the other output.


    For the data columns

    I'm unclear if sort order is important.

    e.g. Does 'front1.png' always need to pair up with 'back1.png'? If it matters you would need to ensure correct file naming. How about if a file is missing?

    Or if there is a png & a jpg called front2.(jpg/png)?


    It may make sense to get the front & back as separate lists & then merge them, but if sort order is important that gets difficult. .

  • kevnm67 Level 1 Level 1 (0 points)

    Thanks for the help Drew!

    The file names do need to be matched → front_1 to back_1, for example

  • Drew Reece Level 5 Level 5 (6,145 points)

    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…



    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…


    # Output file
    # source folder with images
    # Files for temporary storage of filename lists
    # 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…

    1. 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…)
    2. 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).
    3. 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).
    4. 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.

  • kevnm67 Level 1 Level 1 (0 points)

    Thanks Drew.  I tried running it as you suggested and directly from Bbedit but get the same message:

    "$ /var/folders/x8/sh64vx155k50hh9326b41smr0000gn/T/Cleanup\ At\ Startup/paginator-427726936.596.sh.command ; exit;

    find: /Users/kevinMacPro/test/: No such file or directory

    find: /Users/kevinMacPro/test/: No such file or directory


    Ive tried creating a "test" folder as well.  Any thoughts?





    it's creating the list.csv despite the log from terminal but only contains the headers.  Also, just moved the "test" folder to the home directory... I had it on my desktop. But the created csv is still the same. Here is terminal's log:

    /var/folders/x8/sh64vx155k50hh9326b41smr0000gn/T/Cleanup\ At\ Startup/paginator-427727269.305.sh.command ; exit;


  • Drew Reece Level 5 Level 5 (6,145 points)

    The info from BBEdit doesn't really help.

    Drop the script into Terminal to run it.



    The line …


    …defines where the folder of 'front_1.jpg, back_2.png, …' images are kept. If it is empty you won't get any files to be written into the output.

    Place your images into that folder (or create a copy of them) & re-run the script.

  • kevnm67 Level 1 Level 1 (0 points)

    Thanks for the explanation. I modified it a little to handle PDF's and got it working.  How can I modify it to look within the file names for "front" or "back" to account for variations?  ex: if I name a file Anatomy_Front_1 it doesn't seem to find the file.

    Thanks Drew!

  • Drew Reece Level 5 Level 5 (6,145 points)

    Change the find argument to find *front* – that will match front inside other words.

    e.g. within the find command…

    -iname \*front\*

    Just be careful not to name files 'front to back.jpg'

  • kevnm67 Level 1 Level 1 (0 points)

    Thanks Drew!


    ha, I won't name them "front to back"


    The only remaining part is zipping the folder. Any ideas?  Greatly appreciate the help, Drew.

  • Drew Reece Level 5 Level 5 (6,145 points)

    zip is a built in command…

    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/ man1/zip.1.html

    See the examples near the bottom of that page.


    zip -r foo foobar


    Will zip a folder called foobar into an archive called foo.zip

  • kevnm67 Level 1 Level 1 (0 points)

    Thanks Drew! i greatly appreciate your help.