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

Jun 13, 2015 6:33 AM in response to Cooker8

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.

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.

Nov 3, 2015 6:04 AM in response to vnylund

vnylund wrote:


I've created the .csv file and placed all images in a folder - How and where should I place these files?


The CSV file and the images don't need to be in the same Folder as the Script will ask for the CSV file and then will ask for the Folder with the images.


Looks like what you posted should work, but, if as its not, please edit the script as follows (this is for debugging purposes):

change:

mv "$OldImageName" "$NewImageName"

to:

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


then post a few lines of what's in the debug.txt file that's created on the Desktop.

Dec 5, 2016 2:02 PM in response to antegrav88

You can use Automator, which will prompt you for the Folder to search.

Below is from the previous pages in the thread.

This will also convert line endings if necessary.

You just need to change the Run Shell Script Action to:


cd "$1"

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

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

find . -mindepth 2 -maxdepth 2 -name "$OldImageName" -execdir mv {} "$NewImageName" \;

done < ~/Desktop/file.tmp

rm ~/Desktop/file.tmp



User uploaded file


https://discussions.apple.com/content/attachment/580142040

May 4, 2014 1:24 PM in response to kmgold

The 1st bash script listed here should work:


#!/bin/bash


cd ~/Documents/images

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

mv "$OldImageName" "$NewImageName"

done <"SND.csv"


As is does not, try this, then post a few lines of the error.txt file that will be created on the Desktop


#!/bin/bash


cd ~/Documents/images
while read line          
do          
     OldImageName=${line%,*}
     NewImageName=${line#*,}
     echo mv "$OldImageName" "$NewImageName" >> ~/Desktop/error.txt
done <"SND.csv"

Nov 4, 2015 8:16 AM in response to vnylund

I tested it and it should be working.


Lets try something simple.


Save a portion of the CSV file to the Desktop and name it Untitled.csv

Then create an Automator Workflow with this one Run Shell Script Action:

cd ~/Desktop

while read line

do

OldImageName=${line%;*}

NewImageName=${line#*;}

echo mv "$OldImageName" "$NewImageName"

done <"Untitled.csv"


Then run the workflow, you should see in the [Results] window the command that would be run (if echo was removed):


User uploaded file


Now, if you see this when you run it, remove echo from the script (so it reads: mv "$OldImageName" "$NewImageName") then copy a few of the image files to the Desktop and run the Workflow again (the image files on the Desktop should be renamed)

Jun 27, 2016 5:00 AM in response to Cartaphilis

>That is what I thought too but no dice. I wonder if my file is the issue?


Could be the line endings in the file. The script is expecting a UNIX \n, but could be a Windoes \r\n

Need to see what line endings are used in your csv file:

In Terminal, try cat -e on the text file to view the line endings (if a UNIX \n you'll see $ at the end, if Windows, you'll see ^M$)


If it is a non-UNIX file, you can convert the file with tr by deleting the carriage return \r:

tr -d '\r' < ~/Desktop/originalfile.csv > ~/Desktop/Newfile.csv

Then you can run the script on Newfile.csv


edit: Never mind, I see now that you solved it.

Dec 4, 2016 3:12 PM in response to antegrav88

If I understand correctly, you have a csv file (in a plain text file named TheFile.csv), for example:


test.pdf,testNew.pdf

test2.pdf,test2New.pdf


And you want to search for the 1st item in the csv file in a directory structure one level below where the search is to start (and rename that file to the 2nd item in the csv file).

So, if the Folders Structure you posted is on your Desktop, to search those folders (but not folders within those folders)

Try:


#!/bin/bash


cd ~/Desktop

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

find . -mindepth 2 -maxdepth 2 -name "$OldImageName" -execdir mv {} "$NewImageName" \;

done <"TheFile.csv"


(Note: Test before using on your data)

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 🙂

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"

May 5, 2014 8:47 AM in response to kmgold

Try to run from Applescript.

Copy this to Apple Script Editor, and click [Run]

Also click [Result] at the bottom of AppleScript Editor to show any errors:


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



User uploaded file

May 23, 2014 6:09 AM in response to Mark Jalbert

Just pipe to a tmp file and add a LF to the eof, then convert CR to LF:



cd "$1"
tFile=$(mktemp -t tmp)
cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"
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"


[Note: This works for both CR and CRLF endings as the extra LF in a csv file saved as Window csv from excel on a mac is ignored as we test for non-existant files. However, this can be adapted by just changingtr '\r' '\n' to tr -d '\r' ]


This bash script is written to be used in this Automator Workflow:


User uploaded file

Jun 11, 2015 1:57 PM in response to Cooker8

Are you always moving to the same new folder?

If so, you can easily add the new folder to the script.

As this thread is now 7 pages, I'm not sure which part that you are using, however to move to a folder called NewFolder on the Desktop:


Change the line that moves the file:

from:

mv "$OldImageName" "$NewImageName"

to:

mv "$OldImageName" "$HOME/Desktop/NewFolder/$NewImageName"




You can also test if the file exists, and if not, print the missing name to a txt file:


#!/bin/bash


cd ~/Documents

while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

if [ ! -e "$OldImageName" ] ; then

echo "$OldImageName >> "HOME/Desktop/MissingFiles.txt"

else

mv "$OldImageName" "$NewImageName"

fi

done <"TheFile.csv"

This assumes that Documents is the Folder where both the files reside and the csv named TheFile.csv)

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.