Rename Aperture projects in the ISO 8601 date format

Although every Aperture 3 user organizes their projects differently, I am sure most of you have some unfiled, unnamed, and otherwise unorganized projects floating around your Library. To prevent having an unmanageable sidebar with endless scrolling, I file these projects in a folder until I find a home for them. Select a Project, Folder or Album from the sidebar and run the script. Projects named YYYY-MM-DD are created in the “Imported by Date” folder and all items of the selected project are filed according to their EXIF tag ImageDate. I prefer using the ISO 8601 date format 2011-08-27 rather than the default Aug 27, 2011, especially when working with long lists.


set yourFolder to "Imported by Date" -- Name your folder here
set {dateList, dateTest, parentList} to {{}, {}, {}}
property delimiter : "-"

tell application "Aperture"
	activate
	
	-- Wait until Aperture is finished processing other tasks
	repeat
		set taskCount to count of tasks
		if taskCount is 1 then
			display alert "Aperture is processing another task" message "Please wait for the task to complete and try again" buttons {"Try again", "Cancel"} default button {"Try again"} cancel button {"Cancel"}
		else if taskCount > 1 then
			display alert "Aperture is processing " & taskCount & " tasks" message "Please wait for the tasks to complete and try again" buttons {"Try again", "Cancel"} default button {"Try again"} cancel button {"Cancel"}
		else
			exit repeat
		end if
	end repeat
	
	-- Verify that at least one item is selected
	if selection is {} then display alert "The selection {} is empty" message "Please select ONE Project, Folder or Album from the Library tab in the sidebar and try again." buttons {"OK"} cancel button {"OK"}
	
	-- Get the selected Parent ID
	tell item 1 of (selection as list) to set theParent to parent
	set parentClass to class of theParent
	if parentClass is album then display dialog "Albums may contain images from multiple projects. Are you sure you want to move these images from their projects?"
	
	-- Get date of every image in the selected Parent
	tell theParent to set dateList to every image version's (value of EXIF tag "ImageDate")
	
	tell library 1
		-- Create your folder if it does not exist
		if not (exists folder yourFolder) then make new folder with properties {name:yourFolder}
		
		-- Assign name of every project in your folder to a list for the Create project command below
		-- (exists project isoImageDate) command is too slow to be included in the loop
		tell folder yourFolder to set parentList to name of every project
		
		repeat with aDate in my dateList
			-- Test each date to avoid processing duplicates
			set shortDate to short date string of aDate
			if dateTest does not contain shortDate then
				set end of dateTest to shortDate
				
				-- Convert the image date to YYYY-MM-DD format 
				set projectYear to year of aDate
				set projectMonth to (month of aDate as integer) as string
				if length of projectMonth is 1 then set projectMonth to "0" & projectMonth
				set projectDay to (day of aDate as integer) as string
				if length of projectDay is 1 then set projectDay to "0" & projectDay
				set isoImageDate to projectYear & delimiter & projectMonth & delimiter & projectDay as string
				
				tell folder yourFolder
					--Create project if it does not exist
					if parentList does not contain isoImageDate then make new project with properties {name:isoImageDate}
					
					-- Move the images into the project
					move (every image version of theParent whose value of EXIF tag "CaptureYear" is year of aDate and value of EXIF tag "CaptureMonthOfYear" is month of aDate as integer and value of EXIF tag "CaptureDayOfMonth" is day of aDate) to project isoImageDate
				end tell
			end if
		end repeat
		
		-- Move the initial container to the Trash if no images remain or if it is an album			
		if parentClass is album then
			delete theParent
		else if (count of image versions of theParent) is 0 then
			delete theParent
		end if
		beep
	end tell
end tell