Apple Event: May 7th at 7 am PT

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

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

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

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.

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"

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"

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

Feb 3, 2014 6:21 AM in response to Community User

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


In Numbers,

enter "wp-content/uploads/2014/02/Ditch_Witch_SK_300.jpg" in the column next to "Ditch_Witch_SK_300.jpg" and then in the 3rd column enter: =C3&B3 (the & concatenates the 2 strings):


User uploaded file


You can then convert the formula in col D to the concatenate string by selecting the column, copy (⌘C), then Paste Formula Results (⇧⌘V). and delete col B and C

Mar 26, 2014 10:44 AM in response to Tony T1

I really appreciate this thread. I'm trying to do something similar with .wav files. I have a CSV with col 1 being a non-descript filename (HiFe_345.wav) and I want to give it a familiar name (CrowCall_02.wav) which is listed in col 2 in my CSV file.


I've set up an Automator Workflow with the Shell Script as shown.


It ran perfectly... except, all my .wav files are now considered "documents" and won't open/play.

User uploaded filebecomes User uploaded file


If I manually delete the .wav extension and retype ".wav", Finder prompts me to confirm I want to change the filetype. Should I not include the file type extension in the familiar name column in my CSV? There are way too many files to manually change all the extensions... do I need to run another automator workflow?

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)

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.