Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

script to create 2 column CSV and zip upon completion

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)

Posted on Jul 19, 2014 10:10 AM

Reply
10 replies

Jul 19, 2014 10:34 AM in response to kevnm67

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. .

Jul 20, 2014 3:35 PM in response to kevnm67

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…

  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.

Jul 22, 2014 6:10 AM in response to Drew Reece

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

logout"

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


Thanks!


EDIT:

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;

logout

Jul 22, 2014 7:35 AM in response to kevnm67

The info from BBEdit doesn't really help.

Drop the script into Terminal to run it.



The line …

FILES="${HOME}/test/"

…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.

script to create 2 column CSV and zip upon completion

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.