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

    Tony T1 Tony T1 May 24, 2014 7:54 AM in response to Mark Jalbert
    Level 6 (9,249 points)
    Mac OS X
    May 24, 2014 7:54 AM in response to Mark Jalbert

    And I said multiple or otherwise.

    L_END=$(file -b FILE | sed 's/.*with //; s/ .*//')

     

    Aren't you running 2 shells here? One to execute file, and then another piping to sed?


  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 24, 2014 12:03 PM in response to Tony T1
    Level 5 (4,649 points)
    May 24, 2014 12:03 PM in response to Tony T1

    In bash you would be running 3 sub-shells (There's command substition in that statement). I believe in Zsh and Ksh only one sub-shell.

  • by doala,

    doala doala Jun 29, 2014 7:59 PM in response to userremoved
    Level 1 (0 points)
    Jun 29, 2014 7:59 PM in response to userremoved

    Hey everyone I have a similar question about renaming different files using applescript (imo better suited then automator, for the task) from a set list of "names" inside a file (can be either a txt, o csv - idc which format this list is saved, as long as I get the job done).


    Below is the link for my question - which I further explain...

    https://discussions.apple.com/thread/6424131




    Tnx a lot!

  • by Ohms238,

    Ohms238 Ohms238 Jul 23, 2014 6:25 PM in response to twtwtw
    Level 1 (0 points)
    Jul 23, 2014 6:25 PM in response to twtwtw

    Hi sorry for my ignorance but it's not working for me... Here's my setup:

     

    set theCSVData to paragraphs of (read "/Users/ohms/Desktop/data/newnames.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 "/Users/ohms/Desktop/EEPI.VariableData/oldFileName" to newFileName

      end tell

    end repeat

    set my text item delimiters to oldTID

    set

    I get this error:

    System Events got an error: Can’t set file "oldFileName" of folder "/Users/ohms/Desktop/EEPI.VariableData/oldFileName" to "newFileName".

     

    My csv file has two columns:

    oldFileName and newFileName

  • by Tony T1,

    Tony T1 Tony T1 Jul 24, 2014 6:27 AM in response to Ohms238
    Level 6 (9,249 points)
    Mac OS X
    Jul 24, 2014 6:27 AM in response to Ohms238

    You need to use POSIX file when using "/"

      read (POSIX file "/Users/ohms/Desktop/data/newnames.csv")

      folder  (POSIX file "/Users/ohms/Desktop/EEPI.VariableData/oldFileName") to newFileName

     

    See: http://www.satimage.fr/software/en/smile/external_codes/file_paths.html

  • by Cooker8,

    Cooker8 Cooker8 Jun 11, 2015 8:42 AM in response to Tony T1
    Level 1 (4 points)
    Notebooks
    Jun 11, 2015 8:42 AM in response to Tony T1

    Hi Tony T1,

     

    First off, thank you so much for all the info you've posted on here, after a LOT of hours trying to run scripts using Applescript and Terminal - and failing I was able to set up a workflow in Automator successfully. I just have a question about a possible modification. In my circumstance I have a CSV file with a complete list of over 8000 Old part numbers and their corresponding New part numbers. I then have a folder containing a series of images all with the Old Part number. The problem is there are discrepancies between the part number list and my image files (I may have images that longer exist - that part number has been removed from the system for example). Is there a way to move the images that successfully get renamed to a new folder? Then I will be able to tell which images are now non-existent and can be removed and which ones I can upload to our new site?

     

    I hope that makes sense, let me know if you require any other information - and again THANK YOU.

  • by Tony T1,

    Tony T1 Tony T1 Jun 11, 2015 1:57 PM in response to Cooker8
    Level 6 (9,249 points)
    Mac OS X
    Jun 11, 2015 1:57 PM in response to Cooker8

    Are you always moving to the same new folder?

    If so, you can easily add the new folder to the script.

    As this thread is now 7 pages, I'm not sure which part that you are using, however to move to a folder called NewFolder on the Desktop:

     

    Change the line that moves the file:

    from:

        mv "$OldImageName" "$NewImageName"

    to:

        mv "$OldImageName" "$HOME/Desktop/NewFolder/$NewImageName"

     

     

     

    You can also test if the file exists, and if not, print the missing name to a txt file:

     

    #!/bin/bash

     

         cd ~/Documents

         while read line          

         do          

              OldImageName=${line%,*}

              NewImageName=${line#*,}

              if [ ! -e "$OldImageName" ] ; then

                   echo "$OldImageName >> "HOME/Desktop/MissingFiles.txt"

              else

                   mv "$OldImageName" "$NewImageName"

              fi

         done <"TheFile.csv"


    This assumes that Documents is the Folder where both the files reside and the csv named TheFile.csv)

  • by Cooker8,

    Cooker8 Cooker8 Jun 11, 2015 2:05 PM in response to Tony T1
    Level 1 (4 points)
    Notebooks
    Jun 11, 2015 2:05 PM in response to Tony T1

    Hi Tony,

     

    Thank you for your reply and sorry - I did not realize that my post did not reply directly to the post I was referencing.

     

    This should hopefully be a one time operation. We changed our entire inventory over to a new set of part numbers and so I need to rename all my image files for our website. I was using the part of the thread where you set up an Automator Workflow and inserted a shell script I thought I had it working but have since realized I do not. It says workflow complete - but doesn't do anything. No files are altered. I inserted the piece about moving the files to a new folder. Here are two screen shots - one of my csv file and one of my Workflow set up. If you don't mind would you take a look and see if you can determine what I'm doing wrong... I read through the whole thread in case there were edits or modifications so that I would hopefully not be wasting anyone's time but at this point I'm in need of help! Much appreciated. Thanks again!

     

    CSV.jpg

     

    In case you can't read it from the image below - this is the shell script used (with your move to new folder mod)

     

    cd "$1"

     

         while read line        

     

         do        

     

             OldImageName=${line%,*}

     

             NewImageName=${line#*,}

     

             mv "$OldImageName"

            "Desktop/NEWFOLDER/$NewImageName"

     

         done <"$2"

    Workflow.jpg

  • by Tony T1,

    Tony T1 Tony T1 Jun 11, 2015 5:03 PM in response to Cooker8
    Level 6 (9,249 points)
    Mac OS X
    Jun 11, 2015 5:03 PM in response to Cooker8

    Not sure if this is the problem but the mv should be on one line:

       mv "$OldImageName"  "Desktop/NEWFOLDER/$NewImageName"

     

    If this does not work, we need to see what the script is doing with echo.

    To debug, change the line to:

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

     

    Run, and then look at the debug.txt file on the desktop to see what the script is trying to do with the mv.

  • by Cooker8,

    Cooker8 Cooker8 Jun 12, 2015 6:58 AM in response to Tony T1
    Level 1 (4 points)
    Notebooks
    Jun 12, 2015 6:58 AM in response to Tony T1

    Hi Tony,


    I tried both of your suggestions, moved the mv all to one line and the script ran and workflow complete - but again nothing happened.

    When I ran the debug script nothing happened either - workflow complete but no txt file to be found anywhere.

     

    Thanks

  • by Tony T1,

    Tony T1 Tony T1 Jun 12, 2015 5:55 PM in response to Cooker8
    Level 6 (9,249 points)
    Mac OS X
    Jun 12, 2015 5:55 PM in response to Cooker8

    You should be getting the debug.txt file on the Desktop with:

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

     

  • by Cooker8,

    Cooker8 Cooker8 Jun 12, 2015 6:01 PM in response to Tony T1
    Level 1 (4 points)
    Notebooks
    Jun 12, 2015 6:01 PM in response to Tony T1

    I've multiple times and haven't gotten anything. I even tried searching my mac for the debug.txt file just in case it was hiding somewhere. But no luck. Soooo I really don't know what's going on. Thanks anayway, appreciate your efforts to try and help me.

  • by Tony T1,

    Tony T1 Tony T1 Jun 13, 2015 6:26 AM in response to Cooker8
    Level 6 (9,249 points)
    Mac OS X
    Jun 13, 2015 6:26 AM in response to Cooker8

    It's possible that the script is not reading the .csv file.

    Is the .csv file a plain text file?

     

    To test, replace

      done <"$2"

    with

      done <"$HOME/Desktop/files.csv"

     

    and then put a copy of the files.csv on the Desktop

    and also put copies of a few of the files on the Desktop

  • by Tony T1,

    Tony T1 Tony T1 Jun 13, 2015 6:33 AM in response to Cooker8
    Level 6 (9,249 points)
    Mac OS X
    Jun 13, 2015 6:33 AM in response to Cooker8

    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.

  • by williamleon,

    williamleon williamleon Oct 18, 2015 12:28 PM in response to Tony T1
    Level 1 (4 points)
    iPhone
    Oct 18, 2015 12:28 PM in response to Tony T1

    I really appreciate you putting this script in for Automator because I have the same issue.  I think I have copied the form you laid out in my Automator but after I run the program - it apperars to run ok (all green checks) but I can't find the resulting images with their new names. this is what my Automator looks like. -- I have two file folders on my desktop - one named Old Image Names (this contains 10 Jpeg files with their old names) and a second folder named New Image Names ( this contains a single CSV file named statestudentnumberonly  with 10 numbers )

    Thanks for any help you can offer.

     

    Screen Shot 2015-10-18 at 3.11.40 PM.png

first Previous Page 7 of 11 last Next