Tutorial
Macros: Writing Macros with Menus
The tutorial looks at the writing macros with menus. Menus can be particularly helpful if you run out of unused
tool buttons. It assumes that you are already familiar with macros. If not,
it is suggested that you look at the first 3 lessons in the
Introduction to macros
tutorial.
Let's assume that you want to write some web pages that use a style sheet called "MyStyles.css". Moreover
you know that it is going to include a "bordered" table with several rows having exactly 2 columns whose data
is to be centered.
{Comment Setup options menu
}
{MenuOption}{MenuOptionS Stylesheet}{MenuOptionT Table}{MenuOptionR Row}{Comment
Display option menu
}
{ShowMenu}{Comment
Style
}
{IfOptionS {TAB}{Literal on}
<link rel="stylesheet" type="text/css" href="MyStyles.css">
{Literal off}
}{Comment
a new "border table"
}
{IfOptionT {Literal on}
<table class="borderTable">
</table>{Literal off}{UP}
}{Comment
A new "centered row"
}
{IfOptionR {Literal on}
<tr class = "center">
<td>
</td>
<td>
</td>
</tr>{Literal off}{UP}{UP}{UP}{UP}{UP}
}
If you copy or type this macro, make sure that you don't include any blank
spaces or lines at the of the macro.
Right click to download HtmlTools.macro.
There are several possible way to set up macros for this situation. The macro shown on the right is possibly
one of the simpler ones but may not be the most useful. The option commands are shown in red, Other macro
commands and keys are in black. The html code that can actually be written into the file is shown in green.
Before looking at the code, here is the popup menu it sets up. Normally the menu will appear underneath the
programmable tool buttons but if the MultiEd window is too narrow, it will appear in the upper right courner.
Now let's look at some of the code:
- {MenuOption} or {MenuOption Clear}: Clears the menu.
This is necessary to
avoid adding to the menu each time the macro is run. The alternate form specifying "Clear" is probably easier to
understand. (That is, it is probably clearer!)
- {MenuOptionS Stylesheet}: This sets the first menu item and assigns it to the option "S".
Any printable character can be used. Notice that the next two items assign options "T" and "R". The option character
is treated in an case insensitive manner.
- {ShowMenu}: This command displays menu that has been set up by the {MenuOptionX ...}
items preceding it. Note: {Option} is a synonym for {ShowMenu}.
- {IfOptionS ...}: If the user clicked the popup menu item "Stylesheet" then MultiEd responds
by processing the "..." text. In this case, it types
<link rel="stylesheet" type="text/css" href="MyStyles.css">
The text had to be enclosed in {Literal on} and {Literal off} because it includes the meta characters "<" and ">" which
would normally mean press and release the control key instead of being typed.
The cursor should be at an appropriate place in the head section before this option is selected.
- {IfOptionT ... }: If the user clicked "Table" then this option is selected and MultiEd
types:
<table class="borderTable">
</table>
and moves the cursor up to the blank line. Again the cursor should be in the proper places before this option is used.
- {IfOptionR ... }: If the user picked "Row" then MultiEd will type:
<tr class = "center">
<td>
</td>
<td>
</td>
</tr>
Finally, the cursor is moved upwards to the position needed to enter text for the first data item.
Question: How would we install this macro? There are at least four ways.
- We could copy it into the Macro Control and just click the "Run" button each time it is used.
- We could load it into a programmable tool button after loading it into the Macro Control. Then we would
just click the specified tool button to use it. The Macro Control could be closed or used for another macro. If desired,
clicking the Save button in the Tool Setup form would save the macro for future use and it would not be necessary to repeat
this process every time we opened MultiEd.
- We could save the macro in a file in the same folder as the file being edited and write a trivial macro that
would run it either from the Macro Control or from a tool button. The tool button could be programmed as
follows assuming the file is called "MacroTools.html":
- Tool name: Macro Tools
- Required file types: html (or html htm if you also work with htm files)
- Save current file: your choice
- Window name or "Macro": Macro
- Keys sent to window: {read %path%HtmlTools.macro}
(The last macro (i.e. {read %path%MacroTools.html}) is all that is needed if we use the Macro Control.)
When this method is used, the user will see exactly the same thing as the previous two methods after clicking
the "Run" or tool button. The computer just works a little harder as it reads the macro from the file. There are
two advantages. First, it is easier to edit the file than to edit the macro when it stored in a tool button.
Second, we can have a customized "MacroTools.html" files in different folders.
If you like to use different subfolders for different HTML projects, the "Keys set to window" could be
modified to specify the folder holding MacroTools. In fact, if all the files are in subfolders of the same folder
then the MacroTools file could be saved in the parent file and "Keys sent to window" value could be modified to
read "{read %path%..\MacroTools.html}" because "%path%..\" repesents the parent folder of the current
file's folder.
- The macro could be saved in MultiEd's Templates subfolder (or the currently used custom initialization folder's
Template subfolder)
In this case the Key sent to window field given in the previous item would be changed to
- Keys sent to window: {read %initdir%templates\MacroTools.htm}
If the folder contained additional macros that you might like to pick from, you could use
- Keys sent to window: {read %initdir%templates\*.macro}
Note: When typing the {MenuOptionX ... } or {IfOptionX ... }" commands, the option character
must immediately follow "MenuOption" or "IfOption" and then be followed by blank before giving the menu item text
or the commands and keys that
are to be processed when the option is selected. Any non-blank character can be used for the option X.
Just be sure that MenuOption and IfOption use the same character.
Using the macro
In the following example, we will assume that the macro is stored in a tool button.
The suggested way of using this macro would normally be as follows:
- Open a new .html file saved with an appropriate name. We used "Example".
- Click the template tool button and pick the appropriate template. We will assume that it is
"template-XHTML1.0Strick.html". The boiler plate for the typical xhtml file is typed.
- Move the cursor just before the </head> and click the tool button for this macro
and select "Stylesheet" to have the Style tag typed into the document.
- When one is ready for the table, move the cursor to the appropriate place and click the
tool button and select "Table". The <table ...> and </table> tags are typed.
- The cursor will be in the appropriate location for the first row so click the macro's tool
button again and select "Row".
After completing these steps the result would be.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Example</title>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="stylesheet" type="text/css" href="Flowers2009.css">
</head>
<body>
<table class="borderTable">
<tr class = "center">
<td>
</td>
<td>
</td>
</tr>
</table>
</body>
</html>
That is a lot of code written with just a little clicking. One could add extra rows as needed and type the
rest of the page.
To help writing these tutorials, a much more extensive macro, Tutorial macro,
was develovoped. (The easiest way to use this macro would be to RIGHT click and download the file and then
open the Macro Control and open the file in that control. It could then be saved in a tool button.)
It is found in the Tutorial subfolder. It has 15 menu items that were designed to supply HTML tags and code used in when
writing these macros. With minor changes it could be adopted for other uses.
The next tutorial Submenus shows how to write macros with submenus.
Return to the beginning of this file
Return to the tutorial home page
Revised: 10/5/2011