iTunes Auto delete after watched

Hi! I’m a newbie to AppleScripting. I’m trying to create a script that can auto delete a tv show or movie from iTunes after I watch it. However there is a caveat. For TV shows, I always want to keep the latest episode, even after I’ve watched it. Here are the possible scenarios:

  1. TV Show has multiple episodes unwatched. I watch the first of 10, and it gets deleted after watching it, leaving only 9. By the time I watch all episodes, I want the last episode to stay. If the next week a new show is added, I want it to delete the old one once the new one is added, so that iTunes only keeps 1 show from that point on.
  2. TV Show has only 1 episode when the script is created an activated. It leaves this show until a new one is added, then deletes the oldest one.

I’ve seen scripts on Google that will delete movies and tv shows after watched, but none of them contain the need to keep the latest show (so that 1 is always present).

Can anyone help me with this?

As a side note, I don’t need the show deleted from the trash. I have Hazel, which keeps my trash manageable. I just need the TV Show or Movie to be removed from iTunes and put into the trash.

Thanks in advance. I really appreciate the help.

If it helps, this is what I’ve found on Google to delete after watched:

tell application “iTunes”
set filesToDelete to location of (file tracks whose (video kind is TV show) and (unplayed is false))
delete (tracks whose (video kind is TV show) and (unplayed is false))
end tell
tell application “Finder”
repeat with theFile in filesToDelete
delete theFile


All you need to do is create a list of the last movies watched.

Edited: I don’t have a lot of movies, but as an analogy you could do something like this with songs. Although there might not be any reason why one would do this. I’m thinking of an example.