Rename Image Files From Excel Sheet

Hi all,

Struggling here with a fairly simple task. I’ve been googling like mad, but so far nothing I’ve found seems to work.

I need to rename a folder full of jpegs sequentially from a list of names in a single column of an excel sheet with Applescript.

Any ideas would be greatly appreciated!

This is not the best solution. I’m certain that others will provide better.

This solution assumes that (1) the column of new names is in column A of the active spreadsheet [Excel must be open with the name spreadsheet the active sheet]; (2) the first cell of the column is a label so will not be used; (3) you will have to organize your files in the folder so that their sequence matches that of the rows of the spreadsheet; (4) you will be asked to select the folder containing the files to be renamed; (5) the type of file does not matter as this routine carries over the file type to the new name.

The first tell block selects the range of file names in the spreadsheet. The second tell block gets the file folder name and carries out the renaming operation.

Hope this provides some assistance.

tell application "Microsoft Excel"
	activate
	tell active sheet
		select used range
		tell used range
			set rowCount to count of rows
			select range ("A2:A" & rowCount)
		end tell
	end tell
end tell

tell application "Finder"
	activate
	set filesToRename to every item of (choose folder with prompt "Select the folder to rename files:") as list
	set theCounter to 0
	repeat with aFile in filesToRename
		set theCounter to theCounter + 1
		tell application "Microsoft Excel"
			set newName to (value of row theCounter of column 1 of selection) as text
		end tell
		set {itemName, itemExtension} to {name, name extension} of aFile
		set fileExtension to "." & itemExtension
		set newFileName to newName & fileExtension
		set the name of aFile to newFileName as string
	end repeat
end tell

Model: Mac Pro (Mid 2010)
Browser: Firefox 79.0
Operating System: macOS 10.14

Amazing! That seems to work perfectly!

Thank you!

I’ve been working to learn how to script Numbers and thought I would use the OP’s request for practice. The OP uses Excel, so I’ve included my script below just FWIW.

There is a potential issue with my script in that the Finder does not always return files in a folder in the expected order. For example, if a Finder window is set to sort by name in ascending order, three files in a folder are displayed in the following order:

Photo 1.jpg
Photo 2.jpg
Photo 10.jpg

However, the Finder in my script returns these same files as:

Photo 1.jpg
Photo 10.jpg
TextFile 2.jpg

One possible solution to this might be to have Finder sort the files in some specific order, such as by modification date. I’ve included but commented-out a line in my script to demonstrate.

set sourceFolder to choose folder
set columnNumber to 1
set startingRow to 2

tell application "Numbers" to tell table 1 of sheet 1 of document 1
	set newFileNames to value of every cell in column columnNumber whose value > ""
end tell

tell application "Finder"
	set everyFile to every file in sourceFolder
	-- set everyFile to sort (every file in sourceFolder) by modification date
	repeat with i from 1 to (count everyFile)
		set afile to item i of everyFile
		set newFileName to item (i + startingRow - 1) of newFileNames & "." & (name extension of afile)
		try
			set name of afile to newFileName
		on error errorMessage
			display alert "The file " & quote & (name of afile) & quote & " could not be renamed." message errorMessage buttons "Skip"
		end try
	end repeat
end tell