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

Feb 8, 2016 12:55 PM in response to jorrflv

jorrflv wrote:


Tony, You seem to be giving a lot of help here and I appreciate that. I am trying to do something similar with pdf files, but I am not getting errors, nor are my file names changing.


Do you see anything wrong with the code?


First lets see that the csv file has the correct line endings.

Create a test csv file with TextEdit in Plain Text (TextEdit->Format->Make Plain Text).

Type a few lines in by hand (don't cut-n-paste) and then run the workflow selecting this csv file.

(If it is a problem with the line endings, then we can add a line to the script to do a conversion)


If it still is not working, lets debug with "echo":

add: echo before cp and mv and then at the end of each line add: >> $HOME/Desktop/debug.txt

This will create a file on the Desktop showing us what is (or is not) happening

Feb 8, 2016 1:42 PM in response to jorrflv

jorrflv wrote:


I tried new .csv file it was same results. I tried adding the echo command and I am not getting any debug files. Should look like this?




Need to add >> before $HOME (this redirects stdout to a file):

>> $HOME/Desktop/debug.txt


Also, it should not matter, but remove the white space in the script:


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

echo cp "$OldImageName" "~/Documents/$NewImageName" >> $HOME/Desktop/debug.txt

echo mv "$OldImageName" "$NewImageName" >> $HOME/Desktop/debug.txt

done < "$2"

And post a few line of the csv file so that I can test it on my end

Feb 8, 2016 1:59 PM in response to Tony T1

Here are the sample csv lines:

OldImageName,NewImageName

Student1.pdf,Aizenberg_Samuel_0028

Student2.pdf,Alexander_Pierce_0024



Here are the results of the debug.txt file


cp OldImageName /Users/orrj/Documents/NewImageName

mv OldImageName NewImageName

cp Student1.pdf /Users/orrj/Documents/Aizenberg_Samuel_0028

mv Student1.pdf Aizenberg_Samuel_0028

Feb 8, 2016 2:25 PM in response to jorrflv

ok, so it looks like it's not finding the file (it looks like it's not changing to the directory with the file)

First, remove the debug (echo and >> $HOME/Desktop/debug.txt)


Now, lets try the command by hand in Terminal:

Open Terminal (Applications/Utilities/Terminal)

cd to the Folder with the files (enter cd and then the folder name and then press enter.)

You should see the folder name in the command prompt.

For example, when I cd to Documents, I see: MacBook-Air:~/Documents Tony$

(You can get a list of the directory by entering ls)


When you are sure you changed to the correct directory:

Copy the commands that's in the debug.txt file and paste into the Terminal Window:

cp Student1.pdf /Users/orrj/Documents/Aizenberg_Samuel_0028

and

mv Student1.pdf Aizenberg_Samuel_0028


If these copy and move as expected, then lets debug the cd command:

Add echo to the 1st line:

echo cd "$1" > $HOME/Desktop/debug.txt

(note: the single > will overwrite the existing debug.txt file)

Feb 8, 2016 2:55 PM in response to jorrflv

jorrflv wrote:


I am trying to follow the bash script, but I can see right now that there will be an issue because the folders are on my desktop not in my documents folder... that would make a difference yes?


Should not make a difference.

The first action selects the Folder that has the files.

Now that I think about it, there is no need to debug the cd.


Run the Workflow Again (with no echo commands), then when it completes, click the [Results] box and you'll see the directory that the workflow is changing to:

In this example, I selected the Untitled Folder on my Desktop:


User uploaded file


When you click [Results] you should see the folder with the files

Feb 8, 2016 3:48 PM in response to jorrflv

jorrflv wrote:


awesome... The first line went.. Now when I try my other CSV file with the whole batch it is not moving on to the next record. none of the other files are renamed.

Jon


ok, then that means that we need to convert the line ends.

How was the CSV file created? (i.e was it from an excel file?)


I'm going to guess that there are carriage returns in the file that needs to be removed.

Add this line: NewImageName=${NewImageName//$'\r'}


So the script should be:


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

NewImageName=${NewImageName//$'\r'}

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

mv "$OldImageName" "$NewImageName

done < "$2"

If this does not work, then we need to see what the line ends look like, so open Terminal, enter cat -e then space then drag the CSV file to the Terminal Window and press enter and then post a few lines.

Feb 8, 2016 4:27 PM in response to Tony T1

added the line no go... here are the results of terminal command


OldImageName,NewImageName^MStudent1.pdf,Aizenberg_Samuel_0028^MStudent2.pdf,Alex ander_Pierce_0024^MStudent3.pdf,Anderson_Eric_8583^MStudent4.pdf,Apalategui_Aime e_7987^MStudent5.pdf,Attanasio_Nia_7722^MStudent6.pdf,Ayoub-Butcher_Angelina_009 9^MStudent7.pdf,Badger_Gage_7751^M

Feb 8, 2016 4:59 PM in response to jorrflv

jorrflv wrote:


added the line no go... here are the results of terminal command


OldImageName,NewImageName^MStudent1.pdf,Aizenberg_Samuel_0028^MStudent2.pdf,Alex ander_Pierce_0024^MStudent3.pdf,Anderson_Eric_8583^MStudent4.pdf,Apalategui_Aim e e_7987^MStudent5.pdf,Attanasio_Nia_7722^MStudent6.pdf,Ayoub-Butcher_Angelina_00 9 9^MStudent7.pdf,Badger_Gage_7751^M


Looks like just a CR (my guess was CRLF)


This should work (translates CR to LF to file.tmp then deletes the tmp file when done):


cd "$1"

tr '\r' '\n' < "$2" > ~/Desktop/file.tmp

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

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

mv "$OldImageName" "$NewImageName

done < ~/Desktop/file.tmp

rm ~/Desktop/file.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.