Rename files from .csv list (I know it's been asked and answered!)

Hello to the community from a first-time poster! It’s been a long time since I did AppleScript stuff (I was very dependent on them back in the Panther/Leopard/Tiger days on G3 and G4 macs) but I’m returning to the fold. It looks like the basic bones of AppleScript are unchanged from those days — why mess with something that works so well? — and I’m very impressed with Automator (which has been on every Mac I’ve used but which I’ve never launched).

As is recommended in the posting guide here, I searched for the task I’m trying to accomplish, which has been addressed many times. Based on some scripts I found here, I tried methods using Excel files, but they didn’t work. I suspect the problem may have something to do with the interrelationship between AppleScript and VisualBasic for Applications — the scripts I found here were written before Office for Mac re-did its VB/AppleScript connections (I think) — but I couldn’t get any of what I found here to work.

Anyway here’s the scenario:

  1. A folder full of files (of the same type and naming scheme)

  2. A .csv text file wherein each line is the existing filename, a comma or semicolon, and the desired file name

  3. The need to do this more than once (I don’t need a “watched folder” or a “Folder Action” but I would like an AppleScript that prompts me for the folder and the text file and then does the renaming on the spot, in a repeatable fashion).

  4. The ORDER of files (either in the folder or in the .csv file is not important — what’s important is that each file is renamed according to the text file, which matches up old to new names.

I say “existing .csv text file” but I can easily turn this into an Excel worksheet or a Word table or a MySQL table or anything else (for a while I was using an .html file and a Firefox plugin that allowed re-naming of downloaded files to do it).

Can anyone help? Thanks in advance for any advice or assistance (or script fragments) anyone can provide. I stress that I did try to find a solution here, before posting this.

Thanks again,

Model: MacPro 14" M1 Max
AppleScript: 2.8
Browser: Firefox 96.0
Operating System: macOS 12

Jordan. Welcome to the forum.

I’ve included my suggestion below. The text file that contains the old and new file names uses a comma to separate the file names. If I misunderstood anything about your requirements just post back.

set sourceFolder to (choose folder) as text
set fileRenamingData to (choose file) as text
set fileRenamingData to paragraphs of (read file fileRenamingData)

set ATID to AppleScript's text item delimiters -- save existing text item delimiters
set AppleScript's text item delimiters to {","} -- set text item delimiters to a comma

tell application "Finder"
	repeat with i from 1 to (count fileRenamingData)
		set theData to item i of fileRenamingData
		if theData > "" then -- skip blank lines
			set sourceFile to sourceFolder & text item 1 of theData
			if (exists file sourceFile) then
					set name of file sourceFile to text item 2 of theData
				on error
					display alert "A file with the name " & quote & (text item 2 of theData) & quote & " already exists and will be skipped"
				end try
			end if
		end if
	end repeat
end tell

set AppleScript's text item delimiters to ATID -- reset text item delimiters to those saved

Thank you very much for responding, and thanks for your help with the script!

I tried it. At first I got an error that read

Can’t make text item 1 of “” into type Unicode text.

I assumed that this was because the .csv list was created in Microsoft Word and saved as a .txt file, with MacOS encoding. So I saved it again (from Word) and this time specified Unicode (UTF-16) encoding.

Then when I tried again, I got a little “Running…” indicator in the base of the Script Editor window and then nothing; no error messages, and no files renamed.

I’m sorry I can’t provide more information! I can’t pinpoint what’s going wrong because I don’t fully understand the syntax being used. (I’m not adverse to learning — that’s why I’m here — but I need to solve this problem and be able to rename these files).

Thanks again in advance for anyone’s help and/or advice.

Model: MacPro 14" M1 Max
AppleScript: 2.8
Browser: Firefox 96.0
Operating System: macOS 12

Jordan. There was an error in my script, which I’ve now fixed. When testing the revised script, just for now, could you make the csv file with TextEdit. The following is the contents of the csv file I used in my testing.

I’m sorry — I’m confused by your edit. It looks like you changed the line about delimiters to read

set AppleScript’s text item delimiters to {“,”}

but at the bottom of the script it reads

set AppleScript’s text item delimiters to ATID

What’s the final, corrected text of the script supposed to read?

Again, apologies for my ignorance and thanks for your help and patience.

Model: MacPro 14" M1 Max
AppleScript: 2.8
Browser: Firefox 96.0
Operating System: macOS 12

I retested my script in post 2 and it worked as expected.

I did not change the code that deals with text item delimiters, which are necessary to do the following:

  1. Save the existing text item delimiters to ATID
  2. Set text item delimiter to a comma in order to parse the lines of the text file with old and new file names
  3. At the end of the script, reset the text item delimiters to the text item delimiters that were saved in ATID

My edit involved changing the first several lines at the top of the repeat loop, so that the script skips any lines in the text file that are blank.

It worked!

Thank you very much indeed.


Model: MacPro 14" M1 Max
AppleScript: 2.8
Browser: Firefox 96.0
Operating System: macOS 12