Rename files sequentially - keep prefix

So I have some files to rename which they look like this :

NSIHH-75373.jpg NSIHH-75374.jpg NSIHH-75375.jpg NSIHH-75376.jpg

UAMSBTE-BK-75379.jpg UAMSBTE-BK-75380.jpg UAMSBTE-BK-75381.jpg UAMSBTE-BK-75382.jpg UAMSBTE-BK-75383.jpg

I would need them to be named like :

NSIHH-75373.jpg - NSIHH-1.jpg NSIHH-75374.jpg - NSIHH-2.jpg and so on

UAMSBTE-BK-75379.jpg - UAMSBTE-BK-1.jpg UAMSBTE-BK-75380.jpg - UAMSBTE-BK-2.jpg and so on

Any help is apreciated ! Thanks

You tagged this with AppleScript. Does that mean that you have to do this in AppleScript? Or do you just want the job done and it seems like AppleScript is a good option?

I’m asking because, if you just want the job done, you can do this using Finder (by choosing File > Rename and then selecting the Replace Text option).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I tried to make this script as scalable as possible, but your situation is pretty unique. The script will find any numbers in the file name and remove them (so necessary numbers in the main file name are at risk). Anyways, here it is:

# NOTE* Move all the files you want to edit into a separate folder.
# NOTE* Make sure they have all the same file extensions as declared below.
set file_extension to ".jpg" # <- CHANGE IF NECESSARY

tell application "Finder"
	# Gets the contents of the folder the user selected.
	set file_list to entire contents of (choose folder)
	
	# Runs this code for every item in the folder.
	repeat with file_index from 1 to (count file_list)
		# Gets the current file to be processed.
		set selected_file to (item file_index of file_list)
		
		# Calls the specified handler to remove the unnecessary numbers and ammend the new number.
		set selected_file_name to my change_numbers((name of selected_file), file_index, file_extension)
		
		# Applies the new name.
		set name of selected_file to selected_file_name
	end repeat
	return "done"
end tell

# Handler to process the file name.
on change_numbers(file_name, file_index, file_extension)
	# First pass; removes the unnecessary numbers.
	set AppleScript's text item delimiters to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
	set file_name_items to (every text item of file_name)
	set AppleScript's text item delimiters to ""
	set file_name to (file_name_items as string)
	
	# Second pass; removes the file_extension and replaces it with the version that has the new number attatched.
	set AppleScript's text item delimiters to file_extension
	set file_name_items to (every text item of file_name)
	set AppleScript's text item delimiters to ((file_index as string) & file_extension)
	set file_name to (file_name_items as string)
	set AppleScript's text item delimiters to ""
	
	# Exit handler.
	return file_name
end change_numbers

I based a lot of this on a few assumptions about your situation, so if something isn't working like how you need, I can help. (P.S., to any people more advanced in AppleScript, maybe the user, I feel like this script can be simplified, especially in the handler section at the bottom. Can someone help with that?)

Rename files sequentially - keep prefix
 
 
Q