How do I handle complex File Renaming?

I have a bunch of files which has names like this:

Buster Keaton, Fatty Arbuckle - His Wedding Night (french titles) - 1917 - (DivX 4.1.2-214 kbps).avi

and I need to change the names to like this (year after performer(s):

Buster Keaton, Fatty Arbuckle - 1917 - His Wedding Night (french titles) - (DivX 4.1.2-214 kbps).avi

Has anybody tried to make this kind of file name change with AppleScript or is it even possible to do with AppleScript?

Any help for this curious problem would be highly appreciated!

Kari

I’d use [shameless plug] Name those Files! You can use GREP replacements so you can set it to just use the original name with a GREP search & replace. Search for

([^-]± )([^-]± )([^-]± )(.*)

and replace with

1324

and you should be set.

Jon

!!!

Wow. As a guy who’s still more “classic” than “OS X”, these kinds of long filenames still throw me.

You got a nice, natural delimiter in there: space & hyphen & space, so let’s play with that:

set aviFolder to choose folder

set astids to AppleScript's text item delimiters -- save
try
	set AppleScript's text item delimiters to " - "
	
	tell application "Finder"
		
		set aviFiles to every file of aviFolder whose name ends with ".avi"
		
		repeat with aviFile in aviFiles
			
			set nameParts to every text item of (get name of aviFile)
			
			--	The 2-item shuffle, as they say...
			--
			set temp to item 2 of nameParts
			set item 2 of nameParts to item 3 of nameParts
			set item 3 of nameParts to temp
			
			set name of aviFile to nameParts as string
			
		end repeat
	end tell
on error m number n from f to t partial result p
	set AppleScript's text item delimiters to astids -- restore
	error m number n from f to t partial result p
end try
set AppleScript's text item delimiters to astids -- restore

jonn,

I already downloaded you script and tried to figure out the proper search pattern, but without no luck. Unfortunately the search pattern that you provided does dot work either.

AdmiralNovia,

Your script works fluently. Thanks!

Here is another working solution (a bit slower than AdmiralNovia’s) from other forum for those who are interested:

on open theseFiles 
tell application "Finder" 
set {tid, my text item delimiters} to {my text item delimiters, " - "} 
repeat with theFile in theseFiles 
set fileName to the name of theFile 
set thePerformers to text item 1 of fileName 
set theEvent to text item 2 of fileName 
set theYear to (text item 3 of fileName) as string 
set theRest to text items 4 thru (count of text items of fileName) of fileName as string 
set newName to {thePerformers, theYear, theEvent, theRest} 
set name of theFile to newName as string 
end repeat 
set my text item delimiters to tid 
end tell 
end open

Kari

jonn,

I got your search pattern to work!
(It seems that one must be very careful with those GREP patterns!)

Kari