AppleScript for Renaming Scene Movies

So, you recently downloaded a few movies from your favourite movie tracker (completely legally of course seeing as you own the DVD/BluRay hard copy and so are just exercising your fair use rights), but they’re all named the way the scene likes them. That is, “Movie.Title.With.Lots.of.Dots.date.quality.source.codec-group”. Now, this isn’t particularly great if you like to keep a nice movie collection with readable and attractive names. Also, those types of names aren’t always ideal for things like Boxee or NMTs when they come to scan the filename and scrape IMDB and TMDB.

In fact, I rename my movies primarily so they’re fit for use with YAMJ (see my YAMJ collection here). YAMJ likes the name first, then the date in brackets to help it distinguish between other titles with a potentially similar name, and then finally an optional indicator of what quality it is. I think YAMJ can determine whether the file is HD content or not by its use of mediaInfo, but I like to tell it for sure just in case.

So, after a particularly large downloading session I really couldn’t be bothered renaming each of the files by hand and decided it was time to bring some automation to the table, so here we go!

This script will only work for “traditional” scene files, like Blue.Valentine.2010.720p.BluRay.x264-HiDtHarry.Potter.And.The.Deathly.Hallows.Part.1.2010.720p.BluRay.DD5.1.x264-EbP or Discussions.with.Richard.Dawkins.Episode.01.The.Four.Horsemen.2008.DVDRip.x264.

A few conditions: It’s only programmed to work with MKV files. It ignores any file with “sample” in the filename (so it doesn’t bother renaming sample files that come with most scene encodes). It’s recursive, so it’s advisable to create a little temporary folder with all the MKVs (NB: you don’t have move the MKV from the “scene folder” that it came in, just move the entire folder) you want to rename to avoid the danger of something being caught in the crossfire.

This is the script:

set AppleScript's text item delimiters to ""

set theDir to POSIX path of (choose folder with prompt "Pick the folder containing the (scene) *.mkv films you would like to rename:") as text

set theFiles to (do shell script "cd " & (quoted form of theDir) & " && find . -type f -name \"*.mkv\" -size +1048576k \\! -iname \"*sample*\" -print | sed s1./11")
set x to 1

repeat while x is less than or equal to (number of paragraphs of theFiles)

	set AppleScript's text item delimiters to "/"

	set theFilename to the last text item of paragraph x of theFiles

	set AppleScript's text item delimiters to ""

	set highDef to (do shell script "echo " & (quoted form of theFilename) & " | grep -i -e bluray -e 720p -e 1080p -e hddvd -e hd-dvd -e hdtv; exit 0")
	if highDef is not equal to "" then
		set highDef to true
	else
		set highDef to false
	end if

	set try19 to offset of "19" in theFilename
	set try20 to offset of "20" in theFilename

	if (try19 + try20) is equal to 0 then
		try
			set highDef to "2"
			display dialog "The file, " & theFilename & ", doesn't seem to be in a standard scene format. Please name it manually (include extention):" default answer theFilename
			set theName to text returned of result
		on error errstr number errnum
			display dialog "AppleScript suffered an error: \"" & errstr & " " & errnum & "\". Sorry about that. The file has been left alone, and the script will continue."
			set theName to theFilename
		end try
	else if try19 is equal to 0 then
		set theName to (characters 1 thru (try20 - 2) of theFilename as text)
		set AppleScript's text item delimiters to "."
		set someText to text items of theName
		set AppleScript's text item delimiters to " "
		set theName to someText as text
		set AppleScript's text item delimiters to ""
		set theDate to (characters try20 thru (try20 + 3) of theFilename as text)
	else if try20 is equal to 0 then
		set theName to (characters 1 thru (try19 - 2) of theFilename as text)
		set AppleScript's text item delimiters to "."
		set someText to text items of theName
		set AppleScript's text item delimiters to " "
		set theName to someText as text
		set AppleScript's text item delimiters to ""
		set theDate to (characters try19 thru (try19 + 3) of theFilename as text)
	else if try19 is less than try20 then
		set theName to (characters 1 thru (try19 - 2) of theFilename as text)
		set AppleScript's text item delimiters to "."
		set someText to text items of theName
		set AppleScript's text item delimiters to " "
		set theName to someText as text
		set AppleScript's text item delimiters to ""
		set theDate to (characters try19 thru (try19 + 3) of theFilename as text)
	end if

	if highDef is equal to true then
		do shell script "mv " & (quoted form of (theDir & (paragraph x of theFiles))) & " " & (quoted form of (theDir & ((theName & ".(" & theDate & ").BluRay.mkv"))))
	else if highDef is equal to "2" then
		do shell script "mv " & (quoted form of (theDir & (paragraph x of theFiles))) & " " & (quoted form of (theDir & (theName & ".mkv")))
	else
		do shell script "mv " & (quoted form of (theDir & (paragraph x of theFiles))) & " " & (quoted form of (theDir & (theName & ".(" & theDate & ").mkv")))
	end if

	set x to (x + 1)

end repeat

set AppleScript's text item delimiters to ""

Before

After

Of course, you can change the script to mod the final filename however you wish. Comment if you have any problems, or even if you like it!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>