' ==============
' SetVideoKindTV
' ==============
' Version 1.0.0.1 - August 29th 2012
' Copyright © Steve MacGuire 2012
' http://samsoft.org.uk/iTunes/SetVideoKindTV 
' Please visit http://samsoft.org.uk/iTunes/scripts.asp for updates

' =======
' Licence
' =======
' This program is free software: you can redistribute it and/or modify it under the terms
' of the GNU General Public License as published by the Free Software Foundation, either
' version 3 of the License, or (at your option) any later version.

' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
' without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' See the GNU General Public License for more details.

' Please visit http://www.gnu.org/licenses/gpl-3.0-standalone.html to view the GNU GPLv3 licence.

' ===========
' Description
' ===========
' A VBScript to correct the VideoKind property.

' Related scripts: SetVideoKindMovie, SetVideoKindMusic, SetVideoKindTV

' =========
' ChangeLog 
' =========
' Version 1.0.0.1 - Initial version


Option Explicit
Dim I,K,T,U,Count,iTunes,Title,Track,Tracks

Title="Set VideoKind TV Show"
U=0
Set iTunes=CreateObject("iTunes.Application")
Set Tracks=iTunes.SelectedTracks
If Tracks is Nothing Then
  MsgBox "Please selected some files before calling this script!",VBExclamation,Title
Else
  Count=Tracks.Count
  For I=Count To 1 Step -1      ' Work backwards in case edit removes item from selection 
    Set Track=PersistentObject(Tracks.Item(I))      ' Use global object in case edit removes item from selection
    If Track.Kind=1 Then        ' Only process file tracks
      K=Track.VideoKind
      If K>0 Then               ' Only process videos
        If K<>3 Then            ' That aren't TV Shows
          Track.VideoKind=3     ' And set to TV Show
          U=U+1
        End If
      End If
    End If
  Next
  T=Count & " file" & Plural(Count,"s were"," was") & " processed of which" & vbCrLf
  T=T & U & " file" & Plural(U,"s","") & " updated."
  MsgBox T,0,Title
End If


' Return the persistent object representing the track
' Keeps hold of an object that might vanish from a smart playlist as it is updated
' Modified 2012-09-11
Function PersistentObject(T)
  Dim Ext
  Ext=LCase(Mid(T.Location,InStrRev(T.Location,".")))
  If Instr(".ipa.ipg.m4r",Ext) Then
    Set PersistentObject=T
  Else
    Set PersistentObject=iTunes.LibraryPlaylist.Tracks.ItemByPersistentID(iTunes.ITObjectPersistentIDHigh(T),iTunes.ITObjectPersistentIDLow(T))
  End If  
End Function


' Return relevant string depending on whether value is plural or singular
' Modified 2011-10-04
Function Plural(V,P,S)
  If V=1 Then Plural=S Else Plural=P
End Function