Why won't this process the files in order?

Below is my code. I think it is right. It does everything it is supposed to. It just doesn’t do it in the correct order.

I have a folder full of pictures (screenshots converted to TIFs) that need to be renamed using a convention of a 3-letter prefix, followed by a 4-digit number, followed by a “type” code, and finally the ‘.tif’ ending. The pics are named “Picture 001, Picture 002,…Picture 111”. All 111 pics in the order they need to be in. However, the renamed pics won’t be starting at abc0001G.tif. They may need to start at, say, abc0085G.tif because of previous pics in the series.

Problem is that when I run this code on a set of pics (or any files) it does not rename the files starting with the first in the folder. I REALLY NEED for it to start with the first file in the folder, then do the second, then the third, and so on until all 111 are renamed consecutively.

Any and all help appreciated. I know this board has not been getting much traffic lately, but I do know that some of you are freaking geniuses at this stuff. Please share the wealth of that genius for a simple applescripter-wannabe.

CODE START

set theFolder to (choose folder with prompt “Please select the folder containing the files to rename.”)
set theIdentifierCode to choose from list {“G”, “C”, “L”, “K”} with prompt “Please select the indentifer code.”

set the3letterPrefix to text returned of (display dialog “Please enter in a 3 letter prefix.” default answer “xxx” buttons {“Cancel”, “Enter”} ¬
default button “Enter”) --here we will ask the user for the 3 letter character code.

if length of the3letterPrefix is greater than 3 then --if more than 3 characters were entered, such as “xyza” then…
set the3letterPrefix to (characters 1 through 3 of the3letterPrefix) as string --we will just get the first 3 characters.

else

if length of the3letterPrefix is less than 3 then -- but if less than 3 characters were entered, such as "xy" then...       
	set the3letterPrefix to characters of the3letterPrefix --here we will temporarily turn "xy" into {"x", "y"}       
	repeat (3 - (length of the3letterPrefix)) times --3 minus the number of characters we have (2 in this case ) is 1 so...           
		set the3letterPrefix to the3letterPrefix & "x" --we will add 1 (or the appropriate ammount of "x"s) to the 3 letter prefix       
	end repeat
end if

end if

set the3letterPrefix to the3letterPrefix as string --we will change {“x”, “y”, “x”} into “xyx”

set StartNum to display dialog “Please enter the starting number if other than 1.” default answer “1”

set theStartNum to text returned of StartNum
set theStartNum to theStartNum as integer

tell application “Finder”

if theStartNum > 1 then
	set currentNumber to (theStartNum - 1)
else
	set currentNumber to 0
end if

set miscFileCount to 0
set theReturned to {}

repeat with aFile in theFolder
	set currentNumber to currentNumber + 1
	set theName to the3letterPrefix & (my insertLeadingZeroes(currentNumber, 4)) & theIdentifierCode & ".tif"
	set theName to theName as string
	set name of file (aFile as alias) to theName
end repeat

end tell

on insertLeadingZeroes(thestring, numb)
set thestring to thestring as string
repeat until length of thestring is numb
set thestring to “0” & thestring
end repeat
return thestring
end insertLeadingZeroes


END OF CODE

Thank you,

Jmax

Ok, I figured it out.

Well…, not really. I still don’t know WHY it won’t do the files in order. But I figured out how to MAKE it do the files in order. I added the following line to the code:

set aFile to the first item on theFolder

I put it right before the repeat statement. I still don’t know why it doesn’t do it that way automatically. If someone has an idea I would love to hear it.

jmax

I’ve never needed to use it but I think you might need to explore the Finder’s sort command (see Finder’s AppleScript dictionary). This might help to overcome the difference between what you see and what you get.

Jmax:

Take a gander at Sal’s “Number Items in a Folder” script. It displays a dialog that asks the user the preferred sort order before numbering the items.

Probably adaptable to your situation.

HTH.