renaming finder items II

renaming finder items introduced a couple of ways to batch-rename files in the finder using either automator or applescript, depending on the complexity of the job. those examples involved altering the existing filename by replacing characters or removing parts of the name. here’s another renaming workflow that was developed when one set of filenames needed to be mapped to a completely different set — hundreds of barcodes named by ISBN needed to be renamed by product code.

Applescript Icon applescript is fantastic for this kind of work because renaming hundreds of files by hand is just about as tedious as it gets. as with most scripting tasks, there are myriad ways this problem could be tackled — here’s just one :

screen grab showing filenames in excel

this solution starts with two lists — original filenames and new filenames — in a single excel worksheet. for this version of the app to work correctly, the worksheet needs to be saved in the same folder as the files to be changed and the worksheet needs to be open in excel.

the app looks through the top level of the folder for files and folders matching the names in column A and changes the filenames to the corresponding ones in column B — simple. files that don’t match a name in the list are skipped. all original file extensions are maintained. files in subfolders remain untouched. here’s a before and after shot of test files :

screen grab showing files before and after name changes

RenameByList is a zipped folder containing the app and a sample excel file. to use it — update the excel spreadsheet to your requirements, put all your files in the same folder and double-click the app :

you can get a copy of RenameByList here

screen grab showing compiled script in script editor

for those interested in this sort of thing…
excel can be a bit of a pain to script. for example, this is the command to extract the entries in column A :
set mgANames to formula of range mgARange

instead of giving us a list of names :

{"9781234567890", "9781234567891", "9781234567892" ...

this command gives us a list of lists :

{{"9781234567890"}, {"9781234567891"}, {"9781234567892"} ...

that’s why we need to loop through the original list (mgANames) with this command — to convert the list of lists into a straight list :
set end of mgOriginalNames to item 1 of item x of mgANames

the other way would be to just loop through the cells in excel to make the list in one pass like this :

tell used range to set mgRowCount to count rows
repeat with x from 2 to mgRowCount
  set end of mgOriginalNames to formula of cell ("A" & x)
  set end of mgNewNames to formula of cell ("B" & x)
end repeat

but this script was developed and tested through script editor before being saved out as an app. and this second method is MUCH slower through script editor (although not much different when run as an app). try it yourself.


• related post : renaming finder items : renaming using automator or applescript.
• related post : renaming finder items III : renaming by creation date.

macgrunt icon