' ==================
' KillNamedPlaylists
' ==================
' Version 1.0.0.2 - February 25th 2016
' Copyright © Steve MacGuire  2011-16
' http://samsoft.org.uk/iTunes/KillTopLevelPlaylists.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
' ===========
' Seek out and destroy playlists matching an input string
' Written in response to this ASC post: https://discussions.apple.com/message/29825194#29825194

' =========
' ChangeLog
' =========
' Version 1.0.0.1 - Initial version
' Version 1.0.0.2 - Process all sources, not just Library

' Visit http://samsoft.org.uk/iTunes/scripts.asp for updates

' ==========
' To-do List
' ==========
' Add more things to do

' =============================
' Declare constants & variables
' =============================
Option Explicit	        ' Declare all variables before use
Dim Intro,Outro,Check   ' Manage confirmation dialogs
Dim iTunes              ' Handle to iTunes application
Dim P,S,U               ' Counters
Dim nl,tab              ' New line/tab strings

Const Title="Kill Named Playlists"
Dim Summary,Match
Summary="Enter playlist name to remove on partial match." & vbCrLf & vbCrLf & _
  """Test"" matches ""Test"", ""Test 1"", ""Testing"" etc."
 
' =======================
' Initialise user options
' =======================
Intro=True              ' Set false to skip initial prompts, avoid if non-reversible actions
Outro=True              ' Produce summary report
Check=False             ' Step-by-step confirmation

' ============
' Main program
' ============

Init                    ' Set things up
ProcessLists            ' Main process 
Report                  ' Summary

' ===================
' End of main program
' ===================


' ===============================
' Declare subroutines & functions
' ===============================


' Initialisation routine
' Modified 2016-02-24
Sub Init
  Dim Q,R
  ' Initialise global variables
  P=0
  S=0
  U=0
  nl=vbCrLf
  tab=Chr(9)
  ' Initialise global objects
  If Intro Then
    Q=Summary
    R=InputBox(Q,Title)
    If R="" Then WScript.Quit
  End If
  Match=R
  Set iTunes=CreateObject("iTunes.Application")
End Sub


' Return relevant string depending on whether value is plural or singular
' Modified 2011-09-28
Function Plural(V,P,S)
  If V=1 Then Plural=S Else Plural=P
End Function


' Loop through playlist
' Modified 2016-02-25
Sub ProcessLists
  Dim C,E,I,L,Lists,M,R,S
  E=False
  For S=iTunes.Sources.Count To 1 Step -1
    Set Lists=iTunes.Sources.Item(S).Playlists  ' Process all sources
    For I=Lists.Count To 1 Step -1
      Set L=Lists.Item(I)
      If L.Kind=2 Then
        If L.SpecialKind=0 Then
          If Left(L.Name,Len(Match))=Match Then ' Only kill matching playlists
            ' MsgBox iTunes.Sources.Item(S).Name & "/" & L.Name
            On Error Resume Next                ' Trap potential error
            L.Delete
            If Err.Number<>0 Then               ' Handle error if one occurred
              If E=False Then   
                R=MsgBox("Error deleting playlist, please switch device to manual management.",vbCritical+vbOKCancel,Title)
                If R=vbCancel Then Exit Sub     ' Abort script
                E=True                          ' Ignore further errors
              End If
            Else
              U=U+1
            End If
            On Error Goto 0                     ' Restore default error handler
          End If
        End If
      End If
      P=P+1
    Next 
  Next  
End Sub


' Output report
' Modified 2011-10-24
Sub Report
  If Not Outro Then Exit Sub
  Dim T
  T=P & " playlist" & Plural(P,"s were"," was") & " processed," & nl
  T=T & U & Plural(U," were"," was") & " removed."
  MsgBox T,vbInformation,Title
End Sub


' ==============
' End of listing
' ==============