File Rename

I really wanted to solve this one on my own, but it’s just inches out of my grasp.

This is a bit of an odd situation to explain, so stick with me. I record audio on film sets and the machine I use does a horrible job of naming files. If there is one mic per take, it creates one file with the name of the take. If there are multiple mics, it creates a folder, names that folder the name of the take and leaves the files inside named the number the mic is assigned.

The example below is an attempt at a graphical representation of two different takes. Take one with one mic. Take two with two mics.

[b]Scn_2A_001.wav

Scn_2A_002.pjt[/b] - This is actually a folder, not a bundle
1.wav
2.wav

What I need is this all in one folder:

Scn_2A_001.wav
Scn_2A_002-1.wav
Scn_2A_002-2.wav

SO! I have written a script as a droplet to do this and it mostly accomplishes it. Where I run into problems is that the single file sometimes throws an error, the “Sort” portion doesn’t move any files out of their “.pjt” folders, and if someone drops an “Audio Files” folder containing all these files onto the droplet, that name (obviously) gets passed down to the objects below.

property my_delimiter : "-"

on run
	display dialog "Drop a folder (or folders) you would like to process on the application icon.

File Renamer will automatically rename any nested files, prepending the files with the first word of the containing folder's name. Folder names will not be changed."
end run

on open my_input_folders
	set sort_items to button returned of (display dialog "Would you like these files consolidated to one folder?" buttons {"Don't Sort", "Sort"} default button 2)
	
	if sort_items is "Sort" then
		set my_sort_folder to choose folder with prompt "Where would you like them sorted?"
	end if
	
	repeat with each_folder in my_input_folders
		tell application "Finder"
			if class of each_folder is not folder then
				set my_source_folder_entire_contents to entire contents of folder each_folder
				repeat with each_item in my_source_folder_entire_contents
					--try
					if class of each_item is not folder and sort_items is "Sort" then
						set this_file to item each_item as alias
						
						set my_containing_folders_name to (name of container of each_item)
						set my_new_file_name to (first word of my_containing_folders_name & my_delimiter & (name of each_item)) as string
						set name of each_item to my_new_file_name
						tell application "Finder" to move this_file to folder my_sort_folder
						
					else if class of each_item is not folder and sort_items is "Don't Sort" then
						set my_containing_folders_name to (name of container of each_item)
						set my_new_file_name to (first word of my_containing_folders_name & my_delimiter & (name of each_item)) as string
						set name of each_item to my_new_file_name
					end if
					
					--end try
				end repeat
			end if
		end tell
	end repeat
	display dialog "Process Completed."
end open

Thoughts?

dennis

Martin,

For whatever reason, your message made it to my inbox, but was never posted on here. So for anyone reading this, here is the script Martin was kind enough to write for me.

property sortflag : true
property destfolder : (((path to desktop) as Unicode text) & "Test:") as alias

on open droppeditems
	repeat with droppeditem in droppeditems
		set iteminfo to info for droppeditem
		if folder of iteminfo then
			set foldername to name of iteminfo
			if foldername ends with ".pjt" then
				set dotoffset to offset of "." in foldername
				set foldernamepart to (characters 1 through (dotoffset - 1) of foldername) as Unicode text
				tell application "Finder"
					set wavfiles to (every file in droppeditem whose name ends with ".wav")
					repeat with wavfile in wavfiles
						set filename to name of wavfile
						set filenameparts to words of filename
						set name of wavfile to (foldernamepart & "-" & (item 1 of filenameparts) & ".wav")
						if sortflag is true then
							move wavfile to folder destfolder
						end if
					end repeat
				end tell
			end if
		else if not folder of iteminfo then
			set filename to name of iteminfo
			if filename ends with ".wav" and sortflag is true then
				tell application "Finder"
					move droppeditem to folder destfolder
				end tell
			end if
		end if
	end repeat
end open

It almost works, too! Yours is throwing the following error when it hits a folder with multiple files:

Can’t get <> “1.wav” of <> “Scene_05B_001.pjt” of <> “Audio” of <> “Desktop” […] of application “Finder”

This happens specifically after the very first file of the folder has been renamed. Meaning the error message refers to the file as “1.wav” but it’s on my system as “Scene_05B_001-1.wav”. Any thoughts on this?

Aside from that, it works perfect. I’m not sure why your syntax properly moved the file and mine didn’t, but c’est la vie

dennis

If I am reading things correctly you are renaming the file then trying to move it with the old file reference. You need the new file reference using the new name to move the file.

What probably will work is changing the finder block to something like this (not tested, might need to coerce filepath to string for it to work):

tell application "Finder"
	set wavfiles to (every file in droppeditem whose name ends with ".wav")
	repeat with wavfile in wavfiles
		set filename to name of wavfile
		set filepath to path of wavfile
		set filenameparts to words of filename
		set newname to (foldernamepart & "-" & (item 1 of filenameparts) & ".wav")
		set name of wavfile to newname
		if sortflag is true then
			move (filepath & newname) to folder destfolder
		end if
	end repeat
end tell

Seems a bit cumbersome to me overall. I might have time to play around with it tomorrow if no one else solves the problem for you.

Try this, not a droplet but seems to work. You will have to change the txt to wav here appropriate:
property sortFlag : true
property destFolder : “”

set TheItems to {choose folder}
set destFolder to choose folder
set OldDelim to AppleScript's text item delimiters

if destFolder is "" then choose folder with prompt "Choose destination folder for renamed files"
set OldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
repeat with AnItem in TheItems
	if (AnItem as string) ends with ":" then
		set FolderName to text item -2 of (AnItem as string)
		set AppleScript's text item delimiters to "."
		set NamePrefix to first text item of FolderName
		
		if sortFlag is true then
			tell application "Finder"
				set movedFiles to move (every file of AnItem) to destFolder with replacing
				repeat with aFile in movedFiles
					set name of aFile to NamePrefix & "-" & (name of aFile)
				end repeat
			end tell
		end if
		
	else if ((AnItem) as string) ends with ".txt" then
		tell application "Finder" to move AnItem to folder destFolder
	end if
end repeat
set AppleScript's text item delimiters to OldDelim

I’m getting some errors when turning it into a droplet with the move command which works good above, think it has something to do with the file reference that is being passed to it for AnItem:

property sortFlag : true
property destFolder : ""

on open TheItems
	if destFolder is "" then choose folder with prompt "Choose destination folder for renamed files"
	set OldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	repeat with AnItem in TheItems
		if (AnItem as string) ends with ":" then
			set FolderName to text item -2 of (AnItem as string)
			set AppleScript's text item delimiters to "."
			set NamePrefix to first text item of FolderName
			
			if sortFlag is true then
				tell application "Finder"
					set movedFiles to move (every file of AnItem) to destFolder with replacing
					repeat with aFile in movedFiles
						set name of aFile to NamePrefix & "-" & (name of aFile)
					end repeat
				end tell
			end if
			
		else if ((AnItem) as string) ends with ".txt" then
			tell application "Finder" to move AnItem to folder destFolder
		end if
	end repeat
	set AppleScript's text item delimiters to OldDelim
end open

Jerome,

Thanks for jumping into the fray. I’m having the same results on my machine. The script works fine when run inside script editor but when it’s made into a droplet, I’m getting an error similar to what happened with Martin’s. The only difference is it doesn’t actually pertain to a file. It just says:

Can’t get “” of <>

As for your earlier Finder tell block, I threw that into the script and got the following error:

Can’t get <> of <> “1.wav” of <> “Scene_05B_001.pjt” of <> “Audio” of <> “Desktop” […] of application “Finder”

Which is the same error before but with FTPc in the beginning.

A friend of mine said the double brackets show up when a particular application’s library can’t be found and can usually be solved by restarting it. However, since this now spans two of my machines and isn’t resolved with a restart, my next best guess is to assume that Finder’s dictionary is somehow damaged.

I’m running 10.5.5 on both an iMac and a MacBook. Jerome, what’s your system software version? And can anyone else with 10.4 or so weigh in on this? I’d love to eliminate this theory.

dennis

The snippet with the tell block I just put together, no testing. The other two I wrote on a computer running 10.4 and it seemed to work fine. It looks like the move statement is not working for some reason when run inside of the open handler. I did try coercing the folder name to a string and got similar results. I’m not sure of your workflow but you might be able to save the one that is working as an application and just launch and choose the folders to process, you can change the choose folder call to allow multiple selections.

Have you tried Jacques solution? Just looking at the code it looks like it has a problem in these two lines:

set name of wavfile to (foldernamepart & (get name of wavfile))
if sortflag then move wavfile to folder destfolder

It is setting a new name for the file, but using the old reference for the file when it is trying to move it and since it has a new name then it may not be able to find it to move to the new location. I didn’t test it so it might work.

Jacques,

Success! It’s performed exactly as required. Anyone who is using an Edirol Audio Recorder should add this script to their cannon. Here is the script with the GUI elements added back in.

property sortflag : true

on run
	display dialog "Drop a folder (or folders) you would like to process on the application icon.

File Renamer will automatically rename any nested files, prepending the files with the first word of the containing folder's name. Folder names will not be changed."
end run

on open droppeditems
	
	set sortitems to button returned of (display dialog "Would you like these files consolidated to one folder?" buttons {"Don't Sort", "Sort"} default button 2)
	
	if sortitems is "Sort" then
		set destfolder to choose folder with prompt "Where would you like them sorted?"
	end if
	
	repeat with droppeditem in droppeditems
		set {t_name, isAFolder} to {name, folder} of (get info for droppeditem without size)
		if isAFolder then
			if t_name ends with ".pjt" then
				set x to (the offset of "." in t_name) - 1
				set foldernamepart to text 1 thru x of t_name & "-"
				tell application "Finder"
					repeat with wavfile in (get document files in droppeditem whose name ends with ".wav")
						set wavfile to wavfile as alias
						set name of wavfile to (foldernamepart & (get name of wavfile))
						if sortflag then move wavfile to folder destfolder
					end repeat
				end tell
			end if
		else if t_name ends with ".wav" and sortflag then
			tell application "Finder" to move droppeditem to folder destfolder
		end if
	end repeat
end open

MANY, MANY thanks to those of you who helped. You have no idea the amount of time you have saved me and my associates!

dennis

I deleted the script shortly after posting the code, because it did not really come up to my own expectations. The code was somehow working, but not perfect. I guess it’s because I hate the Finder so much :lol:

So I am very glad that you finally got a working script saving you a lot of time :wink: