JavaScript Instance Tutorial (19) using HoTMetaL (4)

Source: Internet
Author: User
Tags format object cdata window
Javascript| tutorial using JavaScript in HoTMetaL

4. How to write a document format script

Here's an example of a macro that formats a document according to the layout criteria of a predefined page. To see the effect of this macro, turn off the Enable source Layout (allow layout of Sources) button, select Tools (Tool)->customization (Custom) from the menu. We first open the application to initialize some global variables. In addition, the name of the macro must be N_application_open, the specific code is as follows:

<macro name= "On_application_open" lang= "JScript" ><! [cdata[

var viewwysiwyg = 0;

var Viewtagson = 1;

var viewsource = 2;

]]></macro>

This macro must be invoked when the HoTMetaL application is opened. Its sole purpose is to define three constants, which are used in other macros. These constants represent only three corresponding hotmetal views. To manipulate the window tabs on the left button corner of the HoTMetaL edit window, you can see the WYSIWYG (WYSIWYG) view, Tagson View and source program view.

The following macro checks whether the current view is a source view and formats the entire document according to the predefined criteria. If the visual map is not a source view, a message is printed and displayed to the user, and the user is told to switch views, as follows:

<macro name= "Format current Document" lang= "JScript" id= "153" desc= "Apply source layout to entire Document" >

<! [cdata[

if (Activedocument.viewtype = = Viewsource) {

Activedocument.layout ();

}

else {

Application.alert ("Applying source layout only works with source View.\nswitch to Source view and try again.");

}

]]></macro>

Note here that we have used two HoTMetaL objects here: ActiveDocument and application. This layout () method formats the current document. The alert () method pops up a warning box.
OK, now let's test this macro. Please open a document in Hottmetal and switch the view to the source program view. You can select one of these in the Progguide directory. And close the Enable Source Layout button by using tools (tool)->customization (custom). Then move one line to the right by adding a space and then invoke the Macro dialog box from the Tools menu. This shows a list of macros defined in the HOTMETAL.MCR file. Then run the Refresh Macros macro to load the new macro you just edited. You will see the format current document macro. You can run it and notice that the indented line returns to its original position. Next we test the alert () method. Now switch the view to Tagson view and run the macro again, and the dialog box will pop up as shown in Figure 1:



(Figure 1)

Now suppose you want to format the selected part of the document. The requested macro (called the format current Selection) is similar to the macro described above (format current Document). The difference between them is only in: Format current document the Layout () method is to manipulate the ActiveDocument object, and the format current selection Layout () The method is to manipulate the Selection object:

<macro name= "Format current Selection" lang= "JScript" id= "desc=" "Apply source layout to the current Selection" >

<! [cdata[

if (Activedocument.viewtype = = Viewsource) {

Selection.layout ();

} else{

Application.alert ("Applying source layout only works with source View.\nswitch to Source view and try again.");

}

]]></macro>
Now let's take a good look at how this macro works. We indented two lines in the open document. The first line starts with "does not specify", and the other is the "referred to in" MA: ". As shown in Figure 2:



(Figure 2)

Now we select three lines, and these three lines include rows that begin with "does not specify:" As shown in Figure 3:



(Figure 3)

Finally we click on the green arrow in the upper-left corner of the window, and the macro's name (Format current Selection) is displayed in the Drop-down menu window, as shown in Figure 4:



(Figure 4)

It is worth mentioning that the selected row has been formatted as the original paragraph boundary. The second line beginning with the "referred to in" is indented without being formatted.

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.