userremoved

Q: 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,

 

Old-Image-New-Image.jpg

MacBook Pro with Retina display, iOS 7.0.4

Posted on Dec 27, 2015 4:28 PM

Close

Q: Apple Script to rename images using CSV file

  • All replies
  • Helpful answers

Page 1 of 11 last Next
  • by Tony T1,Helpful

    Tony T1 Tony T1 Jan 30, 2014 6:29 PM in response to userremoved
    Level 6 (9,249 points)
    Mac OS X
    Jan 30, 2014 6:29 PM in response to userremoved

    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"

  • by twtwtw,Helpful

    twtwtw twtwtw Jan 30, 2014 6:43 PM in response to userremoved
    Level 5 (4,935 points)
    Jan 30, 2014 6:43 PM in response to userremoved

    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.

  • by Tony T1,

    Tony T1 Tony T1 Jan 30, 2014 6:51 PM in response to userremoved
    Level 6 (9,249 points)
    Mac OS X
    Jan 30, 2014 6:51 PM in response to userremoved

    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:

     

    Screen Shot 2014-01-30 at 9.46.36 PM.pngScreen Shot 2014-01-30 at 9.46.49 PM.png

     

    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

  • by userremoved,

    userremoved userremoved Dec 27, 2015 4:28 PM in response to Tony T1
    Level 1 (0 points)
    Dec 27, 2015 4:28 PM in response to Tony T1

    Hello, thanks for the quick reply. I have tried using the script but can't seem to get it working. When I click Run on the AppleScript Editor I get a message stating:

     

    Syntax Error

    Expected end of line, etc. but found “while”.

     

    Below is what I have changed. I put the CSV file in the same folder as the images

     

    #!/bin/bash

     

    cd /Users/aaron/Desktop/Images

     

    while read line         

     

    do         

     

        OldImageName=${line%,*}

     

              NewImageName=${line#*,}

     

        mv "$OldImageName" "$NewImageName"

     

    done <"/Users/aaron/Desktop/Images/CSVFile.csv"

  • by Tony T1,

    Tony T1 Tony T1 Jan 30, 2014 6:54 PM in response to userremoved
    Level 6 (9,249 points)
    Mac OS X
    Jan 30, 2014 6:54 PM in response to userremoved

    I posted a bash script, not an Applescript. 

    You would need to run this from Terminal.

    Better to use twtwtw's Applescript example below if you're not familiar with bash scripts and Terminal

  • by userremoved,

    userremoved userremoved Dec 27, 2015 4:28 PM in response to Tony T1
    Level 1 (0 points)
    Dec 27, 2015 4:28 PM in response to Tony T1

    Hello twtwtw,

     

    I have tried your script and I get the following error message:

     

    error "System Events got an error: Can’t set file \"Old Image Name\" of folder \"/Users/aaron/Desktop/Images\" to \"New Image Name\"." number -10006 from file "Old Image Name" of folder "/Users/aaronkitney/Desktop/Images"

     

    How shall I export the CSVFile from Numbers? I currently export as Unicode UTF-8. Shall I put the CSVFile on my desktop or inside the images folder?

     

    Thanks for your help as well Tony,

  • by twtwtw,

    twtwtw twtwtw Jan 30, 2014 7:57 PM in response to userremoved
    Level 5 (4,935 points)
    Jan 30, 2014 7:57 PM in response to userremoved

    Sorry, I didn't realize the headers were written directly into the CSV file. You'll want to skip the first line.  Or maybe better, catch and ignore file-doesn't-exist errors like so:

     

              tell application "System Events"

                        try

                                  set name of file oldFileName of folder "path/to/folder of images" to newFileName

                        end try

              end tell


  • by userremoved,

    userremoved userremoved Dec 27, 2015 4:28 PM in response to userremoved
    Level 1 (0 points)
    Dec 27, 2015 4:28 PM in response to userremoved

    Hello Tony,

     

    I managed to get your idea your Automator version working, really happy. Thanks.

     

    I did have another Automator/Script issue that , istead of processing images I would like to rename the file image file in the Numbers doc. I need to put a _1 after about 600 images

     

    As I am uploading two images per product item onto the website, I can't have them named the same because they will overwrite each other on the FTP. I'm not sure if it's easier to add _1 to every row name in Numbers, 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. Either way I'm not sure how to write a script to accomadate this.

     

    If this is confusing I make a few screen shots to help explain

     

    I can't express how grateful I am already, I spent most of today testing different plugins to no avail

  • by Tony T1,

    Tony T1 Tony T1 Jan 31, 2014 4:40 AM in response to userremoved
    Level 6 (9,249 points)
    Mac OS X
    Jan 31, 2014 4:40 AM in response to userremoved

    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:

     

         Screen Shot 2014-01-31 at 7.25.24 AM.png

     

    ....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"

  • by Tony T1,

    Tony T1 Tony T1 Jan 31, 2014 5:03 AM in response to Tony T1
    Level 6 (9,249 points)
    Mac OS X
    Jan 31, 2014 5:03 AM in response to Tony T1

    Oh, if you want the files with _1 in the same folder, just delete ~/Documents/ and use:

        

         cd "$1"

         while read line       

         do       

              OldImageName=${line%,*}

              NewImageName=${line#*,}

              cp "$OldImageName" "$NewImageName"_1

              mv "$OldImageName" "$NewImageName"

         done <"$2"

  • by userremoved,

    userremoved userremoved Dec 27, 2015 4:28 PM in response to Tony T1
    Level 1 (0 points)
    Dec 27, 2015 4:28 PM in response to Tony T1

    Thanks.

     

    Going back to my original issue. After I finished my first batch of images I renamed that folder to 'Featured Images' and created a new folder called 'Images'. I tried to batch rename another CSV file with my Supporting website Images, but I encounter an error again. I cant figure out what I'm doing wrong. I'm essentially just trying to copy the script as before

     

    I've attached an image - the error can be seen at the bottom of Automator Images Rename.jpg

  • by Tony T1,

    Tony T1 Tony T1 Jan 31, 2014 10:38 AM in response to userremoved
    Level 6 (9,249 points)
    Mac OS X
    Jan 31, 2014 10:38 AM in response to userremoved

    In the second Action, you need to use the Set Value of Variable Action, not Get

     

    Screen Shot 2014-01-31 at 1.37.53 PM.png

  • by userremoved,

    userremoved userremoved Dec 27, 2015 4:28 PM in response to Tony T1
    Level 1 (0 points)
    Dec 27, 2015 4:28 PM in response to Tony T1

    Dreamy. Glad you spot that.

     

    Thanks again

  • by Tony T1,

    Tony T1 Tony T1 Jan 31, 2014 10:58 AM in response to userremoved
    Level 6 (9,249 points)
    Mac OS X
    Jan 31, 2014 10:58 AM in response to userremoved

     

Page 1 of 11 last Next