Can Automator rename files to this extent?

Hi guys.

Wow Automator has saved me a ton of time. Rather than manually place ~2000 images into InDesign, it did it for me in minutes :slight_smile:

My dilemma right now though is that I have about 2000 files, that correspond to a person. So I have an excel table that says the file name, and person’s last and first name.
The files were named arbitratily, however, so when I open the folder containing the 2000 files, the first person on my list might be near the bottom. Is there anyway I can possibly use Automator to take, say the names in my excel list, and rename the files they correspond to? This seems like a total shot in the dark but I can’t think of any other way to approach the problem.

Any help would be greatly appreciated.

Thanks!

Hi,

this is a plain Applescript, which should do it.
The script assumes three columns, the file names in the first, the last names in the second and the first names in the third column e.g.
myFile.jpg Doe John
If you have only two columns omit & “_” & item 3 of i in line 10
The script uses a binary search, which works much faster with large lists than a linear search


set excelFile to choose file with prompt "Choose the Excel file" of type {"XLS6", "XLS7", "XLS8"}
set theFolder to choose folder with prompt "Choose the folder containing the files to rename"
tell application "Microsoft Excel"
	open excelFile
	set P to value of used range of active sheet
end tell
set {fileNames, peopleNames} to {{}, {}}
repeat with i in P
	set end of fileNames to item 1 of i
	set end of peopleNames to item 2 of i & "_" & item 3 of i
end repeat
set theFiles to list folder theFolder

repeat with oneFile in theFiles
	set idx to BinarySearch(fileNames, contents of oneFile)
	set x to item idx of peopleNames
	tell application "Finder" to set name of file oneFile of folder theFolder to item idx of peopleNames
end repeat

on BinarySearch(theList, value)
	local low, mid, high
	script o
		property l : theList
	end script
	
	set low to 1
	set high to (count theList)
	repeat while (low ≤ high)
		set mid to (low + high) div 2
		if ({value} is in items low thru mid of o's l) then
			set high to mid - 1
		else
			set low to mid + 1
		end if
	end repeat
	
	if (item low of o's l is value) then return low
	return false
end BinarySearch

Thank you very much for the help :slight_smile:

I have run into some problem, however (I’m not too well versed with AppleScript).

For the line “set P to value of used range of active sheet” it said “Expected end of line, etc. but found class name.”
So I changed it to A1 (I tried a few other things but they results in syntax errors). It accepted that, but once I ran it it said highlighted open excelFile and said "Microsoft Excel got an error: ‘’ could not be found.__Check the spelling of the file name, and verify that the file location is correct.

If you are trying to open "

I’m a bit stuck here with the part.

Thanks again for your help, I really appreciate it!

I’ve tested the script successfully with Office 2004.
Maybe you use Office v.X, which has a different dictionary.

Yes, I am using Office X. Sorry, I would’ve said so earlier but I didn’t realize there was a difference.

Thanks.

I think a friend has Office 2004, so I’ll move my files to her computer. Thanks again, you have no idea how much time you’ve saved me :slight_smile:

Sorry to bother you again, but I have a slightly problem. It says nothing for a minute, and then it says “Can’t get item 1037 of the range of my spreadsheet”. I have 1036 rows of information. I tried playing around with the script but couldn’t get it to stop giving me that error, or not crash :slight_smile:

Thanks again!

I’m sorry, I haven’t Office v.X installed so I can’t neither read the dictionary nor test anything.

Edit: I installed v.X on a test volume and the solution is quite simple.
The Excel block must be

tell application "Microsoft Excel"
   Open excelFile as text
   set P to value of UsedRange of ActiveSheet
end tell

and change also this line

set theFiles to list folder theFolder without invisibles

I really hate to do this since you’ve given me so much help already, but I still can’t seem to get it to work for me.

It says “Can’t get item 1037 of {“1Y11007.TIF”, “1Y11117.TIF”, “1Y05021.TIF”, “1Y04006.TIF”, “1Y11018.TIF”, “1Y02022.TIF”, “1Y04009.TIF”…” and highlights value after I click ok.

if (item low of o’s l is value) then return low
return false
end BinarySearch

maybe a terminology clash with a scripting addition,
try this modified search routine

on BinarySearch(theList, theValue)
   local low, mid, high
   script o
       property l : theList
   end script
   
   set low to 1
   set high to (count theList)
   repeat while (low ≤ high)
       set mid to (low + high) div 2
       if ({theValue} is in items low thru mid of o's l) then
           set high to mid - 1
       else
           set low to mid + 1
       end if
   end repeat
   
   if (item low of o's l is theValue) then return low
   return false
end BinarySearch

Thanks for the quick reply, although unfortunately I still get the same error. It keeps on saying that it can’t get item 1037. I have 1036 rows of data (so from 1037 onwards is just blank).

and the number of rows matches exactly the number of files in the folder?

Yes it does.

Then there must be a problem with the search subroutine
Try this slightly modified one


on BinarySearch(TheList, theValue)
	script o
		property l : TheList
	end script
	
	set low to 1
	set high to (count TheList)
	repeat until (low = high)
		set mid to (low + high) div 2
		if ({theValue} is in items low thru mid of o's l) then
			set high to mid
		else
			set low to mid + 1
		end if
	end repeat
	
	if (item low of o's l is theValue) then return low
	return false
end BinarySearch

It works! :slight_smile:

Thank you so very much, I even can’t say how of a help you’ve been!

good, finally

You’re welcome :slight_smile:

Stefan

Do you have a version of this script that works with office for mac 2004 and os x 10.4.11?

Model: g4
Browser: Safari 525.22
Operating System: Mac OS X (10.4)

Hi Hassan,

the origin version should work with Excel 2004,
but I answered another post of you in OS X forum

I’m in need of the same process. I have “Itemcode” and “Filedescription” in place of names in the Excel file. I adjusted your script, but I get the error:
Can’t get item false of {“filedescription”, “082653_Poster1”, “082653_Poster2”, “082653_Poster3”}.
Highlighted line item:
set x to item idx of filedescription

Is there something I can do to fix this?

THANK YOU

if the binary search returns boolean false, there is no matching item in the list