Sequential batch rename putting files out of order!

I have a scripted droplet that had been working great for a while, but lately it has started taking the first file input and making it either last or some other seemingly arbitrary number! I need it to retain the correct order of the original file set while renaming them sequentially. I didn’t change anything with the original code, but OS updates have happened. Maybe the code needs to be tweaked to accommodate new OS things? Anyway, here’s the code:


set text item delimiters to "."
on open theDroppedItems
	tell application "Finder"
		set all_files to every item of theDroppedItems as list
		display dialog "New file name:" default answer ""
		set new_name to text returned of result
		repeat with index from 1 to the count of all_files
			set this_file to item index of all_files
			set {itemName, itemExtension} to {name, name extension} of this_file
			if index is less than 10 then
				set index_prefix to "00"
			else if index is greater than 9 and index is less than 100 then
				set index_prefix to "0"
			else
				set index_prefix to ""
			end if
			if itemExtension is "" then
				set file_extension to ""
			else
				set file_extension to "." & itemExtension
			end if
			set the name of this_file to index_prefix & index & "_" & new_name & file_extension as string
		end repeat
	end tell
end open


Any and all help would be appreciated!! :slight_smile:

Some updates:

I have created a version of the app where you have to manually select the files instead of performing a drag-and-drop, and it retains the correct order.

I also created a Finder service by injecting the code into an Automator file and tweaking it slightly so that it acts upon whatever files are selected instead, but it puts them out of order again! I would rather not have to manually select the files, so I would like the app to either be drag-and-drop or a Finder service.

Ok, another update.

I was able to make the Finder Service act correctly, so I’m using that now. I adjusted the input that the service receives in Automator from “No Input” to “Files and Folders” from “Finder” and it now works correctly. In the code, I changed the way Applescript builds its list by setting all_files to the selection (so it will act on the currently selected files).

I am still puzzled as to why the drag-and-drop function was causing it to be out of order, and my brain probably won’t stop trying to solve it. So any suggestions are still welcome!

Hi. Droplets work haphazardly. I’m assuming that your script ran more than once, probably due to a unique item class that dropped at a different time. Your Automator solution is sound, or you could use the droplet as a simple trigger to get Finder to do the same thing.

Thank you for the reply, Marc. It’s good to know that drag and drop may not be a reliable route for the future! I was aware that this function is now built in to Finder, but it doesn’t format the file name in a way that adheres to the way my team names files (for organization purposes), so I needed something that could be specific to the way we do things.

I looked at this script just to learn and had two questions:

  • Is there any reason for setting text item delimiters to “.”

  • Should “index” be used in the initial line of the repeat loop. I had understood that this word is reserved for other use

BTW, I think I understand the concept of the index property–for example when getting the index number of a Finder window–but don’t understand its use in this script.

Thanks

peavine is perfectly right.
When I compile your script, index is displayed in purple as every properties of objects.
Replace index by indx and compile. This time, indx is displayed as new_name. It’s a normal variable.

Below is a rebuilt version.

-- set text item delimiters to "." not used by the handler

on open theDroppedItems
	
	
	display dialog "New file name:" default answer ""
	set new_name to text returned of result
	tell application "Finder"
		set all_files to every item of theDroppedItems as list
		set indx to 0 # init our index
		repeat with this_file in all_files
			set indx to indx + 1
			set {itemName, itemExtension} to {name, name extension} of this_file
			if itemExtension > "" then set itemExtension to "." & itemExtension
			
			set the name of this_file to text -3 thru -1 of ("000" & indx) & "_" & new_name & itemExtension -- it's a string as is. (as string)
		end repeat
	end tell
end open

I’m not sure that it gives a better result because I assume that your problem may be due to an “annoying” behaviour of the drop feature.
It doesn’t pass the files as we assume but it pass them in two groups:
group 1 those whose quarantine bit is clear
group 2 those whose quarantine bit is set.

If you have both kind of files, the resulting order will not be what you assume.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 19 septembre 2018 15:59:31