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 Top-ranking reply

Posted on Jun 13, 2015 6:33 AM

I just noticed that the OldImageName files in the csv file snapshot you posted has no extensions, is that correct?

(If the actual file has extensions, that's the problem)


For example, if your listing the file in the cvs as 56958 but the file is actually 56958.jpg or 56958.gif, then the script (or the cvs file) needs to be adjusted.

165 replies

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"

Dec 27, 2015 4:28 PM in response to Tony T1

Hello, thanks for the quick reply. I have tried using the script but can't seem to get it working. When I click Run on the AppleScript Editor I get a message stating:


Syntax Error

Expected end of line, etc. but found “while”.


Below is what I have changed. I put the CSV file in the same folder as the images


#!/bin/bash


cd /Users/aaron/Desktop/Images


while read line


do


OldImageName=${line%,*}


NewImageName=${line#*,}


mv "$OldImageName" "$NewImageName"


done <"/Users/aaron/Desktop/Images/CSVFile.csv"

Dec 27, 2015 4:28 PM in response to Tony T1

Hello twtwtw,


I have tried your script and I get the following error message:


error "System Events got an error: Can’t set file \"Old Image Name\" of folder \"/Users/aaron/Desktop/Images\" to \"New Image Name\"." number -10006 from file "Old Image Name" of folder "/Users/aaronkitney/Desktop/Images"


How shall I export the CSVFile from Numbers? I currently export as Unicode UTF-8. Shall I put the CSVFile on my desktop or inside the images folder?


Thanks for your help as well Tony,

Dec 27, 2015 4:28 PM in response to Community User

Hello Tony,


I managed to get your idea your Automator version working, really happy. Thanks.


I did have another Automator/Script issue that , istead of processing images I would like to rename the file image file in the Numbers doc. I need to put a _1 after about 600 images


As I am uploading two images per product item onto the website, I can't have them named the same because they will overwrite each other on the FTP. I'm not sure if it's easier to add _1 to every row name in Numbers, or save out the images (using the previous script provided) into a differen't folder (so it doesn't overide on my desktop) then add _1 to the each image. Either way I'm not sure how to write a script to accomadate this.


If this is confusing I make a few screen shots to help explain


I can't express how grateful I am already, I spent most of today testing different plugins to no avail

Dec 27, 2015 4:28 PM in response to Tony T1

Thanks.


Going back to my original issue. After I finished my first batch of images I renamed that folder to 'Featured Images' and created a new folder called 'Images'. I tried to batch rename another CSV file with my Supporting website Images, but I encounter an error again. I cant figure out what I'm doing wrong. I'm essentially just trying to copy the script as before


I've attached an image - the error can be seen at the bottom of Automator User uploaded file

Dec 27, 2015 4:28 PM in response to Tony T1

Hello again,


Sorry for keep having to get in touch, I've been spending hours on this, sometimes it works and other times I get errors. I run into errors everytime I switch files. I've got to resave all the images again because I forgot to add the .jpg suffix and they won't upload into Wordpress (stressful for a friday)


I've attached a picture, I can only select 'Storage' in Variable, if i click the dropdown and click New Variable it just makes a 'Storage 2'. Maybe it's not even that which is the issue.


I hand typed 'Choose CSVFile' in the Ask for Finder Items.


Thanks for you help


User uploaded file

Dec 27, 2015 4:28 PM in response to Community User

There is something that happens during the Automator that changes the .jpg file. They cannot be loaded into Wordpress I get an error message saying:


“Ditch_Witch_SK_300.jpg%0D” has failed to upload due to an error

Sorry, this file type is not permitted for security reasons.

Dec 27, 2015 4:28 PM in response to Tony T1

Amazing as always.


In Numbers you wouldn't know how to add text in front of a word rather than at the end?


For example:


Ditch_Witch_SK_300.jpg > wp-content/uploads/2014/02/Ditch_Witch_SK_300.jpg


Much appreciated

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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 Account.