' =====================
' KillTopLevelPlaylists
' =====================
' Version 1.0.0.1 - December 30th 2011
' Copyright © Steve MacGuire  2011
' 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 top-level non-smart playlists from iTunes, only removes non-special playlists not in folders
' Written in response to this ASC thread: https://discussions.apple.com/message/17145340#17145340

' =========
' ChangeLog
' =========
' Version 1.0.0.1 - Initial version

' 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 Top Level Playlists"
Const Summary="Remove top level non-smart playlists."

' =======================
' 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 2011-10-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 & nl & nl & "Proceed?"
    If Check Then
      R=MsgBox(Q,vbYesNoCancel+vbQuestion,Title)
    Else
      R=MsgBox(Q,vbOKCancel+vbQuestion,Title)
    End If
    If R=vbCancel Then WScript.Quit
    If R=vbYes or R=vbOK Then
      Check=False
    Else
      Check=True
    End If
  End If 
  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 2011-09-28
Sub ProcessLists
  Dim I,L,Lists,M
  Set Lists=iTunes.Sources.Item(1).Playlists
  For I=Lists.Count To 1 Step -1
    Set L=Lists.Item(I)
    If L.Kind=2 Then
      'If L.Tracks.Count=0 And L.SpecialKind=0 And L.Smart=False Then
      If L.SpecialKind=0 And L.Smart=False And L.Name<>"Voice Memos" And Left(L.Name,9)<>"Purchased" Then
        Set M=L.Parent
        If (M Is Nothing) Then  ' Only kill top-level playlists
          ' MsgBox L.Name,vbInformation,Title
          L.Delete
          U=U+1
        End If
      End If
    End If
    P=P+1
  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
' ==============
