Script iTunes to prepend Season and Episode number (padded) to episode title

applescriptappletvitunes

I'm not knowledgeable about scripting, so I'll be upfront about that. I've been doing management with Doug's Scripts for over a decade, but the one that would be most useful (Append to Selected Tag) doesn't offer the correct fields to adjust the information I want to change.

I would like to prepend the season number and the (padded 2 digit) episode number to the beginning of episode names in my library (not the file name!), so I get something like 101 Pilot, 102 Deep Throat, 103 Squeeze, etc., in my smart playlists for using on AppleTV (because the newer AppleTVs running tvOS do not show that additional information in the playlist view, and it's easy to lose your place in long running series).

I have used Subler and iFlicks, and both can handle prepending the information and adding to iTunes going forward, but if I drop my older library files to those apps, some of my comments, groupings, etc., get erased using their update metadata options (in iFlicks 2, the artwork also gets changed, and I have to handle each season separately to change it back), and neither handle extended episodes or DVD vs Aired orderings well when starting from a clean slate.

I'm looking for a script that I can use to select entire playlists or shows and only changes the one tag (the episode name) using info from other tags that I already have populated (the original episode name, season number, and episode number) and that can pad the episode number 0-9 to 2 digits. Can someone point me in the right direction?

Best Answer

I think this will do it for you:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
--
-- Start by selecting the shows in a playlist. 
-- Don't select the playlist. Select the Shows.

tell application "iTunes"
    set theShowList to the selection
    repeat with aShow in theShowList
        -- get the information we need
        set theSeasonNumber to season number of aShow
        set theEpisodeNumber to episode number of aShow
        set theName to name of aShow
        --
        -- pad the episode number to two digits
        set thePaddedEpisodeNumber to "0" & theEpisodeNumber as string
        set theTwoDigitEpisodeNumber to characters -2 thru -1 of thePaddedEpisodeNumber as string
        --
        -- assemble the new name
        set theNewName to theSeasonNumber & theTwoDigitEpisodeNumber & " " & theName as string
        --
        -- set the new name
        set the name of aShow to theNewName
    end repeat
end tell

The script could be cleaned up and made more efficient but I wrote it this way so it was easier to follow.

Be sure to select the shows, not the playlist. It doesn't work otherwise.

While experimenting with this I noticed that some TV shows have episode numbers that are three digits already. That would be a problem for this script. FYI.

This was done with iTunes 12.9.5.5, macOS Mojave 10.14.6.

Here are before and after pictures. You're going to have to zoom in.Before-- and note the shows are selected!

enter image description here