spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column56


Using JavaScript in HoTMetaL PRO 6.0

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Checking Last Modified Date Example

In this example we show how to write a set of macros that will check whether any other program has modified a file currently being edited in HoTMetaL. The macros involved in this Check For Updates feature are On_Document_Open_Complete, On_Document_Activate, and On_Application_Activate. As explained earlier in this column, the names of these macros are predefined and should not be altered. The names specify the events that trigger the macros. This event-macro association is implicit and cannot be overwritten in any way. When you open a document, for example, the On_Document_Open_Complete is always called at the completion of the file opening. Here is its definition:

<MACRO name="On_Document_Open_Complete" lang="JScript"><![CDATA[
  var name = ActiveDocument.LocalFullName;
  if (Application.ReadableFileExists(name)) {
   // if document has never been saved, do nothing
    Application.Run("On_Document_Save");
  }
]]></MACRO>

We first extract the file name of the current document, name = ActiveDocument.LocalFullName, and then check that a readable file exists. We then run the macro On_Document_Save. The macro On_Document_Save demonstrates the usage of Microsoft's FileSystemObject as well as ActiveX Control. It's a good idea to review Column 55, OLE Automation in JavaScript. The main idea of this macro is to update the document's LastMod property as to reflect the current time of the document on disk:

<MACRO name="On_Document_Save" lang="JScript"<>![CDATA[
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var f = fso.GetFile(ActiveDocument.LocalFullName);
  var mod = Date.parse(f.DateLastModified);
  var props = ActiveDocument.CustomDocumentProperties;
  if (props.count != 0) {
    props.Add("LastMod", mod);
  }
]]></MACRO>

The macro creates an ActiveX control out of the FileSystemObject which is included in Microsoft's Scripting library:

var fso = new ActiveXObject("Scripting.FileSystemObject");

We then get the file attributes from the disk, f = fso.GetFile(name), and extracts its Last Modified Date, mod = Date.parse(f.DateLastModified). We create a collection of user-defined properties, props, by calling ActiveDocument's CustomDocumentProperties property. We initialized this collection with the mod property and its value of "LastMode".

The macro On_Document_Activate checks that the file on disk has the same Last Modified Date as the current document being edited by HoTMetaL. It prompts the user what to do in case the dates do not match. Here is the macro:


<MACRO name="On_Document_Activate" lang="JScript" id="44"
  tooltip="Hide_On_Document_Activate"
  desc="Runs Macro: Hide_On_Document_Activate"><![CDATA[
  // Do this for local documents only
  if (ActiveDocument.FullName == ActiveDocument.LocalFullName) {
    var name = ActiveDocument.LocalFullName;
    if (Application.ReadableFileExists(name)) {
     // if document has never been saved, do nothing
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var f = fso.GetFile(name);
      var newMod = Date.parse(f.DateLastModified);
      var props = ActiveDocument.CustomDocumentProperties;
      if (props.count != 0) {
        oldMod = props.Item("LastMod").value;
        if (oldMod != newMod) {
          var Yes = 6;
          var No = 7;
          var msg = "The disk version of this document has changed from the\n";
          msg += "version in memory.  Do you want to re-open the document?";
          var ret = Application.MessageBox(msg, 36, "Document Changed");
          if (ret == Yes) {
            ActiveDocument.Reload();
          }
          // Reset the timestamp regardless of the user's response
          // This will prevent the dialog from always showing
          Application.Run("On_Document_Open_Complete");
        }
      }
    }
  }
]]></MACRO>

We first check that the file is local, ActiveDocument.FullName == ActiveDocument.LocalFullName. Then we verify that the file is saved on a disk, Application.ReadableFileExists(name). Similarly to the previous On_Document_Open_Complete macro, we create an ActiveX control and extracts the file's Last Modified Date:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile(name);
var newMod = Date.parse(f.DateLastModified);

We call upon the current document's custom properties collection, props = ActiveDocument.CustomDocumentProperties and checks that the number of properties is not equal to 0. We read the property that we have saved earlier in the On_Document_Open_Complete macro, and assign it to oldMod:

oldMod = props.Item("LastMod").value

When we find inconsistency between oldMod (from the open document) and newMod (from disk), we scream, and ask the user whether to upload the file from disk:

var Yes = 6;
var No = 7;
var msg = "The disk version of this document has changed from the\n";
msg += "version in memory.  Do you want to re-open the document?";
var ret = Application.MessageBox(msg, 36, "Document Changed");
if (ret == Yes) {
  ActiveDocument.Reload();
}

Finally, we reset the active document's timestamp by mimicing the open operation:

Application.Run("On_Document_Open_Complete");

We want to extend this Check For Updates feature and trigger it not only when the document is activated, but also when the application becomes active. We define the On_Application_Activate macro that just calls the macro defined above:

<MACRO name="On_Application_Activate" lang="JScript"><![CDATA[
  Application.Run("On_Document_Activate");
]]></MACRO>

Now we need to duplicate the On_Document_Save functionality to the On_Document_SaveAs macro:

<MACRO name="On_Document_SaveAs" lang="JScript"<>![CDATA[
  Application.Run("On_Document_Save");
]]></MACRO>

Let's test it now. Open a document in HoTMetaL PRO 6.0. Open the same document in another editor of your choice. Insert a blank character somewhere and save to disk. When you go back and activate the HoTMetaL application, you will get the following message:

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior


Created: January 18, 2000
Revised: January 18, 2000

URL: http://www.webreference.com/js/column56/refresh.html