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 10 of 11 last Next
  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 11:58 AM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 11:58 AM in response to Tony T1

    Tony,  You seem to be giving a lot of help here and I appreciate that.  I am trying to do something similar with pdf files, but I am not getting errors, nor are my file names changing.

     

    Do you see anything wrong with the code?Screen Shot 2016-02-08 at 11.56.51 AM.png

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 12:55 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 12:55 PM in response to jorrflv

    jorrflv wrote:

     

    Tony,  You seem to be giving a lot of help here and I appreciate that.  I am trying to do something similar with pdf files, but I am not getting errors, nor are my file names changing.

     

    Do you see anything wrong with the code?

     

    First lets see that the csv file has the correct line endings.

    Create a test csv file with TextEdit in Plain Text (TextEdit->Format->Make Plain Text).

    Type a few lines in by hand (don't cut-n-paste) and then run the workflow selecting this csv file.

    (If it is a problem with the line endings, then we can add a line to the script to do a conversion)

     

    If it still is not working, lets debug with "echo":

    add: echo before cp and mv and then at the end of each line add: >> $HOME/Desktop/debug.txt

    This will create a file on the Desktop showing us what is (or is not) happening

  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 1:25 PM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 1:25 PM in response to Tony T1

    I tried new .csv file it was same results.  I tried adding the echo command and I am not getting any debug files. Should look like this?

     

    Screen Shot 2016-02-08 at 1.20.43 PM.png

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 1:42 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 1:42 PM in response to jorrflv

    jorrflv wrote:

     

    I tried new .csv file it was same results.  I tried adding the echo command and I am not getting any debug files. Should look like this?

     

     

     

    Need to add >> before $HOME (this redirects stdout to a file):

         >> $HOME/Desktop/debug.txt

     

    Also, it should not matter, but remove the white space in the script:

     

    cd "$1"

    while read line        

    do

         OldImageName=${line%,*}

         NewImageName=${line#*,}

         echo cp "$OldImageName" "~/Documents/$NewImageName" >> $HOME/Desktop/debug.txt

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

    done < "$2"


    And post a few line of the csv file so that I can test it on my end


  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 1:59 PM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 1:59 PM in response to Tony T1

    Here are the sample csv lines:

    OldImageName,NewImageName

    Student1.pdf,Aizenberg_Samuel_0028

    Student2.pdf,Alexander_Pierce_0024

     

     

    Here are the results of the debug.txt file

     

    cp OldImageName /Users/orrj/Documents/NewImageName

    mv OldImageName NewImageName

    cp Student1.pdf /Users/orrj/Documents/Aizenberg_Samuel_0028

    mv Student1.pdf Aizenberg_Samuel_0028

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 2:25 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 2:25 PM in response to jorrflv

    ok, so it looks like it's not finding the file (it looks like it's not changing to the directory with the file)

    First, remove the debug (echo and >> $HOME/Desktop/debug.txt)

     

    Now, lets try the command by hand in Terminal:

    Open Terminal (Applications/Utilities/Terminal)

    cd to the Folder with the files (enter cd and then the folder name and then press enter.)

    You should see the folder name in the command prompt.

    For example, when I cd to Documents, I see: MacBook-Air:~/Documents Tony$

    (You can get a list of the directory by entering ls)

     

    When you are sure you changed to the correct directory:

    Copy the commands that's in the debug.txt file and paste into the Terminal Window:

         cp Student1.pdf /Users/orrj/Documents/Aizenberg_Samuel_0028

    and

         mv Student1.pdf Aizenberg_Samuel_0028

     

    If these copy and move as expected, then lets debug the cd command:

    Add echo to the 1st line:

         echo cd "$1" > $HOME/Desktop/debug.txt


    (note: the single > will overwrite the existing debug.txt file)

  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 2:47 PM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 2:47 PM in response to Tony T1

    I am trying to follow the bash script, but I can see right now that there will be an issue because the folders are on my desktop not in my documents folder... that would make a difference yes?

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 2:55 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 2:55 PM in response to jorrflv

    jorrflv wrote:

     

    I am trying to follow the bash script, but I can see right now that there will be an issue because the folders are on my desktop not in my documents folder... that would make a difference yes?

     

    Should not make a difference. 

    The first action selects the Folder that has the files.

    Now that I think about it, there is no need to debug the cd.

     

    Run the Workflow Again (with no echo commands), then when it completes, click the [Results] box and you'll see the directory that the workflow is changing to:

    In this example, I selected the Untitled Folder on my Desktop:

     

    Screen Shot 2016-02-08 at 5.52.56 PM.png

     

    When you click [Results] you should see the folder with the files

  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 3:30 PM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 3:30 PM in response to Tony T1

    awesome... The first line went.. Now when I try my other CSV file with the whole batch it is not moving on to the next record. none of the other files are renamed.


    Jon

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 3:48 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 3:48 PM in response to jorrflv

    jorrflv wrote:

     

    awesome... The first line went.. Now when I try my other CSV file with the whole batch it is not moving on to the next record. none of the other files are renamed.


    Jon

     

    ok, then that means that we need to convert the line ends.

    How was the CSV file created? (i.e was it from an excel file?)

     

    I'm going to guess that there are carriage returns in the file that needs to be removed.

    Add this line: NewImageName=${NewImageName//$'\r'}

     

    So the script should be:

     

    cd "$1"

    while read line        

    do

         OldImageName=${line%,*}

         NewImageName=${line#*,}

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

         cp "$OldImageName" "~/Documents/$NewImageName"

         mv "$OldImageName" "$NewImageName

    done < "$2"


    If this does not work, then we need to see what the line ends look like, so open Terminal, enter cat -e then space then drag the CSV file to the Terminal Window and press enter and then post a few lines.

  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 4:27 PM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 4:27 PM in response to Tony T1

    added the line no go... here are the results of terminal command

     

    OldImageName,NewImageName^MStudent1.pdf,Aizenberg_Samuel_0028^MStudent2.pdf,Alex ander_Pierce_0024^MStudent3.pdf,Anderson_Eric_8583^MStudent4.pdf,Apalategui_Aime e_7987^MStudent5.pdf,Attanasio_Nia_7722^MStudent6.pdf,Ayoub-Butcher_Angelina_009 9^MStudent7.pdf,Badger_Gage_7751^M

  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 4:28 PM in response to jorrflv
    Level 1 (0 points)
    Feb 8, 2016 4:28 PM in response to jorrflv

    Fyi I had to SUDO to make the command work. is that normal? or could that be the problem as well?

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 4:59 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 4:59 PM in response to jorrflv

    jorrflv wrote:

     

    added the line no go... here are the results of terminal command

     

    OldImageName,NewImageName^MStudent1.pdf,Aizenberg_Samuel_0028^MStudent2.pdf,Alex ander_Pierce_0024^MStudent3.pdf,Anderson_Eric_8583^MStudent4.pdf,Apalategui_Aim e e_7987^MStudent5.pdf,Attanasio_Nia_7722^MStudent6.pdf,Ayoub-Butcher_Angelina_00 9 9^MStudent7.pdf,Badger_Gage_7751^M

     

    Looks like just a CR (my guess was CRLF)

     

    This should work (translates CR to LF to file.tmp then deletes the tmp file when done):

     

    cd "$1"

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

    while read line        

    do

         OldImageName=${line%,*}

         NewImageName=${line#*,}

         cp "$OldImageName" "~/Documents/$NewImageName"

         mv "$OldImageName" "$NewImageName

    done < ~/Desktop/file.tmp

    rm ~/Desktop/file.tmp


  • by jorrflv,

    jorrflv jorrflv Feb 8, 2016 5:10 PM in response to Tony T1
    Level 1 (0 points)
    Feb 8, 2016 5:10 PM in response to Tony T1

    Bingo!  Got it..... Wow I would have never worked through that without your willingness to help.... This will be extremely helpful for me.

     

    Jon

  • by Tony T1,

    Tony T1 Tony T1 Feb 8, 2016 5:25 PM in response to jorrflv
    Level 6 (9,249 points)
    Mac OS X
    Feb 8, 2016 5:25 PM in response to jorrflv

    Glad it worked

first Previous Page 10 of 11 last Next