' =====================
' RefreshSmartPlaylists
' =====================
' Version 1.0.0.1 - April 29th 2015
' Copyright © Steve MacGuire 2013-2015
' http://samsoft.org.uk/iTunes/RefreshSmartPlaylists.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
' ===========
' Refresh smart playlists by deleting each track in turn. Only effective on playlists that have a random section criteria


' =========
' ChangeLog
' =========
' Version 1.0.0.1 - Initial version


' ==========
' To-do List
' ==========
' Think of possible variations on this theme


' =============================
' Declare constants & variables
' =============================
Dim iTunes              ' Handle to iTunes application
Dim Title               ' Script title
Set iTunes=CreateObject("iTunes.Application")
Title="Refresh Smart Playlists"


' ============
' Main program
' ============

' One or more calls to SmartRefresh, e.g. SmartRefresh("Folder1\Folder2\SmartList3"), or SmartRefresh("*") for all playlists that contain a random selection
SmartRefresh("*")

' ===================
' End of main program
' ===================


' ===============================
' Declare subroutines & functions
' ===============================


' Returns the path of the playlist object
' Modified 2013-07-07
Function GetListPath(Playlist)
  If Playlist.Kind=1 Then
    GetListPath=Playlist.Name
  ElseIf (Playlist.Parent Is Nothing) Then
    GetListPath=Playlist.Name
  Else
    GetListPath=GetListPath(Playlist.Parent) & "\" & Playlist.Name
  End If
End Function


' Clears the contents of any smart playlist with the specified path, use "*" to match all
' Modified 2015-04-29
Function SmartRefresh(Path)
  Dim C,I,J,List,Lists,P
  Set Lists=iTunes.LibrarySource.Playlists
  For I=1 To Lists.Count
    Set List=Lists.Item(I)
    If List.Kind=2 Then
      If List.SpecialKind=0 And List.Smart Then
        If LCase(GetListPath(List))=LCase(Path) Or Path="*" Then
          ' MsgBox "Clearing " & GetListPath(List),0,Title
          C=List.Tracks.Count
          For J=C To 1 Step -1          ' Loop is ignored if C=0
            ' Ignore error caused by attempting to delete from non-random smart lists
            On Error Resume Next        ' Skip possible error
            List.Tracks.Item(J).Delete  ' Errors for non-random smart playlist
            If Err.Number<>0 Then       ' An error occurred
              ' MsgBox "Skipping " & GetListPath(List),0,Title
              On Error Goto 0           ' Disable error skipping
              Exit For                  ' Abort For loop
            Else
              On Error Goto 0           ' Disable error skipping
            End If
          Next
        End If
      End If
    End If
  Next 
End Function


' ==============
' End of listing
' ==============