' ========
' TrimName
' ========

' Version 1.0.0.1 - January 16th 2011
' Copyright © Steve MacGuire 2011


' =======
' 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
' ===========

' Add a description here


' =========
' ChangeLog
' =========

' Version 1.0.0.1 - Initial version



' Visit http://samsoft.org.uk/iTunes/scripts.asp for updates


' ==========
' To-do List
' ==========

' Add 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 iTunes		' Handle to iTunes 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 Cut			' Number of character to cut


' =======================
' 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=4

' 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=vbCr & vbLf
  Title="Trim 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 how
  T="About to process " & Count & " track" & Plural(Count,"s","") & "?" & nl & nl
  T=T & "Enter the number of characters to remove from the left of the Name field or use a negative value to strip from the right."
  R=InputBox(T,Title)
  If R="" Then WScript.Quit
  Cut=Val(R)
  If Cut=0 Then WScript.Quit  
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,N
  For I=Count To 1 Step -1	' Work backwords in case edit remomves item from selection
    Set T=Tracks.Item(I)
    If T.Kind=1 Then		' Only process "File" tracks
      N=Len(T.Name)
      If Cut>0 Then
        If N>Cut Then
          T.Name=Trim(Mid(T.Name,Cut+1))
          U=U+1
        End If 
      Else
        If N>-Cut Then
          T.Name=Trim(Left(T.Name,N+Cut))
          U=U+1
        End If
      End If
      P=P+1
      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


' Reads value from string.
Function Val(T)
  Dim A,I,N
  I=1
  Val=0
  If Left(T,1)="-" Then
    N=True
    I=I+1
  Else
    N=False
  End If
  If T<>"" Then
    Do
      A=Asc(Mid(T,I))-48
      IF A<0 Or A>9 Then Exit Do
      Val=Val*10+A
      I=I+1
      If I>Len(T) Then Exit Do
    Loop
  End If
  If N Then Val=-Val
End Function


' ==============
' End of listing
' ==============