Rename Files By Creation Date

Hi,
I would like to write a script that takes a folder of about 1000 files and
renames them all by creation date (and time). I would like the first
(oldest) file to be named: 1012.jpg and the next one 1015.jpg. In other
words, I do not want to change the extension (there will be files with different extensions) and I do want to change the
file number in increments of three.

I am wondering if this is possible and how I would do it. Thank you very much for your help.
Ken
PS: In case you are wondering what this is for: I am a professional photographer and sometimes work together with a partner. It would be nice to have all photos from a shot sorted by creation date and have the file names reflect that sort order. Some slide show programs only go by file name. That way the files would display properly.

[Warning: Cheap Plug Ahead]

Name those Files! can sort a file list by creation or modification date and then rename using a counter incremented by three (or whatever).

Jon

Thank you Jon, but I don’t see how this program could be incorporated into a script.

Sure. By the way, why are you starting at 1012?


set startNumber to 1012 -- per your request
set incrementNumber to 3

set imageFolder to choose folder -- debugging, (you want drag/drop?)

tell application "Finder"
	
	set imageFiles to every file of imageFolder as alias list
	
	set imageCount to imageFiles's length
	
	repeat with i from 1 to imageCount
		
		set imageAlias to imageFiles's item i
		
		-- sub-list-ify the alias list { { file, date }, { file, date }, etc... }
		--
		set imageFiles's item i to {imageAlias, imageAlias's creation date}
		
	end repeat
	
	-- set up the custom compare script object for sorting
	--
	set CompareAtItem's itemIndex to 2 -- sort on the creation date
	
	-- sort, (in place)
	--
	my CustomQsort(imageFiles, 1, imageCount, CompareAtItem)
	
	
	set astids to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	
	set currentNumber to startNumber
	
	repeat with i from 1 to imageCount
		
		set imageAlias to imageFiles's item i's item 1
		
		set imageExtension to "." & (get imageAlias's name)'s text item -1 -- ".jpg"
		
		set imageAlias's name to "" & currentNumber & imageExtension
		
		set currentNumber to currentNumber + incrementNumber
		
	end repeat
	
	set AppleScript's text item delimiters to astids
	
end tell


script CompareAtItem
	
	-- no error checking here... assumes item itemIndex exists
	-- and that all values at that index are comparible.
	
	property itemIndex : 1
	
	on comp(listA, listB)
		
		set a to listA's item (my itemIndex)
		set b to listB's item (my itemIndex)
		
		if (a < b) then
			return -1
		else if (a > b) then
			return 1
		else
			return 0
		end if
	end comp
end script

--	A variation on the Garvey/Knapp qsort,
--	(minus the lengthy comments).
--
on CustomQsort(a, l, r, compObj) -- compObj must have handler "comp(a,b)"
	script o
		property cutoff : 10
		property p : a
		on qsrt(l, r)
			set i to l
			set j to r
			set v to my p's item ((l + r) div 2)
			repeat while (j > i)
				set u to my p's item i
				-- repeat while (u < v)
				repeat while (compObj's comp(u, v) < 0)
					set i to i + 1
					set u to my p's item i
				end repeat
				set w to my p's item j
				-- repeat while (w > v)
				repeat while (compObj's comp(w, v) > 0)
					set j to j - 1
					set w to my p's item j
				end repeat
				if (i > j) then
				else
					set my p's item i to w
					set my p's item j to u
					set i to i + 1
					set j to j - 1
				end if
			end repeat
			if (j - l < cutoff) then
			else
				qsrt(l, j)
			end if
			if (r - i < cutoff) then
			else
				qsrt(i, r)
			end if
		end qsrt
		on isrt(l, r)
			set x to l
			set z to l + cutoff - 1
			if (z > r) then set z to r
			set v to my p's item x
			repeat with y from (x + 1) to z
				if (compObj's comp(my p's item y, v) < 0) then
					set x to y
					set v to my p's item y
				end if
			end repeat
			tell my p's item l
				set my p's item l to v
				set my p's item x to it
			end tell
			set u to my p's item (l + 1)
			repeat with i from (l + 2) to r
				set v to my p's item i
				-- if (v < u) then
				if (compObj's comp(v, u) < 0) then
					set my p's item i to u
					repeat with j from (i - 2) to l by -1
						if (compObj's comp(v, my p's item j) < 0) then
							set my p's item (j + 1) to my p's item j
						else
							set my p's item (j + 1) to v
							exit repeat
						end if
					end repeat
				else
					set u to v
				end if
			end repeat
		end isrt
	end script
	o's qsrt(l, r)
	o's isrt(l, r)
end CustomQsort

Thanks Arthur, this is amazing! It works. I had no idea this would be such a complex procedure. I thought it would only be two or three lines of code. Thank you so much. I hope you did not have to make all this up from scratch. How do you every learn stuff like this? Certainly not from Apple language guide. To answer your question: the starting number is arbitrary. I do not want my client to know how many rejects I have and don’t show them. That’s why I started with a high number and with an increment of 3. Thank you again. You did me a big favor.

Ken