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
Question marked as Best reply

Jan 30, 2014 6:29 PM in response to Community User

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"

Jan 30, 2014 6:43 PM in response to Community User

In applescript it goes something like this:


(*

this all assuming the CSV file is a plain text file with a comma delimiting

the old and new filenames on a given line. If you have another structure

you need to give specifics so this can be modified

*)


set theCSVData to paragraphs of (read "/path/to/theCSVFile.csv")


set {oldTID, my text item delimiters} to {my text item delimiters, ","}

repeat with thisLine in theCSVData

set {oldFileName, newFileName} to text items of thisLine

tell application "System Events"

set name of file oldFileName of folder "path/to/folder of images" to newFileName

end tell

end repeat

set my text item delimiters to oldTID


Note that I've made a lot of assumptions about your csv file, any or all of which may be wrong. please clarify as needed.

Jan 30, 2014 6:51 PM in response to Community User

You can also make this into and Automator Workflow or App.

Add this shell script:

cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

mv "$OldImageName" "$NewImageName"

done <"$2"


as part of this Automator Workflow:


User uploaded fileUser uploaded file


Edit: twtwtw posted an Applescript example (which is what you're looking for) while I was posting this Automator example. You now have 3 ways to tackle your problem 🙂

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

Jan 31, 2014 4:40 AM in response to Community User

kittersa wrote:

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


Great 😎


I'm not sure if it's easier to add _1 to every row name in Numbers


If you can add it to a new column, then you could just use a formula:


User uploaded file


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


To do this, add one line in the script to first Copy and rename (cp) before the rename (mv)

cp "$OldImageName" ~/Documents/"$NewImageName"_1


so the script (to add to Automator) now looks like:

(this will copy and rename first to the Documents Folder)


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

cp "$OldImageName" ~/Documents/"$NewImageName"_1

mv "$OldImageName" "$NewImageName"

done <"$2"

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

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.