Aperture Export Script

I’m trying to go from Aperture back to iPhoto, in prep for buying the new iPhoto. I built a little script that should bypass the problem that Aperture crashes if it tries to export over 200 photos at a time. This is a VERY important script, I can’t afford even one photo missed amongst 31,000, so I was hoping the uber-skilled scripters around here would look it over and help me out.

I tested it, and the first batch had 99 images, not the 100 I was expecting. What gives?


tell application "Finder"
	if not (exists alias "Fawkes:Pictures:Aperture Complete Export:") then
		make new folder at "Fawkes:Pictures:" with properties {name:"Aperture Complete Export"}
		set fileDest to ("Fawkes:Pictures:Aperture Complete Export:" as alias)
	else
		set fileDest to ("Fawkes:Pictures:Aperture Complete Export:" as alias)
	end if
end tell
set successfulExportList to {}
tell application "Aperture"
	set allProjects to every project --Get every project
	repeat with aPro in allProjects --Go through one project at a time
		--set aPro to project aPro
		set allImages to every image version of aPro -- get all of the project's image versions
		set icount to count of allImages -- The number of image versions in the variable
		if icount ≠ 0 then -- Check if there are images in the project, if there aren't it skips to the end then moves onto the next project
			if icount ≤ 100 then -- If there are 100 or less images
				my apexport(allImages) -- This exports all the images in the group, there are 100 or less so it won't crash aperture
			else if icount ≥ 101 then -- If there are 101 images or more the repeat loops starts
				repeat
					set batchex to ((items 1 thru 100 of allImages) as list) -- This capture the first 100 images in the variable
					my apexport(batchex) -- Then exports them
					set allImages to items 101 thru -1 of allImages
					set icount to count of allImages
					if icount ≤ 100 then
						my apexport(allImages)
						exit repeat
					end if
				end repeat
			end if
		end if
		-- Write the finished project to file
		--set end of fileLog to (name of aPro as rich text)
		set successfulExportList to successfulExportList & (name of aPro)
		write_to_file((name of aPro), "Europa:Users:Jamie:Desktop:TotalList.txt") of me
	end repeat
end tell
--write_to_file(fileLog, "Fawkes:Export Log.txt")
set unsuccessfulList to {}

repeat with i in successfulExportList
	if allProjects does not contain i then
		set unsuccessfulList to unsuccessfulList & i
	end if
end repeat

set AppleScript's text item delimiters to "
"
set unsuccessfulList to unsuccessfulList as text
set AppleScript's text item delimiters to ""
try
	write_to_file(unsuccessfulList, "Europa:Users:Jamie:Desktop:FailureList.txt")
end try




-- Subroutines --

on apexport(image_versions)
	tell application "Aperture"
		with timeout of (60 * 60 * 16) seconds
			export (image_versions as list) naming files with file naming policy "CompEx" naming folders with folder naming policy "Project Name" to ("Fawkes:Pictures:Aperture Complete Export:" as alias) metadata embedded
		end timeout
	end tell
end apexport

on write_to_file(this_data, target_file)
	try
		set the target_file to the target_file as string
		set the open_target_file to open for access file target_file with write permission
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file