Applescript to rename images using excel file

I have a .jpg images (about 500) which I would like to rename using an excel file.

1) I have copied the original names onto the first column.




2)The second column consists of the names I want the files to be renamed to.




Any help would be greatly appreciated.

Posted on Aug 20, 2017 3:29 PM

Reply
13 replies

Aug 21, 2017 10:48 AM in response to Tony T1

Cool. I really appreciate this Tony and will give this one a try when I get home.


I have question and I can just do this creating a separate and additional column but would there be a way to write code that can get values of columns B through E and "concatenate" them separated by hyphens and use these as the new names?


For example and referencing the first line of the attachment: the new name will be "Altar-20 X 30-Airbrush & Acrylic-2002.JPG"

User uploaded file

Aug 21, 2017 11:13 AM in response to FelizEng

This should work, just change the first line ("~/Desktop/untitled\\ folder") to the directory with your jpg's.

(note the "\\" for a folder name with a space)


You should work on a copy, not the originals, so first copy all your jpg's to a temp folder (i.e. create a folder on the Desktop), and if the files are renamed properly, then erase the old jpg's and copy the renamed files to the original location. Also, you should backup your files first)



set theDirectory to ~/Desktop/untitled\\ folder"

tell application "Microsoft Excel"

tell active sheet

tell used range

set rc to count of rows

end tell

set theList to get value of range ("A1:B" & rc) as list

repeat with theItem in theList

try

do shell script "mv " & theDirectory & "/" & quoted form of item 1 of theItem & space & theDirectory & "/" & quoted form of item 2 of theItem

end try

end repeat

end tell

end tell

Aug 21, 2017 5:13 PM in response to FelizEng

FelizEng wrote:


Cool. I really appreciate this Tony and will give this one a try when I get home.


I have question and I can just do this creating a separate and additional column but would there be a way to write code that can get values of columns B through E and "concatenate" them separated by hyphens and use these as the new names?


For example and referencing the first line of the attachment: the new name will be "Altar-20 X 30-Airbrush & Acrylic-2002.JPG"




Yes, just need to build the string (and add .jpg to the end)


set theDirectory to "~/Desktop/untitled\\ folder"

tell application "Microsoft Excel"

tell active sheet

tell used range

set rc to count of rows

end tell

set theList to get value of range ("A1:E" & rc) as list

repeat with theItem in theList

try

do shell script "mv " & theDirectory & "/" & quoted form of item 1 of theItem & space & theDirectory & "/" & quoted form of (item 2 of theItem & "-" & item 3 of theItem & "-" & item 4 of theItem & ".jpg")

end try

end repeat

end tell

end tell

Aug 22, 2017 8:04 AM in response to FelizEng

FelizEng wrote:


It worked!! I changed the first line to


set theDirectory to "/Users/MyComputer/temp"


The only thing it's not copying the last column E. The year, maybe because of the format of numbers?


Good.

Not sure why it's not copying col E, but to debug, work with an excel file with only one row (to simplify things), then instead of the year, copy Col D to Col E to see if the script works then. If it does, then go back to the old format with the year, and try changing the format in Excel to text (⌘1, Text). If you still can't get it to work, see what command Applescript is showing in the command window at the bottom.

Aug 22, 2017 8:24 AM in response to FelizEng

So I tried the following and got it to work ( adding "-" item 5 of theItem) on the 12th Line :


set theDirectory to "/Users/MyComputer/temp"

tell application "Microsoft Excel"

tell active sheet

tell used range

set rc to count of rows

end tell

set theList to get value of range ("A1:E" & rc) as list

repeat with theItem in theList

try

do shell script "mv " & theDirectory & "/" & quoted form of item 1 of theItem & space & theDirectory & "/" & quoted form of (item 2 of theItem & "-" & item 3 of theItem & "-" & item 4 of theItem & "-" item 5 of theItem & ".jpg")

end try

end repeat

end tell

end tell


This did the trick and copies columns B through E.

Thanks My Friend. I greatly appreciate it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to rename images using excel file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.