' =======
' GetInfo
' =======
' Version 1.0.0.2 - December 2nd 2011
' Copyright © Steve MacGuire 2011
' http://samsoft.org.uk/iTunes/GetInfo.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 track 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

' ==========
' To-do List
' ==========
' Add things to do

Option Explicit
Dim Count,I,iTunes,NL,R,T,tab,Title,Tracks,AC,AD,AE

nl=vbCrLf
tab=Chr(9)
Title="Get Info"
Set iTunes=CreateObject("iTunes.Application")
Set Tracks=iTunes.SelectedTracks
If Tracks is Nothing Then Set Tracks=iTunes.BrowserWindow.SelectedTracks
If Tracks is Nothing Then Set Tracks=iTunes.BrowserWindow.SelectedPlaylist.Tracks
If Tracks is Nothing Then
  Count=0
Else 
  Count=Tracks.Count
End If
If Count=0 Then
  MsgBox "Please select some tracks before calling this script",vbExclamation,Title
Else
  I=1
' List properties here as required
' Refer to iTunes COM SDK to see what can be accessed via script commands
  Do
    T="Viewing" & tab & tab & I & " of " & Count & nl & nl
    With Tracks(I)
      T=T & "Track" & tab & tab & .TrackNumber
      If .TrackCount>0 And .TrackCount>=.TrackNumber Then T=T & " of " & .TrackCount & nl Else T=T & nl
      T=T & "Name" & tab & tab & .Name & nl
      T=T & "Artist" & tab & tab & .Artist & nl
      T=T & "Album" & tab & tab & .Album & nl
      T=T & "Bitrate" & tab & tab & .Bitrate & nl
      T=T & "KindAsString" & tab & .KindAsString & nl
      T=T & "Plays" & tab & tab & .PlayedCount & nl
      T=T & "Skips" & tab & tab & .SkippedCount & nl
      T=T & "Last Played" & tab & FormatDate(.PlayedDate) & nl
      T=T & "Last Skipped" & tab & FormatDate(.SkippedDate) & nl
      T=T & "Remember Position" & tab & .RememberBookmark & nl
      T=T & "Bookmark Time" & tab & FormatTime(.BookmarkTime) & nl
      T=T & "Kind/Video Kind" & tab & .Kind & "/" & .VideoKind & nl
      T=T & "Comments/Lyrics" & tab & (.Comment<>"") & "/" & (.Lyrics<>"") & nl      
      T=T & "Size" & tab & tab & FormatNumber(.Size,0,-2,-2,-2) & " bytes" & nl
      T=T & "Artwork" & tab & tab & .Artwork.Count
      If .Artwork.Count>0 Then
        AD=0
        AE=0
        For AC=1 To .Artwork.Count
          If .Artwork.Item(AC).IsDownloadedArtwork Then AD=AD+1 Else AE=AE+1
        Next
        If AD>0 Then T=T & ", " & AD & " downloaded"
        If AE>0 Then T=T & ", " & AE & " embedded"        
      End If
      T=T & nl & nl
      T=T & "Path" & tab & tab & Wrap(.Location) & nl
      If Count>1 Then T=T & nl & nl & "Yes : Last   -   No : Next   -   Cancel : Exit            "  
    End With
    If Count>1 Then
      R=MsgBox(T,vbQuestion+vbYesNoCancel,Title)
    Else
      R=MsgBox(T,vbQuestion+vbOKCancel,Title)
    End If
    If R=vbYes And I>1 Then I=I-1
    If R=vbNo And I<Count Then I=I+1
  Loop Until R=vbCancel Or R=vbOK
End If


' Format date as text
' Modified 2011-10-06
Function FormatDate(D)
  If D=0 Then
    FormatDate="Never"
  Else
    FormatDate=D
  End If
End Function


' Format time interval from x.xx seconds to hh:mm:ss
' Modified 2011-10-06
Function FormatTime(T)
  If T<0 Then T=T+86400         ' Watch for timer running over midnight
  If T<10 Then
    FormatTime=FormatNumber(T,2) & " seconds"
  ElseIf T<60 Then
    FormatTime=Int(T) & " seconds"
  Else
    Dim H,M,S
    S=Right("0" & (T Mod 60),2)
    M=Right("0" & ((T\60) Mod 60),2)  ' \ = Div operator for integer division
    H=T\3600
    If H>0 Then
      FormatTime=H & ":" & M & ":" & S
    Else
      FormatTime=M & ":" & S
      If Left(FormatTime,1)="0" Then FormatTime=Mid(FormatTime,2)
    End If
  End If
End Function


' Wrap long strings
' Modified 2011-10-06
Function Wrap(T)
  Dim P
  P=Instr(40,T,"\")
  If P Then Wrap=Left(T,P) & nl & tab & tab & Wrap(Mid(T,P+1)) Else Wrap=T
End Function
