I wrote a script to work with iTunes.
It looks at thetvdb.com database to get the episode title and episode description based on the serie name / season number / episode number
the video files are usually name like this:
Serie.S01E01.ext or Serie - 1x01.ext or Serie.101.ext
I’d like to be able to get the show name, season number and episode number from the filename (which is the track name in iTunes) instead of having to enter it manually
I’m not sure of the regexp pattern and I don’t know how to used them
set theFiles to {"Serie.S02E06.ext", "Serie - 1x01.ext", "Serie.312.ext"}
set {epList, snList} to {{}, {}}
repeat with oneFile in theFiles
set t to text 7 thru -5 of oneFile
considering case
if t contains "S" and t contains "E" then
tell t to set {season, episode} to {text 2 thru 3, text 5 thru 6}
else if t contains "x" then
set t to text ((offset of " " in t) + 1) thru -1 of t
set {TID, text item delimiters} to {text item delimiters, "x"}
set {season, episode} to text items of t
set text item delimiters to TID
else
if (count t) = 3 then
tell t to set {season, episode} to {text 1, text -2 thru -1}
else
tell t to set {season, episode} to {text 1 thru 2, text -2 thru -1}
end if
end if
end considering
set {end of snList, end of epList} to {season as integer, episode as integer}
end repeat
property kVideoNamePatterns : "
(.+?)\\.S([0-9]+)E([0-9]+) # matches e.g. 'Serie.S01E01.ext'
|
(.+?)\\s*-\\s*([0-9]+)x([0-9]+) # matches e.g. 'Serie - 1x01.ext'
|
(.+?)\\.([0-9]+)([0-9]{2}) # matches e.g. 'Serie.101.ext'
"
on splitVideoFileName(txt)
tell application "TextCommands"
set allMatches to search txt for kVideoNamePatterns with regex and comments allowed
end tell
if allMatches is {} then error "Unknown format."
set theMatches to item 1 of allMatches
repeat with i from 1 to 7 by 3
set {showName, seasonNo, episodeNo} to items i thru (i + 2) of theMatches
if showName is not "" then exit repeat
end repeat
return {showName, seasonNo as integer, episodeNo as integer}
end splitVideoFileName
-- TEST
set testData to {"Serie.S01E01.ext", "Serie - 1x01.ext", "Serie.101.ext"}
repeat with itemRef in testData
set contents of itemRef to splitVideoFileName(itemRef)
end repeat
testData --> {{"Serie", 1, 1}, {"Serie", 1, 1}, {"Serie", 1, 1}}
If you need help getting to grips with regular expressions there are plently of online tutorials and various books available on the subject. e.g. I believe Jeffery Friedl’s “Mastering Regular Expressions” (3rd edition, O’Reilly) is very well regarded.