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

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 27, 2014 1:34 PM in response to Tony T1

Hey Tony,


I really appreciate your help on this. I don't want to beat a dead horse, nor do I want anyone else to on my behalf. I thought I'd just add one last update to this thread:

When I rename the file... I notice that there is a carriage return AFTER the extension! This line break isn't apparent in my CSV file, and I'm not sure where it's coming from.

User uploaded file

Trying to rename the file reveals that there is a space beneath the file name. After I delete it—just the space—I get an alert about changing the extension. The option to keep the current extension ALSO has the line break.

User uploaded file

Food for thought.

Mar 27, 2014 4:18 PM in response to Tony T1

Hmm, no deal for either solution. Somehow, a return is still getting added to the end of the file extension.

I wonder if adjusting the CSV to hold the file name only, and then add the extension via the workflow... might that be a workaround? (clueless)


I appreciate your help here, if you have better things to do than to mess with this, I totally understand!😀

Mar 27, 2014 4:58 PM in response to bj49wd

Got it. Need $'\r'

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard



This should do it 🙂


NewImageName=${NewImageName//$'\r'}


so:


while read line

do

OldImageName=${line%,*}

NewImageName=${line#*,}

NewImageName=${NewImageName//$'\r'}

mv "$OldImageName" "$NewImageName"

done <"$2"

May 4, 2014 1:04 PM in response to Tony T1

Hi, Sorry to add to this thread, but I'm strapped for time and I need assistance with the scripts described here; I am not able to get any of them to work. My situation is almost identical: The images are in a folder called "images" on my desktop (~/Desktop/images) and the csv file is contained within it (~/Desktop/images/SND.csv)


User uploaded file


So, for example in line 1:I would like the file named 0031.jpg to be changed to 31-Dallas Morning News.jpg. I've been at this problem for a while, so any assistance would be greatly appreciate it.

May 4, 2014 4:23 PM in response to Tony T1

Thanks for the prompt reply.


Originally, when running the script, there was no error, but there was no affect on any of the images.


On a second try, I omitted the first line cd ~/[Desktop]/images and tried saving the script in the folder with everything else and running it from there. At that point, I get a "while: bad interpreter: No such file or directory" error message in the terminal.


Nothing shows on the desktop at either point.


Perhaps I am executing the script incorrectly?


I used SublimeText to create a new script, which i called "rename.sh." I then navigated to it's directory, changed the permissions using the following command: "chmod +x ./rename.sh." I then ran it using the "./rename.sh" command.


Let me know if you need further details and thanks again.

May 5, 2014 8:29 AM in response to Tony T1

Still not working; when I run the script from the desktop directory, I get no errors and no affect on the images in the folder.


When the script is run from the images directory, I get the following, based on whether or not I include the first (second after #!/bin/bash) line.


cd: bad interpreter: No such file or directory

-or-

while: bad interpreter: No such file or directory


which bash yields "/bin/bash"

May 21, 2014 1:55 PM in response to Tony T1

It will not allow me to. Keeps telling me there is an error. I can only post a small part.


ˇÿˇ‡




'

=

T

j

Å

ò

Æ

Û " 9 Q i Ä ò ∞ » · ˘ * C u é ß ¿ Ÿ Û





&

@

Z

t

é

©

¯ . I d  õ ∂ “ Ó % A ^ z ñ ≥ œ Ï & C a ~ õ π ◊ ı 1 O m å ™ … Ë & E d Ñ £ √ „ # C c É § ≈  ' I j ã ≠ Œ  4 V x õ Ω ‡ & I l è ≤ ÷ ˙ A e â Æ “ ˜ @ e ä Ø ’ ˙ E k ë ∑ › * Q w û ≈ Ï ; c ä ≤ ⁄ * R { £ à ı G p ô √ Ï @ j î æ È > i î ø Í A l ò ƒ ! !H!u!°!Œ!˚"'"U"Ç"Ø"›#

#8#f#î#¬#$ $M$|$´$⁄% %8%h%ó%«%˜&'&W&á&∑&Ë' 'I'z'´'‹(

(?(q(¢(‘) )8)k)ù)–* *5*h*õ*œ+ +6+i+ù+—, ,9,n,¢

ÅkÅÕÇ0ÇíÇÙÉWÉ∫Ñ ÑÄÑ„ÖGÖ´Ü ÜrÜ◊á;áüà àiàŒâ3âôâ˛ädä ã0ãñã¸åcå ç1çòçˇéféŒè6èûê ênê÷ë?ë®í ízí„ìMì∂î îäîÙï_ï…ñ4ñüó

óuó‡òLò∏ô$ôêô¸öhö’õBõØú úâú˜ùdù“û@ûÆü üãü˙†i†ÿ°G°∂¢&¢ñ£ £v£Ê§V§«•8•©¶ ¶ã¶˝ßn߇®R®ƒ©7©©™ ™è´ ´u´È¨¨–≠D≠∏Æ-Æ°Ø Øã∞

æÑæˇøzøı¿p¿Ï¡g¡„¬_¬€√X√‘ƒQƒŒ≈K≈»∆F∆√«A«ø»=»º…:…π 8 ∑À6À∂Ã5õÕ5ÕµŒ6Œ∂œ7œ∏–9–∫—<—æ“?“¡”D”∆‘I‘À’N’—÷U÷ÿ◊◊‡ÿdÿËŸlŸÒ⁄v⁄˚€Ä‹ ‹ä› ›ñfi fi¢fl)fl؇6‡Ω·D·Ã‚S‚€„c„Ήs‰¸ÂÑÊ

ÊñÁ Á©Ë2˺ÈFÈ–Í[ÍÂÎpÎ˚ÏÜÌ ÌúÓ(Ó¥Ô@ÔÃXÂÒrÒˇÚåÛ ÛßÙ4Ù¬ıPıfiˆmˆ˚˜ä¯ ¯®˘8˘«˙W˙Á˚w¸ ¸ò˝)˝∫˛K˛‹ˇmˇˇˇ· 2Exif











<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:about=""

xmlns:dc="http://purl.org/dc/elements/1.1/">

<dc:description>

<rdf:Seq>

<rdf:li xml:lang="x-default"/>

</rdf:Seq>

</dc:description>

<dc:subject>

<rdf:Bag/>

</dc:subject>

<dc:title>

<rdf:Seq>

<rdf:li xml:lang="x-default">Vanderploeg010</rdf:li>

</rdf:Seq>

</dc:title>

</rdf:Description>

</rdf:RDF>

</x:xmpmeta>

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.