border

PerfectScript Journal

December 1998: Repeating a series of commands in macros

One of the most powerful things about using macros is that they automate your work. They can take a repetative task and do it much faster than we humans can. WordPerfect provides several ways to help you automate that automation, and to reuse macro code.

For example, you may have a list of items in a file, and you need to change something in each item. This may be something like adding a word at the end of the line, or removing a bunch of spaces at the beginning of the line. You can record a macro that completes the task for one line, and then run that macro several times until each line of information has been altered. This article will show you several methods for replaying those macro commands.

The Repeat Feature

WordPerfect has a feature called Repeat Next Action (Repeat in WPWin 7 and earlier) that will repeat an action a given number of times. This feature is commonly used to insert a string a characters in a document (such as asterisks to form a line), but also can be used to repeat macros a specific number of times. Generally, this method should be used with simple macros that type in text, or perform some type of formatting on your document.

To use this option, create your macro so that it performs the tasks you need. When you are ready to start running the macro, click Edit > Repeat Next Action on the menu. The Repeat dialog will appear, allowing you to enter the number of times you want the macro repeated. Type the value, then click OK.

Next, click Tools > Macro > Play, then select the macro that you want to be repeated. The macro will be played the specified number of times.

The LABEL/GO commands

Commands within a macro can also be repeated with LABEL and GO commands. This method allows you to repeat just a few steps in the macro. This method is useful when the macro performs a search for some string of characters, then manipulates the document at that position. When the macro can no longer find the search string, the macro will automatically quit. Here's an example:

PosDocTop()
OnNotFound(End)
LABEL(top)
Searchstring("x")     // look for this string
MatchPositionAfter()  // place cursor after that string
SearchNext()          // perform search
Type(" ")             // type a space after the search char
GO(top)

LABEL(end)
      

This macro will repeat itself until "x" is no longer found. We added the OnNotFound command so that we don't have to see a Not Found error message when the search fails.

The REPEAT/UNTIL commands

Like LABEL and GO, the REPEAT / UNTIL commands will cause a set of commands to repeat until the expression specified in the UNTIL command is true. The example below will do the same thing as the example for the LABEL/GO commands:

PosDocTop()
NotFound(off!)
REPEAT
SearchString("x")     // look for this string
MatchPositionAfter()  // place cursor after that string
SearchNext()          // perform search
Type(" ")             // type a space after the search char
UNTIL(?NotFound)
      

In this example, the NotFound(Off!) command suppresses the Not Found error.

REPEAT / UNTIL can be used to test other types of expressions also. For example, you can test whether a variable has reached a certain value, whether the cursor has reached a certain line number, and so on.

The commands within a REPAT/UNTIL loop will be invoked at least once, since the test comes at the end of the loop.

The WHILE / ENDWHILE commands

The WHILE command is similar to REPEAT. However, the test comes at the beginning of the loop, so the commands within the loop may never be invoked if the WHILE expession is false. In the following example, the WHILE is used to search for a space. If the character to the right of the space is also a space, it is deleted.

SearchString("")      // look for a space
MatchPositionAfter()  // place cursor after the space
SearchNext()          // perform search
WHILE(?RightChar = " ")
  DeleteCharNext()
ENDWHILE
      

CHAINing a macro

Still another method for repeating a set of macro commands is to chain a macro to itself. To accomplish this, just place the CHAIN command somewhere within the macro, specifying the macro's name in the paramter:

CHAIN("mymacro.wcm">
      

The FOR and FORNEXT commands

FOR and FORNEXT can also be used to repeat a serieis of macro commands. These commands were covered in the January 1998 issue of The PerfectScript Journal. Please click the Back Issues link to read that article.

FUNCTIONS and PROCEDURES

FUNCTIONS and PROCDURES are a great way to reuse code. See the article from the July 1997 issue of The PerfectScript Journal for more information on these commands.

Gotcha's to be aware of

When you use the Repeat feature, or use the FOR or FOREACH commands, or your commands include a search, your loop will normally be self-limiting: the executon will end when the commands have run a specified number of times, or when the search fails. But when you use REPEAT / UNTIL, WHILE / ENDWHILE, or CHAIN, you need to make sure to include some commands that will eventually end the loop. Otherwise you could get stuck in the loop forever. It would appear as though your computer had hung when it really is just doing what you told it to do. So make sure that your test expression with this type of loop will eventually be FALSE.

Also, make sure that the cursor is positioned in the correct place to begin its next round either at the very beginning or the very end of the looping commands. Otherwise, the macro may perform the same steps on the same line of text with each iteration of the loop.

For more information on this topic, and specific usage of the new commands, get a copy of my book.

For information on ordering my book see my web page.

Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your   particular purpose, or for your ability or inability to use the information. You   are advised to make backups of all relevant files before implementing any suggestion  or technique.
© Copyright 1997 by J. Jeppson.