Using JavaScript in HoTMetaL PRO 6.0: Checking Last Modified Date Example
Using JavaScript in HoTMetaL PRO 6.0
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:
Produced by Yehuda Shiran and Tomer Shiran
Created: January 18, 2000
Revised: January 18, 2000
URL: https://www.webreference.com/js/column56/refresh.html