' =======================================================================
'       ActiveScripting example (VBScript)
' =======================================================================
'       Displays a table containing the filenames of all open documents,
'       whether or not each document has been modified, and whether each
'       document is read-only.
' =======================================================================
Sub Main  
    dim app
    dim idx
    dim sTable
    dim newline, fname, br, tab
    dim nCurrentIdx
    newline = chr(13) + chr(10)
    br = "<br>"
    tab = chr(9)
    
    ' create app reference.  note that the Application object is only
    ' available from within HomeSite and Studio - to create the app object from an
    ' external script, use CreateObject("AllaireClientApp.TAllaireClientApp")    
    set app = Application
    
    ' save the index of the current document so it can be returned to
    nCurrentIdx = app.DocumentIndex
    
    ' start the table
    sTable = "<b><font color=Blue>Names of all open"_
           + " documents:</font></b>"_
           + br + newline + br + newline _
           + "<table border=1 width=460>"_
           + newline + "<tr>"_
           + "<td><b>Document</b></td>"_
           + "<td><b>Modified</b></td>"_
           + "<td><b>Read-Only</b></td>"_
           + "</tr>" + newline

    ' loop through all open documents (note that DocumentCount is 1-based,
    ' whereas DocumentCache() is 0-based)           
    for idx = 0 to app.DocumentCount - 1
         ' get document name (uses function example)
         fname = GetDisplayName(app.DocumentCache(idx).Filename)        
         sTable = sTable + tab + "<tr><td>" + fname
         sTable = sTable + "</td><td> "
         
         ' is this document modified?         
         if app.DocumentCache(idx).Modified then sTable = sTable + "Yes"
         sTable = sTable + "</td><td> "
         
         ' is this document read-only?         
         if app.DocumentCache(idx).ReadOnly then sTable = sTable + "Yes"
         
         ' close row
         sTable = sTable + "</td></tr>" + newline        
    next
    sTable = sTable + newline + "</table>"

    ' add a new document (False = don't create from default template - blank)
    app.NewDocument(False)
    
    ' insert the table (note that the ActiveDocument will be the new document
    ' created above)
    app.ActiveDocument.InsertText sTable, False
    
    ' switch to browse mode     
    app.CurrentView = 2    
    
    ' wait for user to re-enter edit mode
    while app.CurrentView <> 1
        ' Wait is a home-grown routine to make up for the loss of
        ' DoEvents in VBScript. it will pause for a given number of
        ' milliseconds without locking the interface
        app.Wait(100)
    wend
    
    ' return to the original document
    app.DocumentIndex = nCurrentIdx
    
    MsgBox "Script Completed."    
    
    ' free the references
    set app = nothing    
End Sub

' function example
function GetDisplayName(fname)
    if fname = "" then 
       GetDisplayName = "(untitled)"
    else
        GetDisplayName = fname
    end if
end function