Copyright (c) Hyperion Entertainment and contributors.

Difference between revisions of "AmigaOS Manual: ARexx Getting Started"

From AmigaOS Documentation Wiki
Jump to navigation Jump to search
(Fixed typos)
Line 8: Line 8:
 
= Starting ARexx =
 
= Starting ARexx =
   
To start using ARexx, you activate the RexxMast program. The RexxMast program is started automatically or manually. Each time ARexx is started or stopped, a text message appears.
+
To start using ARexx, you activate the RexxMast program. The RexxMast program can be started automatically or manually. Each time ARexx is started or stopped, a text message appears.
   
 
== To Start ARexx Automatically ==
 
== To Start ARexx Automatically ==
   
There are two methods to start ARexx automatically: placing the RexxMast icon in the WBStartup drawer or editing the S:User-Startup file.
+
There are two methods to start ARexx automatically: add RexxMast to the WBStartup Prefs window or edit the S:User-Startup file.
   
To place RexxMast in the WBStartup drawer:
+
To place RexxMast in the WBStartup Prefs list:
   
# Open the System drawer.
+
# Open the Prefs drawer.
# Drag the RexxMast icon over the WBStartup drawer.
+
# Open the WBStartup prefs.
  +
# Click the "Add" button.
  +
# Select SYS:System/RexxMast in the requester that pops up
  +
# Click on "Save"
 
# Reboot your Amiga.
 
# Reboot your Amiga.
   
 
To edit the S:User-Startup file:
 
To edit the S:User-Startup file:
   
# Open a text editor.
+
# Open a text editor like Notepad.
 
# Open the S:User-Startup file.
 
# Open the S:User-Startup file.
# Enter ''REXXMAST >NIL:''
+
# Enter ''SYS:System/RexxMast >NIL:''
 
# Save the file.
 
# Save the file.
 
# Reboot your Amiga.
 
# Reboot your Amiga.
Line 30: Line 33:
 
== To Start ARexx Manually ==
 
== To Start ARexx Manually ==
   
There are two ways to start RexxMast manually: double-click on the RexxMast icon in Workbench or start it from the Shell. Floppy-based system users can save disk space by starting ARexx only when necessary.
+
There are two ways to start RexxMast manually: double-click on the RexxMast icon in Workbench or start it from the Shell.
   
 
To start RexxMast from Workbench:
 
To start RexxMast from Workbench:
Line 40: Line 43:
   
 
# Open a Shell.
 
# Open a Shell.
# Type ''REXXMAST >NIL:'' and press Enter.
+
# Type ''RexxMast >NIL:'' and press Enter.
   
 
= About ARexx Programs =
 
= About ARexx Programs =
   
ARexx programs are usually stored in the REXX: directory (which is generally assigned to the SYS:S directory). Although programs can be stored in any directory, storing them in REXX: has several advantages:
+
ARexx programs are usually stored in the REXX: directory (which is generally assigned to the SYS:S/ARexx directory). Although programs can be stored in any directory, storing them in REXX: has several advantages:
   
 
* You can run the program without having to type the complete path.
 
* You can run the program without having to type the complete path.
Line 62: Line 65:
 
is the same as:
 
is the same as:
   
RX Program
+
Rx Program
   
 
A short program can be entered directly at the command line by enclosing the program line in double-quotes. For example, the following program will send five files named myfile.1 through myfile.5 to the printer.
 
A short program can be entered directly at the command line by enclosing the program line in double-quotes. For example, the following program will send five files named myfile.1 through myfile.5 to the printer.
   
RX "DO i=1 to 5;
+
Rx "DO i=1 to 5;
 
ADDRESS command `copy myfile.' | | i `prt:'; END"
 
ADDRESS command `copy myfile.' | | i `prt:'; END"
   
 
When an application is ARexx-compatible, you can run ARexx programs from within the application by choosing a menu item or by specifying command options. Refer to the application's documentation for more information.
 
When an application is ARexx-compatible, you can run ARexx programs from within the application by choosing a menu item or by specifying command options. Refer to the application's documentation for more information.
   
ARexx programs can be run from the Workbench by creating a tool or project icon for the program. You must specify the RX command as the Default Tool for the icon. In the icon's Information window, enter:
+
ARexx programs can be run from the Workbench by creating a tool or project icon for the program. You must specify the Rx command as the Default Tool for the icon. In the icon's Information window, enter:
   
Default Tool: SYS:Rexxc/RX
+
Default Tool: SYS:C/Rx
   
When the icon is opened, RX starts RexxMast (if it is not already running). It executes the file associated with the icon as an ARexx program.
+
When the icon is opened, Rx starts RexxMast (if it is not already running). It executes the file associated with the icon as an ARexx program.
   
 
ARexx accepts two Tool Types: Console, to specify a window, and CMD, to specify a command string. You enter these Tool Types in the project icon's Information window as:
 
ARexx accepts two Tool Types: Console, to specify a window, and CMD, to specify a command string. You enter these Tool Types in the project icon's Information window as:
Line 86: Line 89:
 
The following examples illustrate how to use ARexx to display text strings on your screen, to perform calculations, and to activate the error checking feature.
 
The following examples illustrate how to use ARexx to display text strings on your screen, to perform calculations, and to activate the error checking feature.
   
Programs can be entered into any text editor, such as ED or MEmacs, or a word processor. Save your program as an ASCII file if you use a word processor. ARexx supports the extended ASCII character set (Å, Æ, ß). These extended characters are recognized as ordinary printing characters and will be mapped from lowercase to uppercase.
+
Programs can be entered into any text editor, such as Notepad or a word processor. Save your program as an ASCII file if you use a word processor. ARexx supports the extended ASCII character set (Å, Æ, ß). These extended characters are recognized as ordinary printing characters and will be mapped from lowercase to uppercase.
   
 
The examples also illustrate the use of some basic ARexx syntax requirements such as:
 
The examples also illustrate the use of some basic ARexx syntax requirements such as:
Line 95: Line 98:
 
* Use of single and double quotes
 
* Use of single and double quotes
   
Each ARexx program consists of a comment line that describes the program and an instruction that displays text on the console. ARexx programs must always begin with a comment line. The initial (slash asterisk) /* balanced with an ending (asterisk slash) */ tells the RexxMast interpreter that it has found an ARexx program. Without the /* and the *\, RexxMast will not view the file as an ARexx program. Once it begins executing the program, ARexx ignores any additional comment lines within the file. However, comment lines are extremely useful when reading the program. They can help organize and make sense of a program.
+
Each ARexx program consists of a comment line that describes the program and an instruction that displays text on the console. ARexx programs must always begin with a comment line. The initial (slash asterisk) /* balanced with an ending (asterisk slash) */ tells the RexxMast interpreter that it has found an ARexx program. Without the /* and the */, RexxMast will not view the file as an ARexx program. Once it begins executing the program, ARexx ignores any additional comment lines within the file. However, comment lines are extremely useful when reading the program. They can help organize and make sense of a program.
   
 
== Amiga.rexx ==
 
== Amiga.rexx ==
Line 110: Line 113:
 
Enter the above program, and save it as REXX:Amiga.rexx . To run the program, open a Shell window and type:
 
Enter the above program, and save it as REXX:Amiga.rexx . To run the program, open a Shell window and type:
   
RX Amiga
+
Rx Amiga
   
 
Although the full path and program name is ''Rexx:Amiga.rexx'', you do not need to type the REXX: directory name or the .rexx extension if the program has been saved in the REXX: directory.
 
Although the full path and program name is ''Rexx:Amiga.rexx'', you do not need to type the REXX: directory name or the .rexx extension if the program has been saved in the REXX: directory.
Line 133: Line 136:
 
Save this program as ''REXX:Age.rexx'' and run it with the command:
 
Save this program as ''REXX:Age.rexx'' and run it with the command:
   
RX age
+
Rx age
   
 
This program begins with a comment line that describes what the program will do. All ARexx programs begin with a comment. The SAY instruction displays a request for input on the console.
 
This program begins with a comment line that describes what the program will do. All ARexx programs begin with a comment. The SAY instruction displays a request for input on the console.
Line 143: Line 146:
 
== Calc.rexx ==
 
== Calc.rexx ==
   
This program introduces the DO instruction, which repeats the execution of program statements. It also illustrates the exponentiation operator ( ** ). Enter this program and save it as ''REXX:Calc.rexx''. To run the program, use the " RX calc " command.
+
This program introduces the DO instruction, which repeats the execution of program statements. It also illustrates the exponentiation operator ( ** ). Enter this program and save it as ''REXX:Calc.rexx''. To run the program, use the " Rx calc " command.
   
 
=== Program 3. Calc.rexx ===
 
=== Program 3. Calc.rexx ===
Line 157: Line 160:
 
The DO instruction repeatedly executes the statements between the DO and END instructions. The variable " i " is the index variable for the loop and is incremented by 1 for each iteration (repetition). The number following the symbol TO is the limit for the DO instruction and could have been a variable or a full expression rather than just the constant 10.
 
The DO instruction repeatedly executes the statements between the DO and END instructions. The variable " i " is the index variable for the loop and is incremented by 1 for each iteration (repetition). The number following the symbol TO is the limit for the DO instruction and could have been a variable or a full expression rather than just the constant 10.
   
Generally, ARexx programs use single spacing between alphanumeric characters. In Program 3, however, spacing is closed up between the exponentiation characters ( ** ) and the variables ( i , and 2, i and 3).
+
Generally, ARexx programs use single spacing between alphanumeric characters. In Program 3, however, spacing is closed up between the exponentiation characters ( ** ) and the variables ( i and 2, i and 3).
   
 
The statements within the loop have been indented. This is not required by the language, but it makes the program more readable, because you can easily visualize where the loop starts and stops.
 
The statements within the loop have been indented. This is not required by the language, but it makes the program more readable, because you can easily visualize where the loop starts and stops.

Revision as of 10:24, 25 January 2014

This chapter shows you how to:

  • Start ARexx
  • Save Programs
  • Store Programs
  • Use sample programs

Starting ARexx

To start using ARexx, you activate the RexxMast program. The RexxMast program can be started automatically or manually. Each time ARexx is started or stopped, a text message appears.

To Start ARexx Automatically

There are two methods to start ARexx automatically: add RexxMast to the WBStartup Prefs window or edit the S:User-Startup file.

To place RexxMast in the WBStartup Prefs list:

  1. Open the Prefs drawer.
  2. Open the WBStartup prefs.
  3. Click the "Add" button.
  4. Select SYS:System/RexxMast in the requester that pops up
  5. Click on "Save"
  6. Reboot your Amiga.

To edit the S:User-Startup file:

  1. Open a text editor like Notepad.
  2. Open the S:User-Startup file.
  3. Enter SYS:System/RexxMast >NIL:
  4. Save the file.
  5. Reboot your Amiga.

To Start ARexx Manually

There are two ways to start RexxMast manually: double-click on the RexxMast icon in Workbench or start it from the Shell.

To start RexxMast from Workbench:

  1. Open the System Drawer.
  2. Double-click on the RexxMast icon.

To start RexxMast from the Shell:

  1. Open a Shell.
  2. Type RexxMast >NIL: and press Enter.

About ARexx Programs

ARexx programs are usually stored in the REXX: directory (which is generally assigned to the SYS:S/ARexx directory). Although programs can be stored in any directory, storing them in REXX: has several advantages:

  • You can run the program without having to type the complete path.
  • All of your ARexx programs will be in the same place.
  • Most applications search for ARexx programs in REXX:.

Just as you can store an ARexx program anywhere, you can also name it anything you choose. However, adopting a simple naming convention will make program management much easier. Programs run from the Shell should have a .rexx extension to distinguish them from files run from other applications.

Running ARexx Programs

The RX command is used to run an ARexx program. If a complete path is included with the program name, only that directory is searched for the program. If no path is included, the current directory and REXX: are checked.

As long as your programs is stored in the REXX: directory, you do not need to include the .rexx extension when specifying your program name. In other words, typing:

RX Program.rexx

is the same as:

Rx Program

A short program can be entered directly at the command line by enclosing the program line in double-quotes. For example, the following program will send five files named myfile.1 through myfile.5 to the printer.

Rx "DO i=1 to 5;
ADDRESS command `copy myfile.' | | i `prt:'; END"

When an application is ARexx-compatible, you can run ARexx programs from within the application by choosing a menu item or by specifying command options. Refer to the application's documentation for more information.

ARexx programs can be run from the Workbench by creating a tool or project icon for the program. You must specify the Rx command as the Default Tool for the icon. In the icon's Information window, enter:

Default Tool: SYS:C/Rx

When the icon is opened, Rx starts RexxMast (if it is not already running). It executes the file associated with the icon as an ARexx program.

ARexx accepts two Tool Types: Console, to specify a window, and CMD, to specify a command string. You enter these Tool Types in the project icon's Information window as:

Console=CON:0/0/640/200/Example/Close
CMD=rexxprogram

Program Examples

The following examples illustrate how to use ARexx to display text strings on your screen, to perform calculations, and to activate the error checking feature.

Programs can be entered into any text editor, such as Notepad or a word processor. Save your program as an ASCII file if you use a word processor. ARexx supports the extended ASCII character set (Å, Æ, ß). These extended characters are recognized as ordinary printing characters and will be mapped from lowercase to uppercase.

The examples also illustrate the use of some basic ARexx syntax requirements such as:

  • Comment lines
  • Spacing rules
  • Case-sensitivity
  • Use of single and double quotes

Each ARexx program consists of a comment line that describes the program and an instruction that displays text on the console. ARexx programs must always begin with a comment line. The initial (slash asterisk) /* balanced with an ending (asterisk slash) */ tells the RexxMast interpreter that it has found an ARexx program. Without the /* and the */, RexxMast will not view the file as an ARexx program. Once it begins executing the program, ARexx ignores any additional comment lines within the file. However, comment lines are extremely useful when reading the program. They can help organize and make sense of a program.

Amiga.rexx

This program shows how to use SAY in a set of instructions to display text strings on the screen. Instructions are language statements that denote a certain action to be performed. Each statement always begins with a symbol. In the following example, the symbol is SAY. (Symbols are always translated to uppercase letters when the program is run.) Following SAY is an example of a string. A string is a series of characters surrounded by single quotes (`) or double quotes (").

Program 1. Amiga.rexx

/*A simple program*/
SAY `Amiga. The Computer For the Creative Mind.'

Enter the above program, and save it as REXX:Amiga.rexx . To run the program, open a Shell window and type:

Rx Amiga

Although the full path and program name is Rexx:Amiga.rexx, you do not need to type the REXX: directory name or the .rexx extension if the program has been saved in the REXX: directory.

You should see the following text in your Shell window:

Amiga. The Computer for the Creative Mind.

Age.rexx

This program displays a prompt for input and then reads entered information.

Program 2. Age.rexx

/*Calculate age in days*/
SAY `Please enter your age:'
PULL age
SAY `You are about' age*365 `days old.'

Save this program as REXX:Age.rexx and run it with the command:

Rx age

This program begins with a comment line that describes what the program will do. All ARexx programs begin with a comment. The SAY instruction displays a request for input on the console.

The PULL instruction reads a line of input from the user, which in this case is the user's age. PULL takes the input, converts it to uppercase letters, and stores it in a variable. Variables are symbols which may be assigned a value. Choose descriptive variable names. This example uses the variable name "age" to hold the entered number.

The final line multiplies the variable "age" by 365 and issues the SAY instruction to display the result. The "age" variable did not have to be declared as a number because its value was checked when it was used in the expression. This is an example of typeless data. To see what would happen if age was not a number, try running the program again with a non-numeric entry for the age. The resulting error message shows the line number and type of error that occurred.

Calc.rexx

This program introduces the DO instruction, which repeats the execution of program statements. It also illustrates the exponentiation operator ( ** ). Enter this program and save it as REXX:Calc.rexx. To run the program, use the " Rx calc " command.

Program 3. Calc.rexx

/*Calculate some squares and cubes.*/
DO i = 1 to 10 /*Begin loop - 10 iterations*/
SAY i i**2 i**3 /*Perform calculations*/
END /*End of loop*/
SAY `All done.'

The DO instruction repeatedly executes the statements between the DO and END instructions. The variable " i " is the index variable for the loop and is incremented by 1 for each iteration (repetition). The number following the symbol TO is the limit for the DO instruction and could have been a variable or a full expression rather than just the constant 10.

Generally, ARexx programs use single spacing between alphanumeric characters. In Program 3, however, spacing is closed up between the exponentiation characters ( ** ) and the variables ( i and 2, i and 3).

The statements within the loop have been indented. This is not required by the language, but it makes the program more readable, because you can easily visualize where the loop starts and stops.

Even.rexx

The IF instruction allows statements to be conditionally executed. In this example, the numbers from 1 to 10 are classified as odd or even by dividing them by 2 and then checking the remainder. The // arithmetic operator calculates the remainder after a division operation.

Program 4. Even.rexx

/*Even or odd?*/
DO i = 1 to 10 /*Begin loop - 10 iterations*/
IF 1 // 2 = 0 THEN type = `even'
ELSE type = `odd'
SAY i `is' type
END /*End loop*/

The IF line states that if the remainder of the division of the variable " i " by 2 equals 0, then set the variable " type " to even. If the remainder is not 0, the program will skip over the THEN branch and execute the ELSE branch, setting variable " type " to odd.

Square.rexx

This example introduces the concept of a function, a group of statements executed by mentioning the function name in a suitable context. Functions allow you to build large complex programs from smaller modules. Functions also permit the same code for similar operations in a different program.

Functions are specified in an expression as a name followed by an open parenthesis. (There is no space between the name and the parenthesis.) One or more expressions, called arguments, may follow the parenthesis. The last argument must be followed by a closing parenthesis. These arguments pass information to the function for processing.

Program 5. Square.rexx

/*Defining and calling a function.*/
 
DO i = 1 to 5
SAY i square (i) /*Call the "square" function*/
END
EXIT
square: /*Function name*/
ARG x /*Get the argument*/
RETURN x**2 /*Square it and return*/

Starting with DO and ending with END , a loop is set up with an index variable "i", that will increment by 1. The loop will iterate (repeat) five times. The loop contains an expression that calls the function " square " when the expression is evaluated. The function's result is displayed using the SAY instruction.

The function " square ", defined by the ARG and RETURN instructions, calculates the squared values. ARG retrieves the value of the argument string " i " and RETURN passes the function's result back to the SAY instruction.

Once the function is called by the loop, the program looks for the function name " square ", retrieves the argument " i ", performs a calculation, and returns to the line within the DO/END loop. The EXIT instruction ends the program after the final loop.

Results.rexx

The TRACE instruction activates ARexx's error checking feature.

Program 6. Results.rexx

/*Demonstrate "results" tracing*/
TRACE results
sum = 0 ; sumsq = 0;
DO i = 1 to 5
sum = sum + 1
sumsq = sumsq + i**2
END
SAY `sum=' sum `sumsq=' sumsq

The console displays the executed source lines, each pass through the DO/END loop, and the expression's final results. Removing the TRACE instruction, would display only the final result: sum = 15 sumsq = 55 .

Grades.rexx

This program calculates the final grade for a given student based on four essay grades and a class participation grade. The average of Essay 1 and Essay 2 is worth 30%, the average of Essay 3 and Essay 4 is worth 45%, and participation is worth 25% of the final grade.

Once a final grade is displayed, an option to continue with another calculation is presented. The response is " PULLed " and if it does not equal Q (quit), the loop continues. If the response equals Q , the program quits the loop and exits.

Program 7. Grades.rexx

/*Grading program*/
SAY "Hallo, I will calculate your grades for you."
Response = 0
DO while response ~ = "Q "/*Loop while response isn't Q*/
SAY "Please enter all grades for the student."
SAY "Essay 1:"
PULL es1
SAY "Essay 2:"
PULL es2
SAY "Essay 3:"
PULL es3
SAY "Essay 4:"
PULL es4
SAY "Participation:"
PULL p
Final = (((es1 + es2)/2*.3) + ((es3 + es4)/2*.45) + (p*.25))
SAY "Your final grade for this student is " Final
SAY "Would you like to continue? (Q for quit.)"
PULL response
END
EXIT