rename movie files

I created a script to rename my movie files to: Title (Year).extension

it takes out words not needed, replaces dots with spaces, puts brackets around the year, removes trailing spaces and removes color label

is there a way to make it faster?


set defDel to AppleScript’s text item delimiters
set myList to {“720p”, “x264”, “1080p”}
repeat with theItem in myList

tell application "Finder" to set myFiles to (every file of folder "Time Machine Backups:Movies:" whose name contains theItem)
repeat with thisItem in myFiles
	set thename to name of thisItem
	if thename contains theItem then
		set AppleScript's text item delimiters to theItem
		set newName to text items of thename
		set AppleScript's text item delimiters to ""
		set name of thisItem to (newName as string)
		
		set AppleScript's text item delimiters to defDel
	end if
end repeat
delay 10

end repeat
delay 10

set defDel to AppleScript’s text item delimiters
set myList to {" ", " ", " ", " "}
repeat with theItem in myList

tell application "Finder" to set myFiles to (every file of folder "Time Machine Backups:Movies:" whose name contains theItem)
repeat with thisItem in myFiles
	set thename to name of thisItem
	if thename contains theItem then
		set AppleScript's text item delimiters to theItem
		set newName to text items of thename
		set AppleScript's text item delimiters to " "
		set name of thisItem to (newName as string)
		set AppleScript's text item delimiters to defDel
	end if
end repeat

end repeat
set defDel to AppleScript’s text item delimiters
set myList to {}
repeat with x from 1950 to 2016 by 1
set end of myList to x as text
end repeat

repeat with theItem in myList
tell application “Finder” to set myFiles to (every file of folder “Time Machine Backups:Movies:” whose name contains " " & theItem)
repeat with thisItem in myFiles
set thename to name of thisItem
if thename contains " " & theItem then
set AppleScript’s text item delimiters to theItem
set newName to text items of thename
set AppleScript’s text item delimiters to " (" & theItem & “)”
set name of thisItem to (newName as string)
set AppleScript’s text item delimiters to defDel
end if
end repeat

end repeat

set defDel to AppleScript’s text item delimiters
tell application “Finder” to set myFiles to every file of folder “Time Machine Backups:Movies:”
repeat with thisItem in myFiles
set thename to name of thisItem
if thename contains “((” then
set AppleScript’s text item delimiters to “((”
set newName to text items of thename
set AppleScript’s text item delimiters to “(”
set name of thisItem to (newName as string)
set AppleScript’s text item delimiters to defDel
end if
end repeat

set defDel to AppleScript’s text item delimiters
tell application “Finder” to set myFiles to every file of folder “Time Machine Backups:Movies:”
repeat with thisItem in myFiles
set thename to name of thisItem
if thename contains “))” then
set AppleScript’s text item delimiters to “))”
set newName to text items of thename
set AppleScript’s text item delimiters to “)”
set name of thisItem to (newName as string)
set AppleScript’s text item delimiters to defDel
end if
end repeat
set defDel to AppleScript’s text item delimiters
set myList to {" ", " ", " ", " "}
repeat with theItem in myList

tell application "Finder" to set myFiles to (every file of folder "Time Machine Backups:Movies:" whose name contains theItem)
repeat with thisItem in myFiles
	set thename to name of thisItem
	if thename contains theItem then
		set AppleScript's text item delimiters to theItem
		set newName to text items of thename
		set AppleScript's text item delimiters to " "
		set name of thisItem to (newName as string)
		set AppleScript's text item delimiters to defDel
	end if
end repeat

end repeat

set defDel to AppleScript’s text item delimiters
tell application “Finder”
set _filenames to name of every file of folder “Time Machine Backups:Movies:”
set _extensions to name extension of every file of folder “Time Machine Backups:Movies:”
end tell

– Collect names (filename - dot and extension)
set _names to {}

repeat with n from 1 to count of _filenames

set _filename to item n of _filenames as text
set _extension to item n of _extensions as text

if _extension is not "" then
	set _length to (count of _filename) - (count of _extension) - 1
	set end of _names to text 1 thru _length of _filename
else
	set end of _names to _filename
end if

end repeat
delay 10
set myList to {}
repeat with i from 1 to count of _names
set thisItem to item i of _names
if thisItem contains “.” then
set end of myList to thisItem as text
end if
end repeat
delay 10
repeat with theItem in myList
tell application “Finder” to set myFiles to (every file of folder “Time Machine Backups:Movies:” whose name contains theItem)
repeat with thisItem in myFiles
set thename to name of thisItem
set theextension to name extension of thisItem
if thename contains “.” then

		set AppleScript's text item delimiters to "."
		if number of text items of thename > 1 then
			set thename to text items 1 thru -2 of thename as text
			repeat while thename ends with space
				set thename to thename's text 1 thru -2
			end repeat
		end if
		
		set newName to text items of thename
		
		set newName to text items of thename
		set AppleScript's text item delimiters to " "
		set name of thisItem to (newName as string) & "." & theextension
		
		set AppleScript's text item delimiters to defDel
	end if
end repeat

end repeat
delay 10
tell application “Finder” to set label index of (every file of folder “Time Machine Backups:Movies:”) to 0

–0 → No Label
–1 → Orange Label
–2 → Red Label
–3 → Yellow Label
–4 → Blue Label
–5 → Purple Label
–6 → Green Label
–7 → Gray Label