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

Jan 31, 2014 5:51 PM in response to Community User

The variable name (Storage) is not the issue. Name does not matter, as we're not referencing it by name (we're using $1 and $2).

The problem appears to be that the line read is a file that does not exist (or an typo in the csv file).


Just need to add a line to test if the file exists.

So try with this:


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ -e $OldImageName ] ; then

mv "$OldImageName" "$NewImageName"

else

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

fi

done <"$2"


If there is an error, it will be listed in the file Errors.txt on the Desktop

May 21, 2014 3:17 PM in response to Amosiko

If you want, you can give it a try in Applescript.


This assumes that the image and csv file are on the Desktop.

Open Applescript, paste this into it and change SND.csv to the name of your csv file:


do shell script "
cd ~/Desktop
while read line          
do          
     OldImageName=${line%,*}
     NewImageName=${line#*,}
     mv \"$OldImageName\" \"$NewImageName\"
done < \"SND.csv\"
"



Then in Applescript, click Run


Jun 11, 2015 5:03 PM in response to Cooker8

Not sure if this is the problem but the mv should be on one line:

mv "$OldImageName" "Desktop/NEWFOLDER/$NewImageName"


If this does not work, we need to see what the script is doing with echo.

To debug, change the line to:

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


Run, and then look at the debug.txt file on the desktop to see what the script is trying to do with the mv.

Feb 1, 2014 9:39 AM in response to Community User

kittersa wrote:


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.


Hmm.... %OD is a carriage return, so we'll use tr to get rid of it with:

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


Try:


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"

Mar 26, 2014 1:24 PM in response to bj49wd

That is strange.

What happens if you:


while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

cp "$OldImageName" "$NewImageName"

rm $OldImageName"

done <"$2"


or:


while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

ditto "$OldImageName" "$NewImageName"

rm $OldImageName"

done <"$2"


ditto might work as it preserves extended attributes (but mv should have also)

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

For whatever reason I can't seem to get this to work.

Your csv file needs to be plain text (ascii text). Plain text may have old style mac line endings (CR), windows line endings (CRLF), or unix line endings (LF).

Cocoa GUI applications can read and write to all those variations of plain text files but Unix (posix) applications need LF line endings in order to work properly.

Excel on a Mac saves csv files with the old style mac line endings (CR- carrage return) unless you select the Windows csv option. In each option the saved csv file does not have the last line terminated. This can sometimes be an issue. Apple's Numbers application saves csv files with Windows line endings (CRLF).

Your csv file more than likely needs to be converted to unix line endings (LF- line feed).

May 22, 2014 9:46 AM in response to Tony T1

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

Well it doesn't. The following csv file was produced from excel on a mac:


cat -tve oldmac.csv
0031.jpg,31-foo bar.jpg^M0048.jpg,48-bar of foo.jpg^M0053.jpg,53-bar of foo.jpg[KSH_93u+] $

#Line isn't terminated

wc -l oldmac.csv
     0 oldmac.csv

while read line; do
echo ${line%,*}
done < oldmac.csv

#No results

A csv file saved as Window csv from excel on a mac:


cat -tve windows.csv
0031.jpg,31-foo bar.jpg^M$
0048.jpg,48-bar of foo.jpg^M$
0053.jpg,53-bar of foo.jpg[KSH_93u+] $

#The last line isn't terminated

wc -l windows.csv
     2 windows.csv


while read line; do
echo ${line%,*}          
 done < windows.csv
0031.jpg
0048.jpg

#Last line isn't read

In order to make your script work seamlessly, you are going to have to test the csv file for it's line endings and change them to unix line endings and terminate the last line.

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"

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.

Jan 31, 2014 6:26 PM in response to Tony T1

Opps!, forgot to quote if [ -e "$OldImageName" ]


Use:


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ -e "$OldImageName" ] ; then

mv "$OldImageName" "$NewImageName"

else

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

fi

done <"$2"


If there is an error, it will be listed in the file Errors.txt on the Desktop

Feb 1, 2014 8:32 AM in response to Community User

kittersa wrote:


Hey Tony,

Using the latest Script I got...

Error.txt

Old Image Name


Good! That's the Header in your CSV FIle, so all other lines should have processed without error.

If you don't want the Error.txt file produced, just comment out the 2 lines with a # like so:


cd "$1"

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ -e "$OldImageName" ] ; then

mv "$OldImageName" "$NewImageName"

#else

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

fi

done <"$2"

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.