Corel WordPerfect Office - X5 User Guide for PerfectScript

?
s
;
W
0
g
6
t
=
9
}
3
*
&
m
7
A
#
%
!
@
92
2
5
+
User Guide for PerfectScript™
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Understanding macro concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Understanding macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Understanding macro statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Understanding macro syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Understanding macro structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Using expressions in macro statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Understanding variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Understanding constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Understanding operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Understanding expression types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Using command statements in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Understanding command names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Understanding parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Understanding return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Using assignment statements in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Using conditional statements in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Using loop statements in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Using calling statements in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Understanding labels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Understanding functions and procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Understanding callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Creating calling statements from subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Using comment statements in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Accessing external applications in macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Understanding OLE Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Understanding Dynamic Data Exchange (DDE) . . . . . . . . . . . . . . . . . . . . . . . . 80
Learning more about macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Using the WordPerfect Office Software Development Kit (SDK) . . . . . . . . . . . 81
Using the Corel Web site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Contents i
Getting started with macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
Using the PerfectScript utility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Specifying PerfectScript settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Creating macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Migrating legacy macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Recording macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Writing and editing macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Compiling macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Playing macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Making macros user-friendly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Creating dialog boxes for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
Understanding dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Setting up dialog boxes for macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Setting up controls for dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Setting up callbacks for dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Testing dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Displaying dialog boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Debugging macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Getting started with the PerfectScript Debugger . . . . . . . . . . . . . . . . . . . . . . . . . 135
Using the Debugger to debug macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Getting more information while debugging macros . . . . . . . . . . . . . . . . . . . . . . . 143
Working with breakpoints while debugging macros . . . . . . . . . . . . . . . . . . . . . . . 150
Working with variables while debugging macros . . . . . . . . . . . . . . . . . . . . . . . . . 156
Navigating the code while debugging macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Troubleshooting the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175
ii Contents

Introduction

Welcome to the Corel® WordPerfect® Office User Guide for PerfectScript™!
PerfectScript™ is a command-based macro-programming language that you can use to automate tasks in Corel® WordPerfect®, Corel® Quattro Pro®, and Corel® Presentations™. This documentation contains basic information about creating simple PerfectScript macros, as well as detailed, technical information about creating more complex PerfectScript macros.
This documentation contains the following sections:
• “Understanding macro concepts” on page 3 explains the concepts that are
associated with macros, and shows how these concepts apply to PerfectScript macros
• “Getting started with macros” on page 83 introduces you to the PerfectScript
utility, which you can use to create macros quickly and easily
• “Creating macros” on page 89 examines how to create macros, either by migrating
ones that already exist or by recording or writing new ones
• “Creating dialog boxes for macros” on page 101 describes how to use a dialog box
to create an interface between the application and the user
• “Debugging macros” on page 135 demonstrates how to find and correct any errors
in your macros
This documentation also contains a glossary.
Introduction 1
Please see the PerfectScript Help file (psh.chm) for the following additional sections:
• “PerfectScript Command Reference” documents the syntax elements and macro commands for PerfectScript
• “WordPerfect Command Reference” documents the system variables and macro commands for WordPerfect
• “Quattro Pro Command Reference” documents the syntax elements and macro commands for Quattro Pro, for both PerfectScript and the native Quattro Pro macro-programming language
• “Presentations Command Reference” documents the macro commands for Presentations
• “Gallery of sample macros” provides sample macros for PerfectScript and WordPerfect
2Introduction

Understanding macro concepts

When performing repetitive or complex tasks in WordPerfect Office, you can save time by using PerfectScript macros. In this section, you’ll learn the concepts that are associated with macros, and you’ll learn how these concepts apply to PerfectScript macros.
This section contains the following topics:
• Understanding macros
• Using expressions in macro statements
• Using command statements in macros
• Using assignment statements in macros
• Using conditional statements in macros
• Using loop statements in macros
• Using calling statements in macros
• Using comment statements in macros
• Accessing external applications in macros
• Learning more about macros

Understanding macros

A macro specifies a sequence of actions that you can quickly repeat later. For example, a macro can automate a WordPerfect task such as setting the margins, selecting a font, or creating a merge file.
To create macros for WordPerfect Office, you can use the PerfectScript macro­programming language. PerfectScript is called a “command-based language” because it uses macro commands to store the results of an action rather than storing the individual steps that are used to carry out that action.
You can also create macros for Quattro Pro by using the native macro­programming language for the application. For information, please see “Understanding the native Quattro Pro macro language” in the Quattro Pro Command Reference section of the PerfectScript Help file (psh.chm).
Understanding macro concepts 3
You can also use Microsoft® Visual Basic® for Applications (VBA) to create macros for WordPerfect Office. For detailed information about VBA and VBA macros, please see the Corel WordPerfect Office User Guide for VBA (vba_ug.pdf).
A macro consists of a set of instructions or statements. By using the various types of macro statements, you can create PerfectScript macros that automate anything from a basic task to a complex procedure. For more information about macro statements, see “Understanding macro statements” on page 4.
Through the use of macro statements, PerfectScript lets you create macros that access applications outside of WordPerfect Office. For more information, see “Accessing external applications in macros” on page 77.
For even more information about macros, you can consult additional resources for WordPerfect Office. For information, see “Learning more about macros” on page 81.
The proper form of macro components is governed by a set of rules, or syntax. For more information about macro syntax, see “Understanding macro syntax” on page 5.
If you structure your macros well, they will function well — and be much easier to edit. For more information about macro structure, see “Understanding macro structure” on page 5.

Understanding macro statements

If a macro represents a set of instructions, then a macro statement represents a single step in those instructions. The simplest macro consists of only one statement, while the most complex macro consists of multiple statements that are performed in sequence.
A group of related statements is called a “statement block.”
Some statements require an expression, which is a formula that represents a value. For more information about expressions, see “Using expressions in macro statements” on page 8.
By combining expressions with other macro components, you can create any of the following types of statements:
command statements — consist of a macro command, which represents a single instruction (typically, an action). For more information, see “Using command statements in macros” on page 47.
4 Understanding macro concepts
assignment statements — assign a value to an expression. For more information, see “Using assignment statements in macros” on page 54.
conditional statements — execute a statement (or a group of statements) when a specified condition is met. For more information, see “Using conditional statements in macros” on page 54.
loop statements — execute a statement (or a group of statements) a specified number of times until (or while) an expression is true. For more information, see “Using loop statements in macros” on page 57.
calling statements — call a statement (or a group of statements). For more information, see “Using calling statements in macros” on page 59.
comment statements — contain notes that explain the purpose of a macro without affecting its play. For more information, see “Using comment statements in macros” on page 77.

Understanding macro syntax

The proper form of macro components is governed by a set of rules, or syntax. For a macro to work properly, its code must use the correct syntax — that is, the code must be “syntactically correct.”
For each macro component that is described in this documentation, details on proper macro syntax are included.
Some macro statements are too lengthy to fit into a single line of macro code. If your macro editor automatically inserts a hard return at the end of every line, you must insert an underscore character (
_ ) at the end of each line that wraps.
For information on specifying a macro editor, see “To specify settings for editing macros” on page 86.

Understanding macro structure

If you structure your macros well, they will function well — and be much easier to edit.
You can structure a macro in several ways. The basic function of a macro is to accomplish a task by following a series of steps, so the ideal structure for a macro depends on the task involved — and on the amount of code that is required to carry out that task. For example, if a macro involves multiple tasks that require large amounts of
Understanding macro concepts 5
code, you can make the macro more manageable by breaking it into smaller pieces (called subroutines — see “Understanding subroutines” on page 59).
From a structural standpoint, the two main types of macros are as follows:
sequential macros — progress in steps from start to finish. For more information, see “Understanding sequential macros” on page 6.
procedural macros — progress in steps based on user intervention. For more information, see “Understanding procedural macros” on page 7.
Understanding sequential macros
A sequential macro progresses in steps from start to finish. All steps are taken in the required order, and the code is written to suit that purpose.
An example of a sequential macro follows:
HardReturn () HardReturn () GetString( var1; "Enter Name"; "Data Entry"; 100 ) Type (Text: var1) HardReturn () GetString( var2; "Enter Address"; "Data Entry"; 100 ) Type( var2 ) HardReturn () HardReturn () Type (Text: "Dear " + var1 + ":") HardReturn () HardReturn () Type (Text: "Yaddah Yaddah Yaddah") HardReturn () HardReturn () HardReturn () Type (Text: "Sincerely,") HardReturn () HardReturn () HardReturn () HardReturn ()
6 Understanding macro concepts
Type (Text: "Paul McRussell") HardReturn () Type (Text: "Manager, Eat-a-Chicken Burger, Anywhere, USA")
Understanding procedural macros
A procedural macro progresses in steps based on user intervention, through the use of functions and procedures (see “Understanding functions and procedures” on page 61). Using functions and procedures in a macro lets the programmer compartmentalize code so it can be called from anywhere in the macro. Compartmentalization breaks logical pieces of code into smaller segments, and these segments can be separated by use of the
Label, Function, and Procedure commands (see “Understanding subroutines” on
page 59). Smaller pieces of code are easier to work with, and they are also easier to debug.
An example of a procedural macro follows:
HardReturn () HardReturn () //Call the function to get the name sName = GetName() Type (Text: sName) HardReturn () //Call the function to get the address sAddress = GetAddress() Type (sAddress ) HardReturn () HardReturn () Type (Text: "Dear " + sName + ":") HardReturn () HardReturn () Type (Text: "Yaddah Yaddah Yaddah") HardReturn () HardReturn () HardReturn () Type (Text: "Sincerely,") HardReturn ()
Understanding macro concepts 7
HardReturn () HardReturn () HardReturn () Type (Text: "Paul Russell") HardReturn () Type (Text: "Manager, Eat-a-Burger, Anywhere, USA") Function GetName() GetString( sName; "Type in the name of the addressee"; _ "Enter Name"; 100 ) RETURN( sName ) EndFunction Function GetAddress() GetString( sAddress; "Type in the address of the addressee"; _ "Enter Address"; 100 ) RETURN( sAddress ) EndFunction

Using expressions in macro statements

Macros consist of statements. Some macro statements involve an action that must be captured as an expression. An expression is a formula that represents a value.
To create expressions, you use the following macro components:
variables — store a single value at a time, but this value can change during macro play. For more information, see “Understanding variables” on page 10.
constants — store a single value at a time, and this value cannot change during macro play. For more information, see “Understanding constants” on page 25.
operators — are symbols (such as
+, -, *, and %) that combine variables and
constants to determine a value. For more information, see “Understanding operators” on page 25.
Understanding expressions
Expressions are created by combining variables or constants (or both) with operators — or by combining other expressions with operators.
The following examples contain expressions that involve variables and operators.
8 Understanding macro concepts
Example Result
x := "John Doe"
vLeftMargin := 5i
ResultOfOperation := 3 + 4
z := z + 1
x := y > 1
If (y>1) Beep EndIf
The variable x equals the character string
John Doe.
The variable vLeftMargin equals the measurement
5i.
The variable ResultOfOperation equals
7 (that is, the result of the numeric
expression
3 + 4).
The variable z equals the value of z + 1. However, a variable can contain only one value at a time, so the original value of
z is
lost unless previously assigned to another variable.
The variable x equals the result of the relational expression
True if y contains a value greater than 1, or
it equals
False if y contains a value less
than or equal to
y > 1 (that is, x equals
1).
The result of y>1 is evaluated without assigning the result to a variable. The computer beeps if the value of
1 (that is, if the result of the expression
than
y>1 equals True). The beep is skipped if the
value of
y is less than or equal to 1 (that is, if
the result of the expression equals
y is greater
False).
The following example contains expressions that involve variables, constants, and operators. The value as constants. The operators
vCount - 1, vCount = 0, and vCount = 4. Function BeepBeep(vCount) Repeat Beep Wait(3) vCount := vCount - 1
Understanding macro concepts 9
vCount is used as a variable, while the values 0, 4, and -1 are used
- and = are used to create expressions from these values:
Until(vCount = 0) Return EndFunc ForEach(vCount; {1; 2; 3; 4; 5}) If(vCount = 4) Break EndIf BeepBeep(vCount) Wait(5) EndFor
MessageBox(x; "BREAK"; "Variable vCount equals 4"; IconInformation!)
Quit
For more information about the types of expressions that you can create, see “Understanding expression types” on page 40.

Understanding variables

A variable stores a single value at a time, but this value can change during macro play.
Variables must be “declared” before they can be used. Declaring a variable instructs PerfectScript to set aside memory for the variable.
Assigning a value to — or “initializing” — a variable involves pointing that variable to the memory cell where its desired value is stored. If desired, variables can be initialized with a value at the time of declaration. Although the value of a variable can belong to any data type, the most common data types for variables are numbers and character strings.
For more information about declaring and initializing variables, see “Declaring and initializing variables” on page 12.
Unlike other programming languages, PerfectScript does not force the programmer to specify the type of data to be stored in a variable.
10 Understanding macro concepts
When a variable is declared, it is assigned to one of four types:
local variables — pertain only to the current macro. By default, variables are automatically declared local if no variable type is specified. For more information, see “Working with local variables” on page 13.
global variables — pertain to the current macro and to macros that are called by the
Run and Chain commands. For more information, see “Working with global
variables” on page 15.
persistent variables — pertain to any PerfectScript macro, for as long as PerfectScript is running. For more information, see “Working with persistent variables” on page 16.
constant variables — represent a value that cannot change during macro play. For more information, see “Working with constant variables” on page 18.
Two additional kinds of variables require special attention:
•A system variable is a type of macro command that contains current system information such as the current chart type or the default directory. For example, the PerfectScript system variable contains the error value of a
Cancel, Error, or Not Found condition
ErrorNumber
(as illustrated in line 44 of the annotated macro sample ASSERT.WCM in the PerfectScript Help file [psh.chm]). Similarly, the WordPerfect system variable folder for WordPerfect macros to a variable named
?PathMacros assigns the path and name of the default
vMacroPath, which
is updated to reflect any changes to the directory. For more information about system variables, see “Understanding macro commands” on page 47.
•An implicit variable is a variable that is defined by PerfectScript. For example the
MacroDialogResult variable contains the control value
of the button that releases a dialog box (see “Releasing dialog boxes by using PerfectScript code” on page 132).
The type of a variable determines its visibility (or scope) and its duration in memory, so it’s important to understand when to use each variable type. If you try to access a variable from a line of code in which that variable is not visible, an “out-of-scope” error is generated.
Understanding macro concepts 11
In addition to variable type, the following factors determine the scope of a variable:
• where the variable is declared — for example, in the main body, in a function, in a procedure, in another macro, or in a separate program altogether
• which line of code is currently executing
You can check whether a variable exists. For more information, see “Determining whether variables exist” on page 18.
When a variable is no longer required, you can discard it. For more information, see “Discarding variables” on page 19.
If you want to assign a collection of data to a single variable name, you can use an array. The rules for using arrays are the same as for using variables. For more information, see “Working with arrays” on page 20.
Declaring and initializing variables
When you declare a macro, you specify a name for it.
For best results, it is highly recommended that you give your variables a descriptive name. Variable names have the following standards:
• They must begin with a letter.
• They can include any other combination of letters or numbers.
• They must be 50 characters or fewer in length.
• They are not case-sensitive.
Optionally, you can initialize a variable at its time of declaration by using an assignment operator (
:= or =) to specify a value.
By following a few simple conventions for naming variables, you can make your macro code easier to understand. For instance, variables that have a string value should have a name that begins with a lowercase
sFirstName := "Dave" sAddress := "1625 East Nowhere St." sBirthday := "6/12/69"
s, as in the following examples:
Similarly, variables that have a numeric value should have a name that begins with a lowercase
nAge := 25 nTotal := 145.97
12 Understanding macro concepts
n, as in the following examples:
In the preceding examples, all declared variables are local, by default, because no variable type is specified.
Working with local variables
Local variables pertain only to the current macro. Local variables are the default variable type and, as such, should be used in most situations. You can use the PerfectScript programming commands
Declare or Local to create local variables.
Variables that are declared in user-defined functions and user-defined procedures are local to those subroutines. For more information about subroutines, functions, and procedures, see “Understanding subroutines” on page 59.
Local variables can be declared in the following way:
Declare ( sQReport )
or
Local ( sQReport )
Local variables can be declared and intitialized in the following way:
sQReport := "Q4" Declare ( sQReport := "Q4" )
or
sQReport := "Q4" Local ( sQReport := "Q4" )
If you want, you can use the Declare command or the Local command to declare and initialize more than one local variable at a time. Variables are separated by a semicolon (
;), as in the following example:
Declare (sFilename := "c:\test.wpd"; sTemp; nCount; cMainCount:=10)
When a local variable is declared, it is assigned to the local-variable table. Variables in the local-variable table are visible only until the end of the level of code in which they are declared. The level of code usually refers to the main body or a subroutine (that is, a function or procedure). Consider the following sample code:
FileNew /* Declare sName as a local variable and initialize to the string
value "Dave" */ vName := "Dave"
Understanding macro concepts 13
/* Call the procedure */ TypeName ( ) Quit Procedure TypeName ( ) /* This variable is out of scope. It has not been declared in the
procedure TypeName */ Type ( vName ) HardReturn EndProcedure
The preceding code assigns the string "Dave" to the variable. It then calls the procedure which tries to type the contents of the variable. Because the variable is out of scope within this procedure, the following error occurs when playing the macro:
Undefined variable ‘VNAME’ has been referenced. Check line 9 of macro file ‘test.wcm.’
Consider the following sample code:
... /* Variables declared in the main body are visible in the main body */ vNameMain := "Dave" NewScope ( ) /* When processing this Procedure vNameMain is not visible */ NewScope2 ( ) /* When processing this Function vNameMain is not visible */ ... Quit // Subroutines . . . Procedure NewScope ( ) /* Local variables declared in a procedure are visible only i n that
procedure */ NameNewScope := "Fred" ... EndProcedure Function NewScope2 ( )
14 Understanding macro concepts
/* Local variables declared in a function are visible only in that function */
vNameNewScope2 := "John" ... EndFunction
In the preceding code, all three variables (VNameMain, NameNewScope, and
VNameNewScope2) are named differently. However, these variables could have been
named the same and still have been completely unique variables — each one holding different data — because they are each declared at a different level of the macro and therefore each have their own scope.
Working with global variables
Global variables pertain to the current macro and to macros that are called by the Run
Chain commands. Although a necessity in some cases, global variables should be
and used with care. You can use the PerfectScript programming command
Global to create
global variables.
Global variables can be declared in the following way:
Global ( nCount )
Global variables can be declared and initialized in the following way:
Global ( sFilename := "c:\Expense.wpd" )
If you create two variables with the same name (for example, Declare x and
Global x), the following statement specifies that the global variable x is assigned the
5:
value
Global x:=5
If you want, you can use the Global command to declare and initialize more than global variable at a time by separating variables with a semicolon (
;).
When a variable is declared global, it is assigned to the global-variable table. Variables assigned to the global-variable table are in scope anytime after they are declared, and they exist until the end of the macro in which they are declared. If a global variable is declared on the very first line of a macro, it is accessible in the main body, in subroutines, and in other macros that are started with the commands
Run or Chain. The following
is an example of a global variable in use:
... /* The global variable is not yet declared and not yet accessible */
Understanding macro concepts 15
Global ( sGlobalName := "Fred" ) /* Any reference after this to the variable sGlobalName accesses the
global variable */ ... /* Call the function */ DoSomething ( ) Type ( sGlobalName ) ... /* sGlobalName ceases to exist when the macro ends */ Quit Procedure DoSomething ( ) /* Change the value of the global variable to "Dave" */ sGlobalName := "Dave" ... EndProcedure
In the preceding example, the procedure is called after the global variable
sGlobalName is declared and initialized. Inside this procedure, the contents of the
variable are changed from
Procedure DoSomething allow you to start another macro from within the current
"Fred" to "Dave". The commands DoSomething and
macro and, therefore, to access and change any variables that are declared global in the current macro.
Working with persistent variables
Persistent variables pertain to any PerfectScript macro, for as long as PerfectScript is running. Although a necessity in some cases, persistent variables should be used with care. You can use the PerfectScript programming command persistent variables — in much the same way as you can use the
Persist to create
Global command to
create global variables.
Persistent variables can be declared in the following way:
Persist ( VariableName )
Persistent variables can be declared and initialized in the following way:
Persist ( VariableName := Value ) PersistAll ( On! ) ...
16 Understanding macro concepts
/* All variables declared in the default manner are now persistent instead of local*/
VariableName := Value ... PersistAll ( Off! )
The preceding example uses the PersistAll command to change the default variable­declaration method from local to persistent and back again. All variables between
PersistAll ( On! ) and PersistAll ( Off! ) are declared as Persistent
variables. This technique is useful when you want an entire block of variables to be persistent.
If you want, you can use the than persistent variable at a time by separating variables with a semicolon (
Persist command to declare and initialize more
;).
When a variable is declared persistent, it is assigned to the persistent-variable table. Variables in the persistent table remain in scope and exist until PerfectScript shuts down.
PerfectScript does not shut down until all the applications that use PerfectScript (WordPerfect, Quattro Pro, and Presentations) have shut down.
Persistent variables are visible during merges and, as such, provide an effective method for passing values between macros and merges. If you need to use data during a merge, use persistent variables. For best results, give persistent variables a descriptive name, and denote their data type.
The following example of a persistent variable requires the use of two macros and includes a test that determines whether the variable has been initialized.
The first macro in this example is as follows:
Persist ( sAppName := "WordPerfect Suite 8" ) MessageBox( retVal; sAppName; "Left margin equals: " + ?Marg inLeft ) Run ( "Macro2.wcm" )
The second macro in this example is as follows:
MessageBox(retVal; sAppName; "Right margin equals: " + ?MarginRight)
The following example illustrates scope by using local and persistent variables:
Persist ( x := "This is persistent variable x" ) CreateOutline()
Understanding macro concepts 17
// Original variable value remains unchanged. MessageBox ( retVal ; "Information"; "The variable x = " + x ) Quit Procedure CreateOutline ( ) /* PerfectScript will look first at the local variable table. If a
variable exists in that table, that variable will be used before the persistent variable. By creating a local variable inside the function, we will force PerfectScript to find the local variable. This local variable x will be destroyed when execution returns from this subroutine. */
Local ( x := 0 ) ForNext ( x; 1; 10 ) // for loop creates a basic outline vCharacter := NTOC(96) + x Tab() Type ( "(" + vCharacter + ")" ) Indent() HardReturn() EndFor EndProcedure
Working with constant variables
Constant variables — also called “constants” — represent a value that cannot change during macro play. As such, constants must be initialized upon declaration, and their assigned value cannot change. Constants should be used sparingly, if at all.
Expressions are formed by using operators (see “Understanding operators” on page 25) to combine constants with other types of variables. For more information about constants, see “Understanding constants” on page 25.
Determining whether variables exist
You can use the Exists command to determine whether a variable exists — that is, whether it has been declared and initialized. The following sample code shows how to use the
// Declare and initialize a variable’s Name := "Fred" // Use Exists to see if the variable still exists as a local variable If ( Exists ( sName; Local! ) )
18 Understanding macro concepts
Exists command:
... EndIf ... Quit
The Exists command returns a value after checking the specified variable against the variable tables. The variable tables are checked in the following order: local, then global, then persistent.
If you specify a variable-table parameter for the
Exists command, a value of True is
returned if that variable is found in the specified variable table. If the variable is not found in the specified variable table, a value of
False is returned. The following
example illustrates this scenario:
If( Exists ( x ; Global!) = TRUE ) x := 147 Else Global ( x := 147 ) EndIf
If you do not specify a variable-table parameter for the Exists command, one of the following values is returned:
NotFound! or 0 — indicates that the variable does not exist in any variable table
Local! or 1 — indicates that the variable exists in the local-variable table
Global! or 2 — indicates that the variable exists in the global-variable table
Persistent! or 3 — indicates that the variable exists in the persistent-variable
table
The following example illustrates this scenario:
Persist( x := 3 ) If ( Exists( x ) = Exists.Persistent!) MessageBox ( retVar; "Variable"; "This variable Exists in the _ Persist variable pool (" + Exists ( x ) + ")" ) EndIf
Discarding variables
You can use the Discard command to remove a variable from memory by deleting it from its associated variable table. The following sample code shows how to use the
Discard command:
Understanding macro concepts 19
// Declare and initialize a variable sName := "Fred" ... // Free the memory used by vName Discard ( sName ) // sName no longer exists and cannot be accessed ... Quit
The Discard command searches the variable tables in the following order: local, then global, then persistent. If variables with the same name exis t in d ifferent variable tables, you may need to use the
Discard command multiple times, as in the following sample
code:
While(Exists(VariableName)) Discard(VariableName) EndWhile
Working with arrays
If you want, you can assign a collection of data to a single variable name by creating an “array.” The elements in a PerfectScript array can be declared and initialized in the same ways as variables. Unlike other programming languages, however, PerfectScript lets you assign the elements in an array to different data types. PerfectScript arrays therefore provide a powerful way to control large amounts of data on one or more dimensions.
To use an array, you must first declare it and initialize its elements. When a macro is played, a run-time error is incurred for each array element that is not both declared and initialized.
The commands for declaring arrays are the same as for declaring variables:
Local, Global and Persist. At declaration, an array requires the following items:
Declare,
• an alphanumeric (case-insensitive) name that begins with a letter and is limited to 50 characters
• a subscript, marked in brackets (
[]), that specifies how many array elements to
create
The following commands declare a one-dimensional array that contains five elements:
Declare(aMyArray[5]) — declares a one-dimensional, five-element local array
Local(aMyArray[5]) — declares a one-dimensional, five-element local array
20 Understanding macro concepts
Global(aGlobalArray[5]) — declares a one-dimensional, five-element global
array
Persist(aPersistArray[5]) — declares a one-dimensional, five-element
• persistent array
Every array contains a hidden element called
0. This element stores the total
number of elements in the array (not including itself), and an attempt to assign any other value to this element generates an error message. In the previous examples, the declared array actually includes six elements if you include element
0.
The preceding examples declare an array but do not initialize its elements. Before you can use an array, you must individually initialize each array element. To initialize an array element, you must specify the array name; the subscript (or index) number of the array element, enclosed in brackets (
[]); and the desired value for the array element.
The following example illustrates how to initialize each element in an array after declaring the array.
// Declare a 5-element array Declare ( aMyArray[5] ) // Initialize each of the five elements aMyArray[1] := "One" aMyArray[2] := "Two" aMyArray[3] := "Three" aMyArray[4] := "Four" aMyArray[5] := "Five"
You can simplify the process of initializing array elements after declaring an array by using the following syntax:
Declare ( aMyArray[5]; nCount := 0 ) ForEach (x; {"One"; "Two"; "Three"; "Four"; "Five"}) nCount := nCount +1 aMyArray[ nCount ] := x EndFor
Understanding macro concepts 21
If you want, you can initialize an array upon its declaration (in which case, the number of elements need not be specified). The following commands declare and initialize a one­dimensional array that contains five elements:
Declare(aMyArray[]:={"One";"Two";"Three";"Four";"Five"})
declares and initiatlizes a one-dimensional, five-element local array
Local(aMyArray[]:={"One";"Two";"Three";"Four";"Five"}) — declares
• and initiatlizes a one-dimensional, five-element local array
aMyArray[]:={"One";"Two";"Three";"Four";"Five"} — declares and
• initiatlizes a one-dimensional, five-element local array
Global(aGlobalArray[]:={"One";"Two";"Three";"Four";"Five"})
declares and initiatlizes a one-dimensional, five-element global array
Persist(aPersistArray[]:={"One";"Two";"Three";"Four";"Five"})
• declares and initiatlizes a one-dimensional, five-element persistent array
PerfectScript arrays can have up to ten dimensions. A two-dimensional array is like a table with rows and columns: Each cell in the table is an individual element.
For declaring a multi-dimensional array, the syntax of the subscript operator ( as follows: The number of dimensions is followed by a semicolon (
;), which is followed
[]) is
by the number elements within each dimension. The following example shows how to declare a three-dimensional array in which each dimension has five elements:
Declare ( aMyArray[3;5] )
Each dimension can have up to 32,767 elements (depending on available memory), and each element can be individually accessed and initialized. For accessing and initializing an element in a multi-dimensional array, the syntax of the subscript operator ( as follows: The dimension number of the element is followed by a semicolon (
[]) is
;), which
is followed by the subscript (or index) number of the element within that dimension.
The following syntax specifies the first element in the first dimension:
aMyArray[1;1] := "1-1"
The following syntax specifies the third element in the second dimension:
aMyArray[2;3] := "2-3"
The following stynax specifies the fifth element in the third dimension:
aMyArray[3;5] := "3-5"
Multi-dimensional arrays, like one-dimensional arrays, can be initialized at their time of declaration. In this scenario, the number of elements in each dimension does not need to be explicitly stated because it is implied by the actual initialization of those elements.
22 Understanding macro concepts
In addition, the dimensions are separated by a semicolon (;). The following example illustrates this syntax:
aMyArray[] := {{"1-1"; "1-2"; "1-3"; "1-4"; "1-5"}; // First row {"2-1"; "2-2"; "2-3"; "2-4"; "2-5"}; // Second row {"3-1"; "3-2"; "3-3"; "3-4"; "3-5"} } // Third row
PerfectScript provides a special form of initialization for multi-dimensional arrays. This form, called a slice (
...), lets you initialize the elements on a single dimension by
repeating the last-initialized element throughout that dimension. When initializing with a slice, you must fully initialize at least one row in each dimension to define the extent of the slice. The following example illustrates the syntax for using a slice:
// Declares a three-dimensional array (3x3x6) initializing all elements with a slice
aMyArray[] := { { {1;1;1;1;1;1}; // First dimension, first row {1; ... }; // First dimension, second row, replicated with value 1 {1; ... } }; // First dimension, third row {{2;2;2;2;2;2}; // Second dimension, first row {2; ... }; // Second dimension, second row {2; ... } }; // Second dimension, third row {{3;3;3;3;3;3}; // Third dimension, first row {3; 4, ... }; // Third dimension, second row, replicated with value 4 {3; 1; ... } } } // Third dimension, third r ow, r eplicate d wit h _
value 1
You can use the Dimensions command to return the following information about an array:
• total number of dimensions in the array
• total number of elements in the array
• number of elements in each dimension
• index range
In some cases, you must declare an array dynamically and therefore cannot be sure how many dimensions are contained in the array. The
Dimensions command allows a macro
to act dynamically by querying the size of an array:
Understanding macro concepts 23
aFiles[] := GetFileList() // returns an array of random size ForNext (x; 1; Dimensions( aFiles[]; 0 ) ) // Dimensions queries the array for the size FileOpen ( aFiles[x] ) FooterA(Create!) Type ( "McRae's Eat-a-Burger ") SubStructureExit() FileSave ( aFiles[x]; WordPerfect_60! ) Close() EndFor Function GetFileList() ... // statement block that creates an array (sized) dynamically Return ( ArrayOfFiles[] ) EndFunction
Please note that MacroArgs[] is a special PerfectScript array that contains values that are passed to the macro by the commands illustrates the
// Macro: MAIN.WCM // Include full path if macro not // in default macros directory Run("TSTMACRO"; {"x"; "y"; "z"})
MacroArgs[] array.
Chain, Nest, or Run. The following example
// Macro: TSTMACRO.WCM // Compile, then play MAIN.WCM vElements = Dimensions (MacroArgs[]; 0) If (vElements != 0) ForNext (x; 1; vElements; 1) MessageBox (z; "Element Values"; "MacroArgs[" + x + "] = " + _ MacroArgs[x]) EndFor Else Beep
24 Understanding macro concepts
MessageBox (z; "Error"; "No values passed"; IconExclamation!) EndIf // Result: x y z

Understanding constants

A constant — also called a constant variable — represents a value that cannot change during macro play.
By contrast, most variables represent a value that can change during macro play. For more information, see “Understanding variables” on page 10.
Constants must be initialized upon declaration, and their assigned value cannot change. You can declare and initiatlize a constant in the following way:
Constant ( WPCLASSNAME := "WordPerfect.8.32" )
A compile-time error occurs if a constant is misused in one of the following ways:
• if the constant is not initialized upon declaration — for example, if the declaration statement is missing an assignment operator (
• if an attempt is made to assign a different value to a constant after it has been declared and initialized
You can set constants apart from other variables by giving them a name that appears entirely in capital letters. This naming convention is the generally accepted practice in C/C++ and other programming languages.
:=) or an assigned value (or both)

Understanding operators

Operators are used to combine variables (see “Understanding variables” on page 10) and constants (see “Understanding constants” on page 25) into expressions — and even to combine expressions into other expressions. In PerfectScript, operators can be either “unary” or “binary.”
Unary operators are symbols or words that represent an operation on only one operand or expression. The following table lists the unary operators that are available in PerfectScript.
Understanding macro concepts 25
Operator Action Example and result
+
-
NOT
~
Multiplies an operand by +1
Multiplies an operand by -1
Inverts the result of relational and logical expressions
Toggles a binary value (that is, converts
1 to 0, and 0 to 1)
+5
Result: +5 (that is, 5 * +1)
-10
Result: -10 (that is, 10 * -1)
See “Understanding logical operators” on page 31.
See “Understanding bitwise operators” on page 35.
Binary operators are symbols or words that represent an operation on two operands or expressions. In the following example, the binary plus operator (
4, and the assignment operator (:=) assigns the result of the arithmetic expression
and
3 + 4 to variable x. x := 3 + 4
+) adds the operands 3
All PerfectScript operators can be classified into the following functional categories:
assignment operators — symbols that assign the value of a right-operand expression to a left-operand variable. For more information, see “Understanding assignment operators” on page 27.
arithmetic operators — symbols or words that represent a mathematical operation on two operands. For more information, see “Understanding arithmetic operators” on page 27.
relational operators — symbols that represent a relational operation on two operands, such that the operation result equals either true or false. For more information, see “Understanding relational operators” on page 28.
logical operators — words that represent a logical relationship between conditions, or that invert a condition. For more information, see “Understanding logical operators” on page 31.
bitwise operators — symbols that represent a bitwise operation on two integer operands. For more information, see “Understanding bitwise operators” on page 35.
When used together to form an expression, operators are evaulated by PerfectScript based on precedence level. For more information about operator precedence, see “Understanding operator precedence” on page 38.
26 Understanding macro concepts
Loading...
+ 158 hidden pages