Last modified: 09/09/1999 00:17:12
These macros show how to automatically set some text in a page indicating the last time it was modified.
Copy the macros in macros.txt into the hotmetal.mcr file found in the Macros folder of your HoTMetaL PRO application folder. Then add a SPAN tag to your document with ID="SQ-LastModifiedDate". The contents of the SPAN tag will be filled in with the current date and time whenever you save the document.
An example of such a SPAN tag can be seen in the Last modified line near the top of this document.
Since anything added manually inside the SPAN tag will be deleted when the document is saved, it would be a good idea to make the tag read-only. We show how to do this in the Template Helpers scripts.
These two macros are called when the user has taken action to save the document, but just before the document is actually saved. It is a good time to perform any operations on the document that you want in the saved version. We use these macros as the place to update the last modified date.
Since both macros do the same thing, we just have them call a function which is defined in On_Application_Open (see the next section).
<MACRO name="On_Before_Document_Save" lang="JScript"><![CDATA[ InsertLastModifiedDate(); ]]></MACRO> <MACRO name="On_Before_Document_SaveAs" lang="JScript"><![CDATA[ InsertLastModifiedDate(); ]]></MACRO>
This is the macro where all the action happens. It defines the view constants and the function InsertLastModifiedDate (see preceding section).
We use the DOM to find the place(s) to insert the last modified date, so this is restricted to working in Tags On or WYSIWYG views. The function finds all SPAN tags and checks the ID attribute of each to see if it matches the special value for last modified date. When it finds a match, it replaces the contents of the SPAN with the current date and time.
<MACRO name="On_Application_Open" lang="JScript"><![CDATA[ // Some useful constants var viewWYSIWYG = 0; var viewTagsOn = 1; var viewSource = 2; // InsertLastModifiedDate - inserts date just before document is saved function InsertLastModifiedDate() { // Check what view we're in because we need the DOM var viewType = ActiveDocument.ViewType; if (viewType != viewWYSIWYG && viewType != viewTagsOn) return; // Look for SPAN tags with the special ID var rng = ActiveDocument.Range; var magicID = "SQ-LastModifiedDate"; var nodeList = ActiveDocument.getElementsByTagName("SPAN"); var i; for (i = 0; i < nodeList.length; i++) { var node = nodeList.item(i); if (node.getAttribute("ID") == magicID) { // Replace contents of this node with the current date and time rng.SelectBeforeNode(node); rng.MoveToElement("SPAN"); rng.SelectContainerContents(); var d = new Date(); var timestamp = d.toLocaleString(); // use your favorite date function here rng.TypeText(timestamp); } } } ]]></MACRO>