' =======
' SetYear
' =======
' Version 1.0.0.2 - March 14th 2016
' Copyright © Steve MacGuire 2016
' http://samsoft.org.uk/iTunes/SetYear.vbs
' 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 for iTunes to set the value of Year from the current value of ReleaseDate or manual input

' Version 1.0.0.1 - Initial release
' Version 1.0.0.2 - Minor tweaks

Option Explicit
Dim AA,AL,I,iTunes,nl,R,T,tab,Title,TN,Track,Tracks,U,V,W,Y

Dim Reviewing
Reviewing=True          ' True: See/edit every year value - False: Automatically update year from release date if possible

nl=vbCrLf:tab=Chr(9)
U=0
W=35
Title="Set Year"
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
  For I=1 To Tracks.Count
    Set Track=Tracks.Item(I)
    With Track
      If .Kind=1 Then           ' Only process file tracks
        If .Location<>"" Then   ' Only process videos
          R=Year(.ReleaseDate) : If R<1900 Then R=0 ' Empty release date returns 1899?
          Y=.Year
          ' N.b. Dates in the range 1-1600 are currently are hidden by iTunes. Why?
          If Y<1 And R>0 Then Y=R       ' Assign Year from Release Date where possible     
          If Y<1601 or Reviewing Then   ' Reveal hidden dates, or all dates if reviewing
            AA=.AlbumArtist & "" : If AA="" Then AA=Track.Artist & "" : If AA="" Then AA="Unknown Artist"
            AL=.Album & "" : If AL="" Then AL="Unknown Album"
            TN=Right("0" & .TrackNumber,2) & " " : If TN="00 " Then TN=""
            T="Artist:" & tab & Wrap(AA,W," ",1) & nl
            T=T & "Album:" & tab & Wrap(AL,W," ",1) & nl
            T=T & "Track:" & tab & Wrap(TN & Track.Name,W," ",1) & nl & nl
            T=T & "Enter new value for year:" & nl & nl
            'T=T & "(Values < 1601 can be accepted but won't show in iTunes.)" & vbCrLf & vbCrLf 
            T=T & "Press Cancel to quit"
            V=InputBox(T,Title,Y)
            If V="" Then WScript.Quit   ' Quit script on cancel/no input
            V=Eval(V)
            Y=V
          End If
          If Y<>.Year Then      ' Update if new value entered
            .Year=Y
            If .Year=Y Then U=U+1 Else MsgBox "Change not processed!",vbExclamation,Title
          End If
          ' Cannot set ReleaseDate from script or Get Info. Why not?
        End If
      End If
    End With
  Next
  If U=1 Then
    MsgBox "1 date was updated",0,Title
  Else
    MsgBox U & " dates were updated.",0,Title
  End If
End If


' Wrap & tab long strings, break string S after character C working back from up to W characters adding T tabs to each new line
' Modified 2014-09-27
Function Wrap(S,W,C,T)
  Dim P
  If Len(S)<=W Then
    Wrap=S
  Else    
    P=InstrRev(S,C,W)
    If P Then Wrap=Left(S,P) & nl & String(T,tab) & Wrap(Mid(S,P+1),W,C,T)
  End If
End Function