' ###############################################################################
' #
' # itunes_extract_artwork.vbs
' #
' # This script will extract the artwork from the selected tracks in iTunes
' # and save it to a file in the same directory as the track.
' #
' # written by: Robert Jacobson (http://www.pobox.com/~teridon/itunesscripts/)
' # Last Updated: 2010-01-22
' # Version 1.1
' #
' # This script is GPL v2.  see http://www.gnu.org/copyleft/gpl.html
' #
' ###############################################################################


' Minor modificatons to script to enable use with iTunes 9.x
' Change art output filename from <Track Name>.<Ext> to Folder.jpg
' Prevent overwriting any existing file
' By Steve MacGuire - http://samsoft.org.uk/iTunes/
' Last Updated: 2010-01-23
' Original script at http://www.pobox.com/~teridon/itunesscripts/itunes_extract_artwork.vbs


Option Explicit

Dim iTunesApp   ' iTunes.Application object used to access the iTunes application.
Dim tracks	' The tracks collection object of the Library object. 
Dim TrackPath	' The path to the track
Dim ArtPath	' The path to the artwork
Dim c
Dim songName
Dim artist
Dim i
Dim track
Dim FormatArray(4)
Dim ExtArray(4)
Dim Artobj
Dim Art
Dim ArtDir
Dim Format
Dim fso 
Dim args
Dim arg
Dim artcount
Dim Title

Title = "iTunes Extract Artwork"

artcount = 0
DIM use_windows

'detect CSCRIPT context & use InputLine
IF Instr(lcase(wscript.FullName), "cscript")>0 THEN	
  use_windows = 0
ELSE	'detect WSCRIPT context & use native InputBox
  use_windows = 1
END IF

Set fso = CreateObject("Scripting.FileSystemObject")

FormatArray(0) = "Unknown"
FormatArray(1) = "JPEG"
FormatArray(2) = "PNG"
FormatArray(3) = "BMP"
ExtArray(0) = "unk"
ExtArray(1) = "jpg"
ExtArray(2) = "png"
ExtArray(3) = "bmp"

Set iTunesApp  = CreateObject("iTunes.Application.1")

Dim vers
vers = iTunesApp.Version

Dim Reg1
Set Reg1 = new RegExp
Reg1.Pattern = "^9"
if Reg1.Test(vers) Then
  ' yay
Else
  Wscript.Echo "This script requires iTunes 9"
  Wscript.Quit
End If

Set tracks = iTunesApp.SelectedTracks

If tracks is nothing Then
  MsgBox "Please select some tracks first.",0,Title
  Wscript.Quit
End If

Dim outputstring

For i = 1 To tracks.Count
  Set track = tracks.Item(i)
	
  If track.Kind = 1 Then
    songName = track.Name
    artist = track.Artist
    TrackPath = track.Location
    Set Artobj = track.Artwork
    For c = 1 To Artobj.Count
      Set Art = Artobj.Item(c)
      Format = Art.Format
      ' Wscript.Echo "Format is " & FormatArray(Format)
      ArtDir = fso.GetParentFolderName(TrackPath)
      ' Wscript.Echo "Artdir is " & ArtDir
      ' ArtDir = fso.GetBaseName(ArtDir)
      ' Dim RegX
      ' Set RegX = new RegExp
      ' RegX.Pattern = "[/:\\\*\?""""<>]"
      ' RegX.Global = True
      ' songName = RegX.Replace(songName, "-")
      ' songName = Replace(songName, "/", "-")
      ' ArtPath = fso.BuildPath(ArtDir, songName & "." & ExtArray(Format))
      ArtPath = ArtDir & "\Folder.jpg"
      If FSO.FileExists(ArtPath) Then
        ' do nothing
      Else
        ' If use_windows = 1 Then
        '   outputstring = outputstring & "Artwork saved to " & ArtPath & Chr(10)
        ' Else
        '   Wscript.Echo "Artwork saved to " & ArtPath
        ' End If
        ' save to file
        ' MsgBox ArtPath,0,Title
        Art.SaveArtworkToFile(ArtPath)
        artcount = artcount + 1
      End If
    Next
  End If
Next

If artcount = 0 Then
  If use_windows = 1 Then
    MsgBox "No new artwork was generated.",0,Title
  Else
    Wscript.Echo "No new artwork was generated."
  End If
Else
  If use_windows = 1 Then
    If artcount = 1 Then
      MsgBox "1 new artwork file was created.",0,Title
    Else
      MsgBox artcount & " new artwork files were created.",0,Title
    End If
  End If
End If