script to rename files - rather long, sorry
script to rename files - rather long, sorry
- Subject: script to rename files - rather long, sorry
- From: brian <email@hidden>
- Date: Wed, 07 Aug 2002 00:42:55 -0400
A colleague of mine has presented me with a problem where I believe
Applescript can flex its muscles and save the day.
the problem is this:
He spends hours weekly renaming image files from whatever their old name was
to a new name. The old name is typically a vendor part number, or some
other esoteric piece of data that has meaning to the original creator. The
new name has meaning to my organization. for instance file 'abc.jpg' needs
to be renamed 'p123456.jpg'.
Today he accomplishes this task by manually researching and naming the files
armed with countless spreadsheets detailing the translation. Typically
there are three key pieces to this puzzle, the new name (our SKU), the
vendor part number (or whatever) which is the rosetta stone in each equation
and the filenames of the images. In the data sources, spreadsheets
typically, the SKU, vendor part number and countless other bits of data are
contained in the same row and my colleague needs only figure out which field
was used as the convention when naming the files originally and extrapolate
to rename to our SKU.
I should note that there are often many exceptions to the rules of this
process for which even Applescript offers no salvation, but where I want to
make the most impact is in the area where the data supports automation. All
exceptions where the automation fails should be shuttled to the side for
manual processing.
My first attempt at automating this is a far cry from being finished, but I
suspect it may be beyond my capabilities so I'm turning to the list for any
help you night be able to offer.
What I've built (script at the end of this message) is a script that is run
as an applet that takes in tab delimited text files saved from an Excel
document where column a contains the new names and column b contains the
existing image filenames. Point it to a folder of source images and tell it
where the renamed files should be deposited. All files it can rename based
on the data are copied, renamed and the source files are labeled in their
original location. Those that are not labeled are exceptions that must be
researched manually.
This script works in it's current form if the number of source files is
greater than or equal to the number items the data instructs how to rename,
but not, I theorize, if there are fewer (perhaps an additional try/end try
wrapper?)
Also, no doubt my ignorance at fault here the period separating file type
extensions from the main body of the file name stymied my TID manipulations
to no end, so to get around it I took the easy way out by searching in excel
for every '.' (period) and replacing it with '_' (underbar) so the items in
column b read 'abc_jpg' instead of 'abc.jpg' I set everything back the right
way in the script so file refs work, but I'm curious if someone could
enlighten me what I'm not quite doing right ? My initial attempts to get
text item 1 of, text item 2 of and text item 3 of (text item 1,2 and 3,
word.., paraghraph...etc) all failed miserably with the closest netting the
extension as an item. e.g. I got a list of {new name1, old name1, jpg, new
name2, old name2, jpg...}
Finally, the success of this tool in its current form is predicated on my
colleague's ability to do vlookups or several of a hundred other gyrations
to get the existing filenames lined up with the new filenames which testing
has shown can be done (he needs to do it now to do it manually after all).
I'd prefer if it weren't necessary to get everything lined up quite so
perfectly instead employing some pattern matching or regex type stuff to
allow him to take the data as it's provided with SKU and vendor part number
already lined up and point it to a folder of files whose names might contain
the vendor part number, or a reasonable facsimile of it. I'm not expecting
miracles, but if this script can be extended and become something more than
just a glorified typist that would really be a huge plus. In it's current
form it's already proving useful, I'm just looking for extra bang for the
buck. As impetus and proof of concept I have been told (by yet another
colleague) that he has a very similar piece of functionality built using
Access. My desire has never been greater.
Any help greatly appreciated. Please feel free to email any replies as I'm
subscribed to this list on digest mode. email@hidden and/or
email@hidden
thanks in advance!!!
brian
--script follows, watch line wraps
--adding an extra return where I see it happening
tell application "Finder"
activate
set AppleScript's text item delimiters to return
set source_list to choose file with prompt "PLEASE SELECT THE FILE NAME TAB
DELIMITED FILE"
set source_images_dir to choose folder with prompt "PLEASE SELCT THE SOURCE
IMAGE DIRECTORY"
set source_images to every file of source_images_dir
set dest_image_dir to choose folder with prompt "PLEASE SELECT THE
DESTINATION DIRECTORY"
set the source_info to (read source_list)
set source_items to every paragraph of source_info as text
set i to 1
set the new_name_list to {}
set the old_name_list to {}
repeat until i > the number of paragraphs of source_items
set pair_value to text item i of source_items as text
--returns couplets - new name and old name
set AppleScript's text item delimiters to tab
set first_value to the first text item of pair_value -- the new name
set the new_name_list to (new_name_list & first_value)
set second_value to the second text item of pair_value
(*
the next bit takes the names I massaged in excel to abc_jpg
and sets them back to the more correct abc.jpg
*)
set AppleScript's text item delimiters to "_"
set old_name_a to the first text item of second_value
set old_name_b to the second text item of second_value
set the old_name_list to old_name_list & (old_name_a & "." & old_name_b)
set i to i + 1
set AppleScript's text item delimiters to return
end repeat
--begin copying and renaming
set list_item to 1
repeat with each_item in old_name_list
set old_name_final to item list_item in old_name_list
set new_name_temp to item list_item in new_name_list
(*
try block allows there to be more files in the
source directory than the data knows how to move and rename
*)
try
copy file old_name_final of folder source_images_dir to folder
dest_image_dir
set the name of file old_name_final of folder dest_image_dir to
new_name_temp
set the label index of file old_name_final of folder source_images_dir to 3
set list_item to list_item + 1
end try
end repeat
--add prefix and extension
repeat with i in dest_image_dir
set the_type to the file type of (info for file i)
set previous_name to the name of i
set new_name_final to "p" & previous_name
if the_type is "JPEG" then
set new_name_final to new_name_final & ".jpg"
end if
if the_type is "8BPS" then
set new_name_final to new_name_final & ".psd"
end if
if the_type is "GIFf" then
set new_name_final to new_name_final & ".gif"
end if
if the_type is "BMP " then
set new_name_final to new_name_final & ".bmp"
end if
if the_type is "EPSF " then
set new_name_final to new_name_final & ".bmp"
end if
if the_type is "PICT " then
set new_name_final to new_name_final & ".pct"
end if
if the_type is "PNGf" then
set new_name_final to new_name_final & ".png"
end if
set the name of i to new_name_final
end repeat
display dialog "Done" giving up after 5
end tell
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.