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

May 22, 2014 8:48 AM in response to Amosiko

This will fix the line endings per Mark's comment:

NewImageName=$( echo "$NewImageName" | tr -d '\r' )


So, try this:


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

NewImageName=$( echo "$NewImageName" | tr -d '\r' )

if [ -e "$OldImageName" ] ; then

mv "$OldImageName" "$NewImageName"

else

echo "$OldImageName" >> ~/Desktop/Errors.txt

fi

done <"$2"

May 24, 2014 6:16 AM in response to Tony T1

I like Mark's suggestion of testing with file:


cd "$1"

tFile=$(mktemp -t tmp)

fType=$(file -b "$2")

if [ "$fType" = "ASCII text, with CR line terminators" ]; then

cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"

elif [ "$fType" = "ASCII text, with CRLF line terminators" ]; then

cat "$2" | sed 'a\' | tr -d '\r' > "$tFile"

else

echo "Problem with CSV file ($fType)" > ~/Desktop/Errors.txt

rm "$tFile"

exit 1

fi

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ -e "$OldImageName" ] ; then

mv "$OldImageName" "$NewImageName"

else

echo "$OldImageName" >> ~/Desktop/Errors.txt

fi

done < "$tFile"

rm "$tFile"

May 24, 2014 6:26 AM in response to Tony T1

Edit: Need to do no conversion if LF's:


cd "$1"

tFile=$(mktemp -t tmp)

fType=$(file -b "$2")

if [ "$fType" = "ASCII text, with CR line terminators" ]; then

cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"

elif [ "$fType" = "ASCII text, with CRLF line terminators" ]; then

cat "$2" | sed 'a\' | tr -d '\r' > "$tFile"

elif [ "$fType" = "ASCII text" ]; then

cat "$2" | sed 'a\' > "$tFile"

else

echo "Problem with CSV file ($fType)" > ~/Desktop/Errors.txt

rm "$tFile"

exit 1

fi

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ -e "$OldImageName" ] ; then

mv "$OldImageName" "$NewImageName"

else

echo "$OldImageName" >> ~/Desktop/Errors.txt

fi

done < "$tFile"

rm "$tFile"

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

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.