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

first Previous Page 9 of 11 last Next
  • by vnylund,

    vnylund vnylund Nov 3, 2015 11:19 PM in response to Tony T1
    Level 1 (0 points)
    Nov 3, 2015 11:19 PM in response to Tony T1

    Hi Tony,

     

    Please see attached screenshot of the first lines in the .csv file.

     

    Thanks!

     

    csv_file.png

  • by Tony T1,

    Tony T1 Tony T1 Nov 4, 2015 6:25 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    Nov 4, 2015 6:25 AM in response to vnylund

    vnylund wrote:

    Please see attached screenshot of the first lines in the .csv file.

     

     

    Need to see how it looks in plain text (not excel).

    Can you open the file with TextEdit and then post a few lines?

  • by vnylund,

    vnylund vnylund Nov 4, 2015 6:28 AM in response to Tony T1
    Level 1 (0 points)
    Nov 4, 2015 6:28 AM in response to Tony T1

    Sorry about my mistake!

     

    Attached is a screenshot of the text in TextEdit and following are lines directly copy pasted:

     

    OldImageName;NewImageName

    0256_027.jpg;0256027L.jpg

    0283_3KH.jpg;PI02833KHL.jpg

    0283_3KH.jpg;PI02833KHM.jpg

    0283_3RH.jpg;PI02833RHS.jpg

    05DPP036_SM_PD22_n1z1.jpg;Y41R98070.jpg

    0705_4RF.jpg;07054RFM.jpg

    0705_4RF.jpg;07054RFS.jpg

    0705_4RF.jpg;07054RFXL.jpg

    0705_4RF.jpg;07054RFXXL.jpg

    0711_3RH.JPG;PI07113RHXL.jpg

    0877_3YY.jpg;PI08773YYM.jpg

    0991706 GEL BLOCK.jpg;EL0991706.jpg

    11111029-31._BibTight Elite AmFIB_Blk_2010.jpg;PI11111031021M.jpg

    11111029-31._BibTight Elite AmFIB_Blk_2010.jpg;PI11111031021XL.jpg

    11111032021.jpg;11111032021L.jpg

    11111032021.jpg;11111032021M.jpg

    11111033-35._BibTight El. Th.Fleece_Blk_2010.jpg;PI11111035021M.jpg

    11111037._Pant Sel. Barrier WXB_Blk_2010.jpg;PI11111037021L.jpg

    11111037._Pant Sel. Barrier WXB_Blk_2010.jpg;PI11111037021S.jpg

    11111037._Pant Sel. Barrier WXB_Blk_2010.jpg;PI11111037021XL.jpg

     

     

     

    Screenshot.png

  • by Tony T1,

    Tony T1 Tony T1 Nov 4, 2015 7:36 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    Nov 4, 2015 7:36 AM in response to vnylund

    ok, I see the problem.

    The names are separated with ;  not ,

     

    In the script:

    change:

         OldImageName=${line%,*}

         NewImageName=${line#*,}

    to:   

         OldImageName=${line%;*}

         NewImageName=${line#*;}

  • by vnylund,

    vnylund vnylund Nov 4, 2015 7:51 AM in response to Tony T1
    Level 1 (0 points)
    Nov 4, 2015 7:51 AM in response to Tony T1

    Hi Tony!

     

    I tried it but still the same result; The script runs without any errors but the file names remain unchanged...

    Please see attached screenshot.

     

    Just want to say that I really appreciate your help and if we get it to work it will help me out a lot in my work!

     

    Screenshot.png

  • by Tony T1,

    Tony T1 Tony T1 Nov 4, 2015 8:16 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    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):

     

    Screen Shot 2015-11-04 at 11.07.16 AM.png

     

    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)


  • by williamleon,

    williamleon williamleon Nov 4, 2015 3:22 PM in response to vnylund
    Level 1 (4 points)
    iPhone
    Nov 4, 2015 3:22 PM in response to vnylund

    I Had the same problem where the script would run without error but with no changes to my image names - due to my immediate need I ended up going to out source website (upwork) and hired a guy to write the script exactly as I needed and then email it to me.  I have run it 6 times renaming 500 images each time - without error.  I would love to learn to write the script but this time I took the easier way - although more expensive - cost me $60US but in this case well worth it.  Good luck

  • by vnylund,

    vnylund vnylund Nov 5, 2015 12:02 AM in response to Tony T1
    Level 1 (0 points)
    Nov 5, 2015 12:02 AM in response to Tony T1

    Hi Tony,

     

    I'm afraid the result is still the same...

     

    Screenshot.png

  • by Tony T1,

    Tony T1 Tony T1 Nov 5, 2015 5:25 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    Nov 5, 2015 5:25 AM in response to vnylund

    That is strange.  It works when I tested on the data in your file.

     

    I wonder what you would get if you just tried to print the lines in the file.

    Try this:


         cd ~/Desktop

         while read line      

         do      

              echo $line

         done <"Untitled.csv"


  • by vnylund,

    vnylund vnylund Nov 5, 2015 5:31 AM in response to Tony T1
    Level 1 (0 points)
    Nov 5, 2015 5:31 AM in response to Tony T1

    Tony,

     

    Same thing again, runs script and no results...

  • by Tony T1,

    Tony T1 Tony T1 Nov 5, 2015 6:33 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    Nov 5, 2015 6:33 AM in response to vnylund

    And this is a plain text file? (In TextEdit Format->Make Plain Text)

  • by Tony T1,

    Tony T1 Tony T1 Nov 5, 2015 9:52 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    Nov 5, 2015 9:52 AM in response to vnylund

    vnylund wrote:

     

    Same thing again, runs script and no results...

     

    It's possible that Excel is saving the CSV with CR line ends.

    Lets try to convert from CR to LF with tr

     

    Try this with samples of the images to be renamed on the Desktop

    Use the original CSV file you have (rename "Untitled" in script)

    (the CR line feeds are saved to "Untitled.csv.tmp", then when the script completes, the tmp file is deleted)


    cd ~/Desktop

    tr '\r' '\n' < "Untitled.csv" > "Untitled.csv.tmp"

    while read line   

    do   

         OldImageName=${line%;*}

         NewImageName=${line#*;}

         mv "$OldImageName" "$NewImageName"

    done < "Untitled.csv.tmp"

    rm "Untitled.csv.tmp"

  • by vnylund,

    vnylund vnylund Nov 9, 2015 6:13 AM in response to Tony T1
    Level 1 (0 points)
    Nov 9, 2015 6:13 AM in response to Tony T1

    Hi Tony!

     

    Sorry for the lack of response here but I've been away for a few days.

     

    I tried the script and this time it actually felt like something happened since it took a few seconds for the script to run. And the .tmp file showed on the desktop for a little while as well before it disappeared.

     

    What's the next step?

     

    Edit: I realized that I didn't place any images on the desktop so I did it again and it worked !!!!!! One image didn't get renamed though but I'll check if there's something wrong in the .csv file. But as for now I hope it will work.

     

    Edit#2: The script didn't process the last line in the .csv file so I just added a "dummy line" at the bottom and it renamed the last image as well.

     

    Thank you very much Tony!!!

     

     

     

     

    Screenshot.png

  • by Tony T1,

    Tony T1 Tony T1 Nov 9, 2015 6:27 AM in response to vnylund
    Level 6 (9,249 points)
    Mac OS X
    Nov 9, 2015 6:27 AM in response to vnylund

    vnylund wrote:

     

    Hi Tony!

     

    Edit: I realized that I didn't place any images on the desktop so I did it again and it worked !!!!!! One image didn't get renamed though but I'll check if there's something wrong in the .csv file. But as for now I hope it will work.

     

    Edit#2: The script didn't process the last line in the .csv file so I just added a "dummy line" at the bottom and it renamed the last image as well.

     

    Thank you very much Tony!!!

     

     

    Great.  Sorry it took so long for me to remember that Microsoft uses CR

    BTW, you can add the "dummy line" to the tmp file with the script by adding: echo >> Untitled.csv.tmp

     

    cd ~/Desktop

    tr '\r' '\n' < "Untitled.csv" > "Untitled.csv.tmp"

    echo >> Untitled.csv.tmp

    while read line

    do

         OldImageName=${line%;*}

         NewImageName=${line#*;}

         mv "$OldImageName" "$NewImageName"

    done < "Untitled.csv.tmp"

    rm "Untitled.csv.tmp"

  • by vnylund,

    vnylund vnylund Nov 9, 2015 7:20 AM in response to Tony T1
    Level 1 (0 points)
    Nov 9, 2015 7:20 AM in response to Tony T1

    Worked beautifully!

     

    Again thank you very much Tony! I really appreciate your help and it makes things a lot easier for me!!!

first Previous Page 9 of 11 last Next