Javascript example tutorial (19) Use HoTMetal (5)

Source: Internet
Author: User

Use javascript in HoTMetal

5. How to write a script to check the Last modified Date

In this tutorial, you will learn how to write a macro to check whether any program has used HoTMetaL to modify a file. This macro includes the update features of the following checks: On_Document_Open_Complete, On_Document_Activate, and On_Application_Activate. In the previous tutorial, the macro names have been predefined, so they cannot be modified here. These names specify events to trigger macros. This event-macro Association is implicit, so it cannot be rewritten by any means. When we open a document, such as On_Document_Open_Complete, it is always called when the file is opened. <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>

First, extract the file name of the current folder: name = ActiveDocument. localFullName, and then check whether the readable file exists. Then we run the macro On_Document_Save. This macro On_Document_Save demonstrates how Microsoft's FileSystemObject is used as an ActiveX control, which is in JavaScript. The main idea of this macro is to update the LastMod attribute of the document to reflect the current event of the document on the 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 the Microsoft Script Library: var fso = new ActiveXObject ("Scripting. FileSystemObject ");

We can use the following statement to obtain the file attributes from the disk: f = fso. getFile (name), and then extract the last modified event of the file: mod = Date. parse (f. dateLastModified ). We created a user-defined property set: props by calling the activemdocumentproperties attribute of ActiveDocument. Then we use the mod attribute to initialize the set. The value of this set is "LastMode ".

Use Javascript in HoTMetal

5. How to write a script to check the Last modified Date

The On_Document_Activate macro checks whether the files on the disk have the same last modified date as the current file edited using HoTMetaL. It prompts the user what to do in case the date does not match. The specific code for this macro is as follows:

<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 ents 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>

Check whether the file is loaded with ActiveDocument. FullName = ActiveDocument. LocalFullName. Then, verify whether the file is saved to the disk: Application. readableFileExists (name ). similar to the previous On_Document_Open_Complete macro, we create an ActiveX control and extract the last modification date 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 );
Use Javascript in HoTMetal

5. How to write a script to check the Last modified Date

Next, we call the custom property set props = ActiveDocument. CustomDocumentProperties of the current document and check whether the number of this property is not equal to zero. We have saved the previous On_Document_Open_Complete macro and assigned it to oldMod:

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

When we find the conflict between oldMod (from open document) and newMod (from disk), we should tell the user whether the file is reprinted from the 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 date of the current document by imitating the open operation:

Application. Run ("On_Document_Open_Complete ");

We want to extend this update feature check and trigger it, regardless of whether the document is current or when the application is current. In this case, we can define the On_Application_Activate macro, which only calls the above macro:

<MACRO name = "On_Application_Activate" lang = "JScript"> <! [CDATA [

Application. Run ("On_Document_Activate ");

]> </MACRO>

Now we need to copy the On_Document_Save function to the On_Document_SaveAs macro:

<MACRO name = "On_Document_SaveAs" lang = "JScript" <>! [CDATA [

Application. Run ("On_Document_Save ");

]> </MACRO>

Finally, let's test it. Open a document in HotMetaL PRO 6.0. Open the same document in your favorite editor. Insert a space character anywhere and save it to the disk. When you switch to the HoTMetaL application, you will get 1 information.

(Figure 1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.