Yet another bulk renaming problem.. but a bit different.

Hello everybody.

This is my problem. I am a sound designer and have a ton of sound effects libraries on CD which I copied to my computer. Each CD has maybe 50-100 tracks on it and when I copied it to my hard drive, the file names are Track 01.aif to Track xx.aif, and I have lots of folders like these (one for each CD).

Now, it’s a real pain to try to find the sample I want because I have to go through the lists for each sample library and see if maybe there is a sample that I want then find the info for it and locate it based on the CD and track number. It would be so much better if I could rename all these files to be more descriptive so that I can use spotlight to quickly find samples. So, I downloaded the sample description lists from the internet and created a text file for a cd that contains how I want to name the files in order. The text file would look something like:


01 - Bird Cactus Wren - Rhythmic Chirps Close Perspective
02 - Bird Canary - Singing Close Perspective
03 - Birds Chickens - Cackling With Wing Flaps At Head of FX Some Movement Throughout
… etc …

And what the script would do is rename all the files in a folder such that
Track 01.aif → 01 - 01 - Bird Cactus Wren - Rhythmic Chirps Close Perspective.aif
Track 02.aif → 02 - Bird Canary - Singing Close Perspective.aif
Track 03.aif → 03 - Birds Chickens - Cackling With Wing Flaps At Head of FX Some Movement Throughout.aif
… etc …

Now, it would be important I think to note that the original file names are not always in Track xx.aif form… but sometimes “Audio Track xx.aif” or “PEVol1_xx.aif” etc… I probably should have named them constantly, but I didn’t… although renaming them to something constant should be easy.

Ok, so that’s what I want to do, and based on searches on these forums, I came up with the following code (PS, I never used applescript before):


-- Get the file with the list of names
on open myItems
	set theFile to (choose file)
	set theEOF to get eof of theFile
	set theIncrement to 0
	
	repeat with eachItem in myItems
		set aLine to read theFile from theIncrement before ASCII character 10
		set countof_aLine to the count of aLine
		
                -- For some strange reason this is necessary
		if theIncrement is 0 then
			set theIncrement to 1
		end if
		
		set theIncrement to theIncrement + (countof_aLine + 1)
		
		tell application "Finder"
			set name of eachItem to aLine & ".aif"
		end tell
		
	end repeat
end open

So, I saved this as an application and dropped all the files from one folder onto it, and then picked the text file with all the new names and it renamed all the files… except mixed all the file names up. Of course, me being dumb I forgot to backup the files and had to re rip that CD (no biggie). I can’t figure out how to make this work… any help is much appreciated.

Yes. When you drop items onto your app, you don’t receive a ordered list.
You can try this:

on open {dir} --> drop folder containing items to rename
	--> pick new names from .txt
	set namesList to paragraphs of (read alias "path:to:file_containing_OK_names.txt")
	
	--> pick list of items to rename
	set fileNames to list folder dir without invisibles

	--> do it
	repeat with i from 1 to count fileNames
		tell app "Finder" to set name of item (fileNames's item i) of dir to (namesList's item i)
	end repeat
end open

Don’t forget to add the file extension:


on open {dir} --> drop folder containing items to rename
   --> pick new names from .txt
   set namesList to paragraphs of (read alias "path:to:file_containing_OK_names.txt")
   
   --> pick list of items to rename
   set fileNames to list folder dir without invisibles

   --> do it
   repeat with i from 1 to count fileNames
       tell app "Finder" to set name of item (fileNames's item i) of dir to (namesList's item i) & ".aif" as string--Add ext.
   end repeat
end open

SC

My shareware app Name those Files! makes it easy to rename files and includes a feature for importing file names from a text file. Just drop the folder with the files to be renamed on NtF! and then, from the action menu, selected “Import Names…” (see the included documentation for more information).

Jon