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

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 2, 2016 1:51 PM in response to Tony T1

Hi Tony, sorry to bug on this old thread, but like the previous poster said you seem to be very helpful on this thread. I've used the script you created and it works perfect. My question can this script be modified for it to look under multiple folders? All the image files are within their own folder and was wondering if the script would be able to just find all the files in the different directories and rename the files according to the CSV file. As an ideal i'm attaching the screenshot of my CSV file, if that helps. if you notice that all the files start with a different number so each number corresponds to the file folder. Example of folder structure attached aswell. Any help would be greatly appreciated it! So far this script you created only works for files in one folder right? Thank you so much!User uploaded fileUser uploaded file

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)

Dec 4, 2016 9:25 PM in response to Tony T1

Thank you so much for you reply! Yes your understanding to what I'm trying to accomplish is correct. I tried running the script but the files I tested on, didn't rename. Can this script be used with the same set up you had for in automator or do I have to run it separately? Sorry, I'm a noob when it comes to scripting and still learning and trying to understand a lot.

Another question if i had all the folders in another folder on my desktop (ex. /Desktop/Color_Rename/14901) would i have to put the path in as follows cd ~/Desktop/Color_Rename/


Also by reading past posts do we still need to add the string if we're using a CSV file created on a Mac Excel?


Thank you again for helping me!

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

Dec 5, 2016 3:03 PM in response to Tony T1

Hi Tony,


I noticed that you were using tr to change the carriage return to a newline. If you are attempting to change \r\n line-ending, then this usage of tr will result in \n\n. If that is not your planning, then this article shows how to use Perl and its '\R' meta-character to produce a solitary newline ending. The '\R' usage is basically, “I don't care what the line ending is, just make it the replacement value.”

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.