' ==============
' ChangeCategory
' ==============

' Version 1.0.0.1 - June 4th 2010
' Copyright © Steve MacGuire 2010


' =======
' 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 change the Podcast Category of selected tracks.


' =========
' ChangeLog
' =========
' Version 1.0.0.1 - Initial version


' ==========
' To-do List
' ==========
' Add things to do here...


' Declare constants & variables
Option explicit
Const limit=50
Dim iTunes	' iTunes application object
Dim tracks	' tracks collection object
Dim track       ' a specific track
Dim count	' the number of tracks
Dim category    ' the new category
Dim I		' a loop variable
Dim U,P		' counters
Dim title       ' title for dialog boxes
Dim nl		' New Line string
Dim prompt	' some text 
Dim path	' a file path


' Initialise variables
title="Change Category"
nl=vbCr & vbLf
P=0
U=0


' Create objects
Set iTunes = CreateObject("iTunes.Application")
Set tracks = iTunes.SelectedTracks
If tracks is nothing Then Set Tracks=iTunes.BrowserWindow.SelectedPlaylist.Tracks
If tracks is nothing Then
  count=0
Else 
  count=tracks.Count
End If


' Make sure we have data to process, but not too much...
If count<1 or count>limit Then
   MsgBox "Please select at least 1 and no more than " & limit & " tracks before calling this script!",0,title
   WScript.Quit
End If


' Get confirmation
prompt="About to process " & count & Plural(count," tracks"," track") & nl & nl & "Type in the new category to assign."
category=InputBox(prompt,title)
If category="" Then
  If MsgBox("Are you sure you wish to delete the existing Category values?",vbOKCancel,title)<>vbOK Then WScript.Quit
End If


' Process tracks
For I=1 To count
  Set track=tracks(I)
  P=P+1
  If track.kind=1 Then		' Only process "real" files
    IF track.category<>category AND track.kind=1 Then
      If category="" Then
        track.category=""	' For some reason assigning to a variable containing the empty string causes an error
      Else
        track.category=category
      End If
      U=U+1
    End If
  End If
Next


' Display activity summary
Report P,U

' End of main program



' Declare functions & subroutines

' Return relevant string depending on whether value is plural or singular
Function Plural(V,P,S)
  If V=1 Then Plural=S ELSE Plural=P
End Function


Sub Report(P,U)
  MsgBox P & Plural(P," files were"," file was") & " processed of which" & nl & _
    U & Plural(U," files were"," file was") & " updated.",0,title
End Sub