Renaming image files based on an array?

Every few months I have a bunch of files to rename. I’m thinking of putting the orignal name, new name in a text file to use as an external array that can be called from Applescript such as

G310636072004.jpg,51641a.jpg
G310635072004.jpg,5140y.jpg
G310634072004.jpg,51640m.jpg

Can Applescript read in the variables from the file? Then for each line find the file named same as $1 and rename it to $2?

Thanks,
DAN

Yes. It can read files, loop over lists of values, and rename files. Was there a particular part of that you needed help with?

Hey Krioni,

I’m not sure how to read in a text file to AppleScript then assign “oldname, newname” pairs to variables such as $1, $2.

I’m familiar with finding files, creating loops, and overwritting filenames in Applescript okay though.

Thanks,
DAN

Here’s some example code:


(*
 if the file contains return-delimited lines, eahc of which is:
OLDNAME and a tab and NEWNAME

parseChars() is a handler I use a lot to do separate text into lists.
*)
property nameChangeList: "Drive:folder:namechangelist" -- file that holds your name changes
property folderPath: "Drive:folderWithFiles:" -- folder containing files to be renamed

on run
	set fileChangeBlock to read alias nameChangeList

	set parsedLines to parseChars(fileChangeBlock)
	
	repeat with oneParsedLine in parsedLines
		set {oldName, newName} to parseChars(oneParsedLine, tab)
		
		renameFile(folderPath, oldName, newName)
		-- where renameFile is your handler that does the renaming		
	end repeat
	
end run




on parseChars(thisText, parseString)
	-- version 1.2
	set oldDelims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to the {parseString as string}
		set the parsedList to every text item of thisText
		set AppleScript's text item delimiters to oldDelims
		return parsedList
	on error errMsg number errNum
		try
			set AppleScript's text item delimiters to oldDelims
		end try
		error "ERROR: parseChars() handler: " & errMsg number errNum
	end try
end parseChars



Thanks Krioni !

I’ll post my script once I get it all working.

DAN

I noticed that “renameFile” was colored as a command in my script editor. Because it’s a command in the Jon’s Commands OSAX.

renameFile  anything  -- A file or folder as file, alias or string.
	to  string  -- The new name (not a pathname).

Not a problem. You can adjust the naming line(s) accordingly:

	set origFile to (folderPath & oldName)
	renameFile origFile to newName

Or use a slightly different name for the renameFile() function, and your own naming routine. Just thought I’d mention it.