' =================
' ReplaceTextInName
' =================

' Version 1.0.0.1 - November 21st 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 script for iTunes for Windows to replace text

' =========
' ChangeLog
' =========

' Version 1.0.0.1 - Initial version

' Visit http://samsoft.org.uk/iTunes/scripts.asp for updates


' ==========
' To-do List
' ==========

' Find a way to replace one word with another in the same place, e.g. to swap "and" with "&" - Might be better as a separate script.
' Add more things to do


' =============================
' Declare constants & variables
' =============================

Option Explicit
Const Min=1		' Minimum number of tracks this script should work with
Const Max=0		' Maximum number of tracks this script should work with, 0 for no limit
'Dim CD			' Handle to CommonDialog object
'Dim FSO		' Handle to FileSystemObject
Dim iTunes		' Handle to iTunes application
'Dim SH			' Handle to Shell application
Dim nl			' New line string for messages
Dim Title		' Message box title
Dim Tracks		' A collection of track objects
Dim Count		' The number of tracks
Dim P,S,U		' Counters
Dim Q			' Global flag
Dim Dbg			' Manage debugging output
Dim Opt			' Script options
Dim Sch			' Search text
Dim Rep 		' Replacement text


' =======================
' Initialise user options
' =======================

' N.B. Edit Opt value to suit your needs.

' Control options, add bit values (x) for selective actions
' Bit 0 = Suppress dialog box for previews, just process tracks					(1)
' Bit 1 = Suppress summary report								(2)
' Bit 2 = Process entire library, otherwise try to restict to current playlist			(4)

Opt=0

' Debug/report options, add bit values (x) for selective actions, initial value may be modified during run
' Bit 0 = Confirm actions									(1)

Dbg=0


' ============
' Main program
' ============

  Init			' Set things up
  ProcessTracks		' Main process 
  Report		' Summary

' ===================
' End of main program
' ===================



' ===============================
' Declare subroutines & functions
' ===============================


' Initialise track selections, quit script if track selection is out of bounds or user aborts
Sub Init
  Dim R,T
  ' Initialise global variables
  P=0
  S=0
  U=0
  Q=False
  nl=vbCrLf
  Title="Replace Text In Name"
  ' Initialise global objects
  ' Set CD=CreateObject("UserAccounts.CommonDialog")
  ' Set FSO=CreateObject("Scripting.FileSystemObject")
  Set iTunes=CreateObject("iTunes.Application")
  ' Set SH=CreateObject("Shell.Application") 

  Set Tracks=iTunes.SelectedTracks
  If Tracks is Nothing Then
    If (Opt AND 4) OR iTunes.BrowserWindow.SelectedPlaylist.Source.Name<>"Library" Then
      Set Tracks=iTunes.LibraryPlaylist.Tracks
    Else
      Set Tracks=iTunes.BrowserWindow.SelectedPlaylist.Tracks
    End If
  End If
  Count=Tracks.Count
  ' Check there is a suitable number of suitable tracks to work with
  IF Count<Min Or (Count>Max And Max>0) Then
    If Max=0 Then
      MsgBox "Please select " & Min & " or more tracks in iTunes before calling this script!",0,Title
      WScript.Quit
    Else 
      MsgBox "Please select between " & Min & " and " & Max & " tracks in iTunes before calling this script!",0,Title
      WScript.Quit
    End If
  End If
  ' Check if the user wants to proceed and get keywords
  T="Enter search text for " & Count & " track" & Plural(Count,"s","")
  Sch=InputBox(T,Title)
  If Sch="" Then
    WScript.Quit
  End If
  T="Enter replacement text for " & Count & " track" & Plural(Count,"s","")
  Rep=InputBox(T,Title)
End Sub


' 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


' Loop through track selection processing suitable items
Sub ProcessTracks
  Dim I,T,V
  For I=Count to 1 step -1			' Work backwards to avoid index errors
    Set T=Tracks.Item(I)
    If T.Kind=1 Then				' Only process "File" tracks
      P=P+1
      V=Replace(T.Name,Sch,Rep)			' Change working field here,
      V=Replace(V,"  "," ")
      V=Trim(V)
      IF V<>T.Name Then				' here
        U=U+1
        T.Name=V				' and here
      End If
      IF Q Then Exit Sub
    End If
  Next
End Sub


' Output report
Sub Report
  If (Opt AND 2) Then Exit Sub
  Dim T
  T=P & " track" & Plural(P,"s","")
  If P<Count Then T=T & " of " & count
  T=T & Plural(P," were"," was") & " processed of which " & nl
  T=T & U & Plural(U," were"," was") & " updated"
  T=T & "."
  MsgBox T,vbInformation,Title
End Sub


' ==============
' End of listing
' ==============
