Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple Script to rename images using CSV file

Hello, I have build a website using a Woocommerce for Wordpress. The site is going to have over 1200 images and they need to be renamed appropriately for each product. I've never used Apple Script before but after hours and hours of searching I have noticed people with similar issues have managed to solve the problem using AppleScript.


I have attached a screenshot of the CSV file first 20 files - also the images are all saved in a folder with the names _DSC7916 copy 2.jpg etc etc.


Hopefully my issue makes sense,


User uploaded file

MacBook Pro with Retina display, iOS 7.0.4

Posted on Jan 30, 2014 5:16 PM

Question marked as Best reply

Posted on Jan 30, 2014 6:29 PM

Should be simple with this bash script.

Change ~/Documents to the Folder where the files reside (assumes that the csv file and the jpg files are in the same Folder):


#!/bin/bash


cd ~/Documents

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

mv "$OldImageName" "$NewImageName"

done <"TheFile.csv"

165 replies

Nov 4, 2015 6:28 AM in response to Tony T1

Sorry about my mistake!


Attached is a screenshot of the text in TextEdit and following are lines directly copy pasted:


OldImageName;NewImageName

0256_027.jpg;0256027L.jpg

0283_3KH.jpg;PI02833KHL.jpg

0283_3KH.jpg;PI02833KHM.jpg

0283_3RH.jpg;PI02833RHS.jpg

05DPP036_SM_PD22_n1z1.jpg;Y41R98070.jpg

0705_4RF.jpg;07054RFM.jpg

0705_4RF.jpg;07054RFS.jpg

0705_4RF.jpg;07054RFXL.jpg

0705_4RF.jpg;07054RFXXL.jpg

0711_3RH.JPG;PI07113RHXL.jpg

0877_3YY.jpg;PI08773YYM.jpg

0991706 GEL BLOCK.jpg;EL0991706.jpg

11111029-31._BibTight Elite AmFIB_Blk_2010.jpg;PI11111031021M.jpg

11111029-31._BibTight Elite AmFIB_Blk_2010.jpg;PI11111031021XL.jpg

11111032021.jpg;11111032021L.jpg

11111032021.jpg;11111032021M.jpg

11111033-35._BibTight El. Th.Fleece_Blk_2010.jpg;PI11111035021M.jpg

11111037._Pant Sel. Barrier WXB_Blk_2010.jpg;PI11111037021L.jpg

11111037._Pant Sel. Barrier WXB_Blk_2010.jpg;PI11111037021S.jpg

11111037._Pant Sel. Barrier WXB_Blk_2010.jpg;PI11111037021XL.jpg




User uploaded file

Nov 4, 2015 8:16 AM in response to vnylund

I tested it and it should be working.


Lets try something simple.


Save a portion of the CSV file to the Desktop and name it Untitled.csv

Then create an Automator Workflow with this one Run Shell Script Action:

cd ~/Desktop

while read line

do

OldImageName=${line%;*}

NewImageName=${line#*;}

echo mv "$OldImageName" "$NewImageName"

done <"Untitled.csv"


Then run the workflow, you should see in the [Results] window the command that would be run (if echo was removed):


User uploaded file


Now, if you see this when you run it, remove echo from the script (so it reads: mv "$OldImageName" "$NewImageName") then copy a few of the image files to the Desktop and run the Workflow again (the image files on the Desktop should be renamed)

Nov 4, 2015 3:22 PM in response to vnylund

I Had the same problem where the script would run without error but with no changes to my image names - due to my immediate need I ended up going to out source website (upwork) and hired a guy to write the script exactly as I needed and then email it to me. I have run it 6 times renaming 500 images each time - without error. I would love to learn to write the script but this time I took the easier way - although more expensive - cost me $60US but in this case well worth it. Good luck

Nov 5, 2015 9:52 AM in response to vnylund

vnylund wrote:


Same thing again, runs script and no results...


It's possible that Excel is saving the CSV with CR line ends.

Lets try to convert from CR to LF with tr


Try this with samples of the images to be renamed on the Desktop

Use the original CSV file you have (rename "Untitled" in script)

(the CR line feeds are saved to "Untitled.csv.tmp", then when the script completes, the tmp file is deleted)

cd ~/Desktop

tr '\r' '\n' < "Untitled.csv" > "Untitled.csv.tmp"

while read line

do

OldImageName=${line%;*}

NewImageName=${line#*;}

mv "$OldImageName" "$NewImageName"

done < "Untitled.csv.tmp"

rm "Untitled.csv.tmp"

Nov 9, 2015 6:13 AM in response to Tony T1

Hi Tony!


Sorry for the lack of response here but I've been away for a few days.


I tried the script and this time it actually felt like something happened since it took a few seconds for the script to run. And the .tmp file showed on the desktop for a little while as well before it disappeared.


What's the next step? 🙂


Edit: I realized that I didn't place any images on the desktop so I did it again and it worked 😀!!!!!! One image didn't get renamed though but I'll check if there's something wrong in the .csv file. But as for now I hope it will work.


Edit#2: The script didn't process the last line in the .csv file so I just added a "dummy line" at the bottom and it renamed the last image as well.


Thank you very much Tony!!!





User uploaded file

Nov 9, 2015 6:27 AM in response to vnylund

vnylund wrote:


Hi Tony!


Edit: I realized that I didn't place any images on the desktop so I did it again and it worked 😀!!!!!! One image didn't get renamed though but I'll check if there's something wrong in the .csv file. But as for now I hope it will work.


Edit#2: The script didn't process the last line in the .csv file so I just added a "dummy line" at the bottom and it renamed the last image as well.


Thank you very much Tony!!!



Great. Sorry it took so long for me to remember that Microsoft uses CR

BTW, you can add the "dummy line" to the tmp file with the script by adding: echo >> Untitled.csv.tmp


cd ~/Desktop

tr '\r' '\n' < "Untitled.csv" > "Untitled.csv.tmp"

echo >> Untitled.csv.tmp

while read line

do

OldImageName=${line%;*}

NewImageName=${line#*;}

mv "$OldImageName" "$NewImageName"

done < "Untitled.csv.tmp"

rm "Untitled.csv.tmp"

Apple Script to rename images using CSV file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.