' ==========
' iTunesInfo
' ==========
' Version 1.0.0.5 - March 13th 2015
' Copyright © Steve MacGuire 2011-2015
' http://samsoft.org.uk/iTunes/iTunesInfo.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
' ===========
' Reads & displays miscellaneous information from iTunes.
' Modify to read any other data of interest that cannot be read in the iTunes interface.

' =========
' ChangeLog 
' =========
' Version 1.0.0.1 - Initial version
' Version 1.0.0.2 - Minor updates
' Version 1.0.0.3 - Add playlist counts
' Version 1.0.0.4 - Add ITL, media folder & layout detection
' Version 1.0.0.5 - Minor update to GetMediaFolder function

' ==========
' To-do List
' ==========
' Add things to do

Option Explicit
Dim Count,FSO,iTunes,nl,T,tab,Tracks
Dim I,L,Layout,Lists,Root,FP,NP,SP
nl=vbCrLf
tab=Chr(9)
Set iTunes=CreateObject("iTunes.Application")
Set FSO=CreateObject("Scripting.FileSystemObject")

Set Tracks=iTunes.SelectedTracks
If Tracks is Nothing Then
  Count=0
Else 
  Count=Tracks.Count
End If

FP=0
NP=0
SP=0
' Loop through playlists
'Set Lists=iTunes.BrowserWindow.SelectedPlaylist.Source.Playlists
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.SpecialKind=4 Then
      FP=FP+1
    ElseIf L.Smart Then
      SP=SP+1
    Else
      NP=NP+1
    End If
  End If
Next

Root=GetMediaPath
Layout=(GetLayout(Root)=1)
If Root="" Then Root="Check under " & nl & tab & tab & tab & "Edit > Preferences > Advanced"

' Set values here as required
' Refer to iTunes COM SDK to see what can be controlled via script commands
' iTunes.ForceToForegroundOnDialog=False

' List out iTunes properties
T="iTunes version" & tab & tab & iTunes.Version & nl & nl
T=T & "ITL location" & tab & tab & iTunesITLPath & nl
T=T & "XML location" & tab & tab & iTunes.LibraryXMLPath & nl
T=T & "Media folder location" & tab & Root & nl
T=T & "iTunes media organization" & tab & Layout & nl & nl
T=T & "Selected playlist" & tab & tab & iTunes.BrowserWindow.SelectedPlaylist.Name & nl
T=T & "Current source" & tab & tab & iTunes.BrowserWindow.SelectedPlaylist.Source.Name & nl
T=T & "Current encoder" & tab & tab & iTunes.CurrentEncoder.Name & nl
T=T & "Force dialogs to foreground" & tab & iTunes.ForceToForegroundOnDialog & nl
T=T & "Process media keys" & tab & tab & iTunes.AppCommandMessageProcessingEnabled & nl & nl
T=T & "Total no. of items in the library" & tab & GroupDig(iTunes.LibraryPlaylist.Tracks.Count) & nl
T=T & "Regular playlists " & tab & tab & NP & nl
T=T & "Smart playlists" & tab & tab & SP & nl
T=T & "Playlist folders" & tab & tab & FP & nl & nl
If Count=0 Then
  T=T & "There are no tracks currently selected."
ElseIf Count=1 Then
  T=T & "There is 1 track currently selected."
Else
  T=T & "There are " & Count & " tracks currently selected."
End If
MsgBox T,0,"iTunes Info"
' End of main program


' Determine iTunes Media folder layout
' Modified 2012-08-11
Function GetLayout(Root)
  Dim File,Line,P,Prefs
  GetLayout=1              ' Assume new style layout unless proved otherwise
  Prefs=Root & "\.iTunes Preferences.plist"
  If Not FSO.FileExists(Prefs) Then Exit Function
  Set File=FSO.OpenTextFile(Prefs,1)
  Do While Not File.AtEndOfStream
    Line=File.ReadLine
    P=Instr(Line,"<integer>")
    If P>0 Then
      GetLayout=Mid(Line,P+9,1)
      Exit Do
    End If
  Loop
  File.Close
End Function


' Attempt to determine root of media path by inspecting location of media files
' Modified 2015-03-13
Function GetMediaPath
  Dim A,C,I,L,P,S,T,Tracks
  Set Tracks=iTunes.LibraryPlaylist.Tracks
  C=Tracks.Count
  If C>100 Then C=100		' Give up if can't find one valid location in the first 100 attempts
  I=1
  P=""
  Do Until P<>"" OR I>C
    Set T=Tracks.Item(I)
    If T.Kind=1 Then		' Only process "File" tracks
      With T
        L=.Location
        If L<>"" Then
          A=.AlbumArtist & ""               ' Workaround for null values
          If A="" Then A=.Artist & ""
          If A="" Then A="Unknown Artist"
          A=ValidiTunes(A,"")
          If .Compilation Then A="Compilations"
          If .Podcast Then
            A=ValidiTunes(.Album & "","")
          ElseIf .VideoKind=1 Then
            A=ValidiTunes(.Name & "","")
          ElseIf .VideoKind=3 Then
            A=ValidiTunes(.Show & "","")
          End If
          If Instr(L,A) Then
            P=Left(L,Instr(L,A)-2)
            S=Mid(P,InStrRev(P,"\"))
            If Instr("\Audiobooks\Books\iPod Games\iTunes U\Mobile Applications\Movies\Music\Podcasts\Ringtones\Tones\TV Shows\Voice Memos",S) Then P=Left(P,Len(P)-Len(S))
          Else
            'MsgBox "Artist:" & .Artist & nl & "Name:" & .Name & nl & "Location:" & .Location
          End If
        End If
      End With
    End If
    I=I+1
  Loop
  ' MsgBox "Media path is " & P & nl & "Found in " & I-1 & " step" & Plural(I-1,"s","")
  GetMediaPath=P
End Function


' Group digits and separate with commas
' Modified 2014-04-29
Function GroupDig(N)
  GroupDig=FormatNumber(N,0,-1,0,-1)
End Function

' Determine path to active iTunes library
' Modified 2012-08-11
Function iTunesITLPath
  Dim F,Folder,ITL,S,XML
  XML=iTunes.LibraryXMLPath
  S=InstrRev(XML,"\")
  Set Folder=FSO.GetFolder(Left(XML,S-1))
  For Each F In Folder.Files
    If LCase(Right(F.Name,4))=".itl" Then
      If IsEmpty(ITL) Then
        Set ITL=F
      Else
        If F.DateLastModified>ITL.DateLastModified Then Set ITL=F
      End If
    End If
  Next
  If IsEmpty(ITL) Then
    iTunesITLPath="Not Found"
  Else  
    iTunesITLPath=ITL.Path
  End If
End Function


' Replace invalid filename characters: \ / : * ? " < > | and also ;
' Replace leading space or period, strip trailing spaces, trailing periods allowed
' Limit to 40 characters inclusive of extenion. No tailing period for folder name 
' Modified 2011-09-17
Function ValidiTunes(N,E)
  N=Left(N,40-Len(E))
  N=Replace(N,"\","_")
  N=Replace(N,"/","_")
  N=Replace(N,":","_")
  N=Replace(N,"*","_")
  N=Replace(N,"?","_")
  N=Replace(N,"""","_")
  N=Replace(N,"<","_")
  N=Replace(N,">","_")
  N=Replace(N,"|","_")
  N=Replace(N,";","_")
  Do While Right(N,1)=" "
    N=Left(N,Len(N)-1)
  Loop
  If Left(N,1)=" " Or Left(N,1)="." Then N="_" & Mid(N,2)
  If E="" And Right(N,1)="." Then N=Left(N,Len(N)-1) & "_"
  ValidiTunes=N
End Function