chauncey-garrett
4/27/2014 - 5:24 AM

Aperture Rename Dates.applescript

tell application "Aperture"
  set mons to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
	set digits to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	
	set ps to projects
	repeat with p in ps
		set nameStr to name of p
		
		set yearNum to 0
		set monthNum to 0
		set dayNum to 0
		
		set monthStr to (characters 1 thru 3) of nameStr as rich text
		repeat with i from 1 to the count of mons
			if item i of mons is monthStr then
				set monthNum to i
			end if
		end repeat
		
		try
			set yearStr to (characters 9 thru 12) of nameStr as rich text
			set yearNum to yearStr as number
			set dayStr to (characters 5 thru 6) of nameStr as rich text
			set dayNum to dayStr as number
		end try
		if monthNum > 0 and monthNum < 13 and dayNum > 0 and dayNum < 32 and yearNum > 1900 then
			set len to (count of nameStr)
			if len > 12 then
				set r to (characters 13 thru len) of nameStr as rich text
			else
				set r to ""
			end if
			set newName to yearStr & "-"
			if monthNum < 10 then
				set newName to newName & "0"
			end if
			set newName to newName & (monthNum as rich text) & "-" & dayStr & r
			log nameStr & " => " & newName
			set name of p to newName
		end if
	end repeat
end tell