Sensible Show Synchronization

When synchronizing with an iPod or iPhone, iTunes offers two key options: ‘x least recent unwatched’ or ‘x most recent unwatched’. Unfortunately for us users, recent is determined, not by sensible things such as air-date or episode number, but by the date items are added to a user’s iTunes library. All works fine when items are purchased from the iTunes Music Store and downloads complete in order. Encounter a few failed downloads or add items manually however and iPod synchronization never again works as one might reasonably expect.
Since we, as users, have suffered this strange behaviour for a good number of iTunes releases, I can only assume that somewhere in Cupertino it’s deemed a feature (although I have yet to work out what it achieves).

Fortunately, for OS X users, AppleScript comes to the rescue:

tell application "iTunes"

	if selection is {} then
		display dialog "Select some TV Shows..." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
	end if

	set sel to selection

	repeat with i from 1 to (count sel)
		set t to item i of sel
		tell t

			set my_kind to TV show
			set filename to location
			set my_state to unplayed
			set my_played_count to played count
			delete
			set my_track to (add filename)
			tell my_track
				set video kind to TV show
				set unplayed to my_state
				set played count to my_played_count
			end tell

		end tell
	end repeat
end tell

The above script iterates over the selected items in iTunes, sequentially deleting them and then re-adding them. Assuming this list of TV Shows is in the correct order, this will ensure all ‘added’ times are sequential and iPod and iPhone synchronization should now proceed as expected.