Export iPhoto events into appropriate directory structure

Nov 11, '08 07:30:00AM

Contributed by: ThomPatterson

A common gripe with iPhoto is that it cannot create directory structures for events, albums, etc. during export. However, if you export the photos with meaningful filenames, you can easily post-process them and create the intended directory structure. This can be done by selecting all your events and doing a Batch Change to set the Title to Event Name. Then export your photos using the Title as the File Name. Copy the following code into a pure text editor, save it somewhere on your computer, and make it executable (chmod a+x filename in Terminal).

#	First, in iPhoto select all events and do Photos > Batch Change
#	Set Title to Event Name
#	With all events highlighted, export events a folder somewhere, for filename use title
#	Photos will be exported with filename of "event - number.jpg" (ex: Animals - 0004.jpg)
#	This script will create the appropriate directory structure based on those filenames
#	Set the photo_dir variable below to the full path of the directory

photo_dir=/Users/Thom/Desktop/iPhotoExport

cd $photo_dir

for filename in *
do

#takes the string preceding the dash, trims leading and trailing whitespace, then removes all non web-friendly characters
dir=`echo $filename | cut -d- -f1 | sed 's/^[ ]//' | sed 's/[ ]$//' | sed 's/[^A-Za-z0-9 _]//g'`

#takes the string following the dash, trims leading and trailing whitespace
file=`echo $filename | cut -d- -f2 | sed 's/^[ ]//' | sed 's/[ ]$//'`

#if directory doesn't yet exist, create it
if ! test -d "${dir}"
then
mkdir "${dir}"
fi

#move file to the appropriate directory and rename it to numeric filename
mv "${filename}" "${dir}/${file}"

done
Set the photo_dir variable to the path of your iPhoto Export. Executing the script will create the directories and move the photos into them. I use this to batch export all of my photos for easy upload to my web server.

[robg adds: I haven't tested this one.]

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20081108132735425