AppleScript in Hazel

I’m testing out Hazel on the Mac and I’m looking to rename a TV Show file name similar to:

tv.show.title.S01e02.misc.garbage.characters.mkv (or MP4, or AVI)

to:

Tv Show Title S01E02.mkv (or MP4, or AVI)

I have worked out how to select the file in Hazel, but it looks like I may need Apple Script to remove the characters after SxxExx (I can do the rest in Hazel - I think)

Any help is appreciated.

Initially I did this with basic AppleScript but getting the capitalization right was difficult. So, I used ASObjC. My suggestion assumes that the dot pattern of the OP’s example does not change.

use framework "Foundation"
use scripting additions

set theFileName to "tv.show.title.S01e02.misc.garbage.characters.mkv"

try
	set newFileName to getFileName(theFileName) --> "Tv Show Title S01E02.mkv"
on error
	display alert "A file name could not be processed:" message theFileName
end try

on getFileName(theString)
	set theString to current application's NSString's stringWithString:theString
	set arrayOne to (theString's componentsSeparatedByString:".")
	set arrayTwo to arrayOne's valueForKey:"capitalizedString"
	return (current application's NSString's stringWithFormat_("%@ %@ %@ %@.%@", arrayTwo's objectAtIndex:0, arrayTwo's objectAtIndex:1, arrayTwo's objectAtIndex:2, arrayTwo's objectAtIndex:3, arrayOne's lastObject()) as text)
end getFileName

Wow that looks awesome, I’ll give it a look-see towards the weekend, strapped for time before then. Many thanks.