' ------------------------------------------------------------------------------------
'				SaveLoadComments.vbs
' This script can export all the comments associated to the pictures of the current
' album in a text file.
' It can also reload the comments from the file and assign them to the pictures of
' the current album (comment are assigned to pictures having the same filename).
' ------------------------------------------------------------------------------------


Option Explicit

app.ClearTrace

' Define the variables
dim alb, nb, i, pic, fso, f
Const ForReading = 1, ForWriting = 2
Const sFile = "c:\CommentFile.txt"

set alb = app.GetCurrentAlbum
Set fso = CreateObject("Scripting.FileSystemObject")

dim s, k
s = "Do you want to:" & vbCRLF
s = s & "   - Save the comments to a text file: click 'Yes'" & vbCRLF
s = s & "   - Load the comments from a text file: click 'No'" & chr(13)
k = MsgBox( s, vbYesNoCancel or vbQuestion, "SaveLoadComments" )

if k = vbYes then
  Set f = fso.OpenTextFile( sFile, ForWriting, True )
  nb = alb.nbPicture
  app.Trace "Pictures in this album: " & nb
  for i=0 to nb-1
    Set pic = alb.GetPicture(i)
    'pic.bSelected = True
    if pic.sComment <> "" then
      app.trace pic.sShortFileName & " --> " & pic.sCommentFirstLine
      f.WriteLine pic.sShortFileName
      f.WriteLine pic.sComment
      f.WriteLine "."
    end if
  next
  f.close
  set f = Nothing

elseif k = vbNo then
  Set f = fso.OpenTextFile( sFile, ForReading, True )
  dim sPic, sCmt
  sPic = ""
  sCmt = ""
  Do While f.AtEndOfStream <> True
    s = f.ReadLine
    if sPic = "" then
      sPic = s
    else
      if s = "." then	' Last line of comment
	set pic = alb.GetPictureByFileName( sPic )
	if not pic is Nothing then
	  pic.sComment = sCmt
	  app.trace vbTab & pic.sShortFileName & " <-- " & pic.sCommentFirstLine
	else
	  app.trace "Cannot find picture '" & sPic & "' in this album"
	end if
	sPic = ""
	sCmt = ""        
      else
	if sCmt <> "" then sCmt = sCmt & vbCRLF
	sCmt = sCmt & s
      end if
    end if
  Loop
  f.Close
  set f = Nothing

end if		' User has pressed the Cancel button
alb.redraw
app.Trace "Done !"

