ActiveScripting Examples

JScript

<html>
<head>
<title>JScript Example</title>
</head>
<body>
<pre>
//*****************************************************//
// ActiveScripting example (JScript)
//*****************************************************//
// Generates a table of the filenames of all open documents,
// listing their modified status and read-only property.
//****************************************************//
function Main (){
  var newline;
  var br; 
  var tab; 
  var sTable;
  var count;
  
  newline = '\n';  
  br = '<BR>';          
  tab = '\t';
with (Application){
  // start the table
  sTable = '<B><FONT size="+1" color="Blue">'; 
  sTable = sTable + 'Names and properties of all open documents:';
  sTable = sTable + '</FONT></B><br><br><br>';
  sTable = sTable + '<TABLE border="1" width="500">' + newline;  
  sTable = sTable + newline + '<TR><TD><B>Document Name</B></TD>';
  sTable = sTable + '<TD><B><CENTER>Modified</CENTER></B></TD>';
  sTable = sTable + '<TD><B><CENTER>Read-Only</CENTER></B></TD>';
  sTable = sTable + '</TR>' + newline;
      
    count = 0; 
    
  // loop through all open documents and put their
  // names and properties into the table. 
  
  while (count <= (DocumentCount - 1)){
      
      // get document's name 
      
      fname = GetDisplayName(DocumentCache(count).FileName);  
      
      //extracting the document's name only without its path
      
      sTable = sTable + tab + '<tr><td>' + ExtractFileName(fname);
      sTable = sTable + '</td><td>';

           //Is this document Modified?
      
      if (DocumentCache (count).Modified)
         sTable = sTable + '<center> Yes'+'</td><td></center>';
      else 
         sTable = sTable + '<center> No' + '</td><td></center>';
      
      //Is this document Read-Only? 
      
      if (DocumentCache (count).ReadOnly)
         sTable = sTable + '<center>Yes'+ '</td><td></center> ';
      else 
         sTable = sTable + '<center>No' + '</td><td></center> ';
      
      // close row. 
      
      sTable = sTable + '</td></tr>' + newline;
      
      count ++;  // increment count by 1.
  }
  
  sTable = sTable + newline + '</table><br><br>';      
  
  sTable = sTable + '<b><i><font color="#0000ff"> You ran this script inside </font></i></b>'; 
  sTable = sTable + '<b><font color="#ff0000">'+ VersionText + '.</font></b>';
  
  NewDocument (false);
  
  ActiveDocument.InsertText (sTable, false);
  
  CurrentView = 2;
      
    }
}  
  
//*****************************************************//
// If the filename is an empty string, either set it 
// to untitled or don't change it.
//*****************************************************//

function GetDisplayName (fname){
  if (fname == '')
    return '(untitled)';
  else
      return fname;
}
</pre>
</body>
</html>

VBScript

<html>
<head>
  <title>VBScript Example</title>
</head>

<body>

<pre>

//********************************************************//
// Displays a table containing file information
//********************************************************//
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
</pre>

</body>
</html> 

Comments