javascript| Tutorial
Using JavaScript in HoTMetaL
5. How to write a script to check the date of the last modification
In this tutorial you will learn how to write a macro to check if any program has modified a file using HoTMetaL. This macro includes updated features for the following checks: On_document_open_complete, On_document_activate, and On_application_activate. In the previous tutorial, the names of these macros have been predefined, so they cannot be modified here. These names specify events to trigger the macro. This Event-macro association is implied, so it cannot be rewritten by any means. When we open a document, such as On_document_open_complete, it is always invoked when the file is opened. The following are specific definitions: <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 filename of the current folder: Name = Activedocument.localfullname, and then check that the readable file exists; then we run the macro On_document_save, the macro On_document_ Save demonstrates Microsoft's FileSystemObject as a way to use ActiveX controls, which is a JavaScript. The main idea of this macro is to update the document's Lastmod property to reflect the current event on 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>
This macro creates an ActiveX control from FileSystemObject, which includes Microsoft's script library: var fso = new ActiveXObject ("Scripting.FileSystemObject");
We can use the following statement to get the properties of the file from the disk: F = fso. GetFile (name), and then extracts the last modified event of the file: MoD = Date.parse (f.datelastmodified). We created a user-defined set of properties by calling the ActiveDocument CustomDocumentProperties property: Props. Then we use the MoD attribute to initialize the set, and the value is "Lastmode".
Using JavaScript in HoTMetaL
5. How to write a script to check the date of the last modification
This on_document_activate macro checks whether the file on the disk has the same last modification date as the current document that was edited using HoTMetaL. It prompts the user what to do in case the date does not match. The following is the specific code for this macro:
<macro name= "On_document_activate" lang= "JScript" id= "tooltip=" "Hide_on_document_activate"
Desc= "runs Macro:hide_on_document_activate" ><! [cdata[
Do this for local documents
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 your want to re-open the document? ";
var ret = Application.messagebox (msg, +, "Document Changed");
if (ret = Yes) {
Activedocument.reload ();
}
Reset the timestamp regardless of the user ' s response
This would prevent the dialog from always showing
Application.Run ("On_document_open_complete");
}
}
}
}
]]></macro>
We again check whether the file is loaded: Activedocument.fullname = = Activedocument.localfullname. Then we verify that the file is saved to disk: Application.readablefileexists (name). Similar to the previous On_document_open_complete macros, we created an ActiveX control and extracted the date of the last modification of the file, the code is as follows:
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var f = fso. GetFile (name);
var newmod = Date.parse (f.datelastmodified);
Using JavaScript in HoTMetaL
5. How to write a script to check the date of the last modification
Next, we call the custom set of properties for the current document: props = activedocument.customdocumentproperties and check that the number of this attribute is not equal to zero. We have already saved it in the previous On_document_open_complete macro and assigned it to Oldmod:
Oldmod = props. Item ("Lastmod"). Value
When we find inconsistencies between oldmod (from open documents) and NEWMOD (from disk), we should tell the user whether the file was reproduced 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 your want to re-open the document? ";
var ret = Application.messagebox (msg, +, "Document Changed");
if (ret = Yes) {
Activedocument.reload ();
}
Finally, we reset the date of the current document by imitating the open operation:
Application.Run ("On_document_open_complete");
We want to extend the check of this update feature and trigger it, regardless of whether the document is current or when the application is current. At this point we can define the On_application_activate macro, which simply calls the macro above:
<macro name= "On_application_activate" lang= "JScript" ><! [cdata[
Application.Run ("On_document_activate");
]]></macro>
Now we need to replicate On_document_save functionality to On_document_saveas macros:
<macro name= "On_document_saveas" lang= "JScript" <>! [cdata[
Application.Run ("On_document_save");
]]></macro>
Finally, let's test it. First, open a document in HoTMetaL PRO 6.0. and open the same document in your favorite editor. and insert a spaces anywhere, and then save it to disk. When you switch to the HoTMetaL application, you will be able to get the information as shown in Figure 1.
(Figure 1)