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 3 of 11 last Next
  • by Tony T1,

    Tony T1 Tony T1 Mar 26, 2014 1:24 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    Mar 26, 2014 1:24 PM in response to bj49wd

    That is strange.

    What happens if you:

     

         while read line        

         do        

             OldImageName=${line%,*}

             NewImageName=${line#*,}

             cp "$OldImageName" "$NewImageName"

             rm $OldImageName"

         done <"$2"

     

    or:

     

         while read line        

         do        

             OldImageName=${line%,*}

             NewImageName=${line#*,}

             ditto "$OldImageName" "$NewImageName"

             rm $OldImageName"

         done <"$2"

     

    ditto might work as it preserves extended attributes (but mv should have also)

  • by bj49wd,

    bj49wd bj49wd Mar 26, 2014 1:35 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    Mar 26, 2014 1:35 PM in response to Tony T1

    neither of those worked.

    Not sure if this is relevant, but the line: rm $OldImageName" didn't have a preceeding " so I added one. (The script as written was throwing an error.)

     

    So, rm "$OldImageName" allowed it to run correctly. However... the files produced are still "documents" and not .wav files.

  • by Tony T1,

    Tony T1 Tony T1 Mar 26, 2014 1:38 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    Mar 26, 2014 1:38 PM in response to bj49wd

    Last try with rsync:

     

         while read line       

         do       

             OldImageName=${line%,*}

             NewImageName=${line#*,}

             rsync -a  "$OldImageName" "$NewImageName"

             rm "$OldImageName"

         done <"$2"

  • by bj49wd,

    bj49wd bj49wd Mar 26, 2014 2:03 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    Mar 26, 2014 2:03 PM in response to Tony T1

    Bah! Same result.

    I'm wondering about the shell script, and possible typos... should

    NewImageName=

         ${line#,*}

    and not

         ${line#*,}

     

    UPDATED:

     

    Clearly not. Making this change just adds the two names together.

    I'm super novice script man.

     

    Message was edited by: bj49wd

  • by bj49wd,

    bj49wd bj49wd Mar 27, 2014 1:34 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    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.

    Screen Shot 2014-03-27 at 1.30.34 PM.png

    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.

    Screen Shot 2014-03-27 at 1.30.45 PM.png

    Food for thought.

  • by Tony T1,

    Tony T1 Tony T1 Mar 27, 2014 3:33 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    Mar 27, 2014 3:33 PM in response to bj49wd

    Ah!  Good catch

     

    Adding this line should get rid of the carriage return (if it's a UNIX \n return)

     

         NewImageName=${NewImageName//\\n}

     

    So try:

     

         while read line       

         do       

             OldImageName=${line%,*}

             NewImageName=${line#*,}

             NewImageName=${NewImageName//\\n}

             mv "$OldImageName" "$NewImageName"

         done <"$2"

  • by bj49wd,

    bj49wd bj49wd Mar 27, 2014 3:48 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    Mar 27, 2014 3:48 PM in response to Tony T1

    Tried it. The return is still present in the resulting filename.

  • by Tony T1,

    Tony T1 Tony T1 Mar 27, 2014 3:56 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    Mar 27, 2014 3:56 PM in response to bj49wd

    In the script, add:

         echo "$NewImageName" > ~/Desktop/text.txt

         cat -e ~/Desktop/text.txt

     

    And Run the script in Terminal (not Automator)

    This will show if it's \n or \r\n


     

  • by bj49wd,

    bj49wd bj49wd Mar 27, 2014 4:05 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    Mar 27, 2014 4:05 PM in response to Tony T1

    Hmm... here is the text file... still has the return in it.

    Screen Shot 2014-03-27 at 4.02.45 PM.png

    I ran the cat -e ~/Desktop/text.txt in a Terminal window and returned this:

    Screen Shot 2014-03-27 at 4.04.15 PM.png

  • by Tony T1,

    Tony T1 Tony T1 Mar 27, 2014 4:13 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    Mar 27, 2014 4:13 PM in response to bj49wd

    ^M is \r, so try

     

        NewImageName=${NewImageName//\\r}

    or

     

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

  • by bj49wd,

    bj49wd bj49wd Mar 27, 2014 4:18 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    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!

  • by Tony T1,

    Tony T1 Tony T1 Mar 27, 2014 4:58 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    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"

  • by Hiroto,

    Hiroto Hiroto Mar 27, 2014 4:51 PM in response to bj49wd
    Level 5 (7,341 points)
    Mar 27, 2014 4:51 PM in response to bj49wd

    Hello

     

    In oder to remove trailing CR from the variable, you may use:

     

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

     

    Regards,

    H

  • by bj49wd,

    bj49wd bj49wd Apr 1, 2014 3:39 PM in response to Tony T1
    Level 1 (29 points)
    Notebooks
    Apr 1, 2014 3:39 PM in response to Tony T1

    Tony For The Win!!

     

    $'\r' was the ticket. That worked. I have no idea how that return got IN to the filename, but you got it OUT. Thank you so much!

     

    Thanks for sticking this one out.

  • by Tony T1,

    Tony T1 Tony T1 Apr 1, 2014 4:04 PM in response to bj49wd
    Level 6 (9,249 points)
    Mac OS X
    Apr 1, 2014 4:04 PM in response to bj49wd

        Usually (but not always) the \r comes from Windows files.

first Previous Page 3 of 11 last Next