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 6 of 11 last Next
  • by Amosiko,

    Amosiko Amosiko May 21, 2014 3:00 PM in response to Tony T1
    Level 1 (0 points)
    May 21, 2014 3:00 PM in response to Tony T1

    No worries thank you for your help though.

  • by Tony T1,

    Tony T1 Tony T1 May 21, 2014 3:17 PM in response to Amosiko
    Level 6 (9,249 points)
    Mac OS X
    May 21, 2014 3:17 PM in response to Amosiko

    If you want, you can give it a try in Applescript.

     

    This assumes that the image and csv file are on the Desktop.

    Open Applescript, paste this into it and change SND.csv to the name of your csv file:

     

    do shell script "
    cd ~/Desktop
    while read line          
    do          
         OldImageName=${line%,*}
         NewImageName=${line#*,}
         mv \"$OldImageName\" \"$NewImageName\"
    done < \"SND.csv\"
    "
    

     

     

    Then in Applescript, click Run

     


  • by Amosiko,

    Amosiko Amosiko May 22, 2014 6:46 AM in response to Tony T1
    Level 1 (0 points)
    May 22, 2014 6:46 AM in response to Tony T1

    Thanks Tony,

     

    For whatever reason I can't seem to get this to work.

     

    I have tried the Applescript and this is what I got.

     

    For whatever reason it doesn't change the file names. Thank you for all you efforts.

     

    tell current application

    do shell script "

    cd ~/Desktop

    while read line         

    do         

         OldImageName=${line%,*}

         NewImageName=${line#*,}

         mv \"$OldImageName\" \"$NewImageName\"

    done < \"ImageName.csv\"

    "

      --> ""

    end tell

    Result:

    ""

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 22, 2014 8:33 AM in response to Amosiko
    Level 5 (4,649 points)
    May 22, 2014 8:33 AM in response to Amosiko

    For whatever reason I can't seem to get this to work.

    Your csv file needs to be plain text (ascii text). Plain text may have old style mac line endings (CR), windows line endings (CRLF), or unix line endings (LF).

    Cocoa GUI applications can read and write to all those variations of plain text files but Unix (posix) applications need LF line endings in order to work properly.

    Excel on a Mac saves csv files with the old style mac line endings (CR- carrage return) unless you select the Windows csv option. In each option the saved csv file does not have the last line terminated. This can sometimes be an issue. Apple's Numbers application saves csv files with Windows line endings (CRLF).

    Your csv file more than likely needs to be converted to unix line endings (LF- line feed).

  • by Tony T1,

    Tony T1 Tony T1 May 22, 2014 8:48 AM in response to Amosiko
    Level 6 (9,249 points)
    Mac OS X
    May 22, 2014 8:48 AM in response to Amosiko

    This will fix the line endings per Mark's comment:

        

         NewImageName=$( echo "$NewImageName" | tr -d '\r' )

     

    So, try this:

     

    cd "$1"

    while read line    

    do    

         OldImageName=${line%,*}

         NewImageName=${line#*,}

         NewImageName=$( echo "$NewImageName" | tr -d '\r' )

         if [ -e "$OldImageName" ] ; then

              mv "$OldImageName" "$NewImageName"

         else

              echo "$OldImageName" >> ~/Desktop/Errors.txt

         fi

    done <"$2"

  • by Amosiko,

    Amosiko Amosiko May 22, 2014 9:12 AM in response to Tony T1
    Level 1 (0 points)
    May 22, 2014 9:12 AM in response to Tony T1

    When I changed the line endings it was able to change just the first image and non of the rest using Applescript.

     

    Tony,

     

    Using your I am getting this in the Error txt

     

    Old Image Name

    IMG_0032.JPG

     

    However non of the files change names. When I select allow multiple selections is when I am getting the jiberish.

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 22, 2014 9:46 AM in response to Tony T1
    Level 5 (4,649 points)
    May 22, 2014 9:46 AM in response to Tony T1

    This will fix the line endings per Mark's comment:

    Well it doesn't. The following csv file was produced from excel on a mac:

     

    cat -tve oldmac.csv
    0031.jpg,31-foo bar.jpg^M0048.jpg,48-bar of foo.jpg^M0053.jpg,53-bar of foo.jpg[KSH_93u+] $
    
    #Line isn't terminated
    
    wc -l oldmac.csv
         0 oldmac.csv
    
    while read line; do
    echo ${line%,*}
    done < oldmac.csv
    
    #No results
    

    A csv file saved as Window csv from excel on a mac:

     

    cat -tve windows.csv
    0031.jpg,31-foo bar.jpg^M$
    0048.jpg,48-bar of foo.jpg^M$
    0053.jpg,53-bar of foo.jpg[KSH_93u+] $
    
    #The last line isn't terminated
    
    wc -l windows.csv
         2 windows.csv
    
    
    while read line; do
    echo ${line%,*}          
     done < windows.csv
    0031.jpg
    0048.jpg
    
    #Last line isn't read
    

    In order to make your script work seamlessly, you are going to have to test the csv file for it's line endings and change them to unix line endings and terminate the last line.

  • by Tony T1,

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

    Just pipe to a tmp file and add a LF to the eof, then convert CR to LF:

     

     

    cd "$1"
    tFile=$(mktemp -t tmp)
    cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"
    while read line    
    do    
         OldImageName=${line%,*}
         NewImageName=${line#*,}
         if [ -e "$OldImageName" ] ; then
              mv "$OldImageName" "$NewImageName"
         else
              echo "$OldImageName" >> ~/Desktop/Errors.txt
         fi
    done < "$tFile"
    rm "$tFile"
    

     

    [Note: This works for both CR and CRLF endings as the extra LF in a csv file saved as Window csv from excel on a mac is ignored as we test for non-existant files.  However, this can be adapted by just changing tr '\r' '\n' to tr -d '\r' ]

     

    This bash script is written to be used in this Automator Workflow:

     

         Screen Shot 2014-05-23 at 9.01.46 AM.png

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 23, 2014 6:26 AM in response to Tony T1
    Level 5 (4,649 points)
    May 23, 2014 6:26 AM in response to Tony T1

    This is the approach that I came up with. I didn't test it in Automator. Feel free to borrow any of it.

     

    # Attempt to remove the tmp file if the script terminates early
    trap 'rm -f /tmp/csv$$; exit' 1 2 3 15
    
    # Sanity check and line endings conversion
    IS_ASCII=$(file FILE | sed 's/.*: //; s/, .*//')
    
    if [ "$IS_ASCII" = "ASCII text" ]; then
        # strings changes all line endings to LF
        # regardless of the current line endings
        # and terminates the last line 
        strings FILE > /tmp/csv$$
    else
        echo "Aborting script- problem with csv file"
        exit 1
    fi
    
    while read line; do
    
         . . . . . . .
    
    done < /tmp/csv$$
    
    rm -f /tmp/csv$$
    
  • by Tony T1,

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

    Basically, your testing that the user seleted a valid csv file and used strings instead of cat FILE | sed 'a\' | tr '\r' '\n' (Interesting use of strings, I'll have to remember that).  Not sure if strings is part of Mavericks, I think that the command line developer tools are now needed.  Also good use of file to determine line endings

  • by Tony T1,

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

    I like Mark's suggestion of testing with file:

     

    cd "$1"

    tFile=$(mktemp -t tmp)

    fType=$(file -b "$2")

    if [ "$fType" = "ASCII text, with CR line terminators" ]; then

         cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"

    elif [ "$fType" = "ASCII text, with CRLF line terminators" ]; then

         cat "$2" | sed 'a\' | tr -d '\r' > "$tFile"

    else

         echo "Problem with CSV file ($fType)" > ~/Desktop/Errors.txt

         rm "$tFile"

         exit 1

    fi

    while read line  

    do  

         OldImageName=${line%,*}

         NewImageName=${line#*,}

         if [ -e "$OldImageName" ] ; then

              mv "$OldImageName" "$NewImageName"

         else

              echo "$OldImageName" >> ~/Desktop/Errors.txt

         fi

    done < "$tFile"

    rm "$tFile"

  • by Tony T1,

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

    Edit: Need to do no conversion if LF's:

     

    cd "$1"

    tFile=$(mktemp -t tmp)

    fType=$(file -b "$2")

    if [ "$fType" = "ASCII text, with CR line terminators" ]; then

         cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"

    elif [ "$fType" = "ASCII text, with CRLF line terminators" ]; then

         cat "$2" | sed 'a\' | tr -d '\r' > "$tFile"

    elif [ "$fType" = "ASCII text" ]; then

         cat "$2" | sed 'a\' > "$tFile"

    else

         echo "Problem with CSV file ($fType)" > ~/Desktop/Errors.txt

         rm "$tFile"

         exit 1

    fi

    while read line 

    do 

         OldImageName=${line%,*}

         NewImageName=${line#*,}

         if [ -e "$OldImageName" ] ; then

              mv "$OldImageName" "$NewImageName"

         else

              echo "$OldImageName" >> ~/Desktop/Errors.txt

         fi

    done < "$tFile"

    rm "$tFile"

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 24, 2014 6:27 AM in response to Tony T1
    Level 5 (4,649 points)
    May 24, 2014 6:27 AM in response to Tony T1

    Yah, I just tested for "ASCII text" not the line endings because I knew that strings would take care of the rest. Bummer that strings is missing from a standard install. Personally, I'll attempt to find another solution than use multiple pipes. Here's what I'll offer-

     

    L_END=$(file -b FILE | sed 's/.*with //; s/ .*//')
    
    if [ "$L_END" = "CR" ]; then
       awk '{sub(/\r/,"\n"); print $0}' FILE > /tmp/csv$$
       #or use perl
       #perl -lpe 's/\r/\n/g' FILE > /tmp/csv$$
    elif [ "$L_END" = "CRLF" -o "$L_END" = "ASCII" ]; then
       awk '{gsub(/\r/,""); print $0}' FILE > /tmp/csv$$
       #or in perl
       #perl -lpe 's/\r//g' FILE > /tmp/csv$$
    else
       echo "Aborting script- problem with csv file"
       exit 1
    fi
    
  • by Tony T1,

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

    Personally, I'll attempt to find another solution than use multiple pipes.

     

    Why?  Nothing wrong with pipes (multiple or otherwise, and you're using one with sed) 

    I prefer tr over awk or perl over for a line-feed conversion.

    Also prefer to use mktemp for temp file creation.

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 24, 2014 6:52 AM in response to Tony T1
    Level 5 (4,649 points)
    May 24, 2014 6:52 AM in response to Tony T1

    I said multiple pipes not a single pipe. With multiple pipes you are forking to subshells in order to run each command. If possible I try to avoid them. The advantage to using awk or perl is that either will change the line endings and terminate the last line of the csv file if it isn't.

first Previous Page 6 of 11 last Next