Classic Computer Magazine Archive COMPUTE! ISSUE 42 / NOVEMBER 1983 / PAGE 280

Make Your Apple User-Friendly

Karen Goeller McCullough

With an Apple II and a disk drive, you can use this versatile utility program to create menus that call other programs – or you can merge it with your own multifunction programs to create an effective master menu.

If you have an Apple II with Applesoft BASIC and at least one disk drive, this handy utility can save time and prevent confusion by generating menu programs. All you do is tell the program the number of options on your menu and their names. From that information a BASIC program is generated which presents a nicely formatted display of the options, allows the entry of a selection, and checks it for validity.

The figure shows a sample menu created using "Menu Generator." You simply add the code to tell the program where to go after the option has been selected. The Menu program can be used on its own to call other programs, or it can be merged into your own programs using the renumber and merge options in the "Renumber" program on the System Master diskette.

<1> INITIALIZE DATA DISKETTE
<2> SET UP NEW FILE
<3> ADD ENTRIES TO FILE
<4> CHANGE ENTRIES ON FILE
<5> DELETE ENTRIES ON FILE
<6> PRINT MAILING LIST
<7> PRINT MAILING LABELS
<8> EXIT FROM SYSTEM

Menu Generator uses an Apple DOS feature to create a program as a text file and then EXEC it. The EXEC command treats a text file as a series of commands that are executed just as though they had been entered from the keyboard. Delayed execution commands (those that have line numbers in front of them) are saved in memory to await a RUN command. (For more information on EXEC files, see pages 75–79 of the DOS Manual.) The EXEC command lets you write a BASIC program that will produce another BASIC program which can be immediately EXECed into memory and RUN, or SAVEd to disk as a program file. Menu Generator is an example of how this feature can be used for almost unlimited program generation.

Program Breakdown

In the Menu Generator program, lines 10-50 initialize the screen and variables. Line 100 sends us to line 1000 to begin processing. Lines 200 and 250 are subroutines that clear either a part of the screen (200) or a given line V (250). These are placed close to the beginning of the program to speed execution.

Lines 1000-1060 input the number of options desired on the menu. A string variable is used to input the number, and lines 1030-1040 scan the input string for valid numeric characters (ASCII 48-57). If an invalid character is detected, a flag (E) is set. The flag is then tested in line 1050, and, if true, execution is returned to the input statement at 1020.

This may seem a cumbersome process, since using a numeric variable would obviate the need for lines 1030-1050. However, in applications where an attractive screen format is important, this routine avoids the ?REENTER statement which appears if you try to enter a nonnumeric character into a numeric variable.

The options to appear on the menu are entered in lines 1070-1200. The array OP$ holds the option names, and the array element number also functions as the option number. For example, if option number 1 on the menu is to be INITIALIZE DATA DISKS, then that will be the contents of OP$(1). After all the options are entered and checked for length of less than 30 characters, the program checks to see if changes are desired (1210–1420).

Beginning at line 2000, the text file which builds the program is created. The text file is opened in lines 2010-2040, and the first line is printed at 2045. Since the EXEC command itself does not clear the program currently in memory, the first line of the exec file issues an FP command, which prevents the EXECed program from overlaying the calling program. The POKE 34,24 in lines 2047 and 1420 prevents the screen from scrolling and the cursor from bouncing around while the EXEC file is being processed.

Menu Generator Variable List
A$ -yes or no answerninput
CHS -holds a single character for error checking
D$ -return + control-D (CHR$ (13) + CHR$ (4))
E -error flag
H -horizontal print location
I -counter tor FOR/NEXT loops
L -length of longest option
L1 -temporarily holds length of each option
N -number of options on menu
N$ -number of options (input string)
NN -option number to change
NN$ -option number to change (input string)
Nl -option number selected on menu
OP$ -array holding option names
Q$ -quote mark (CHR$ (34))
V -vertical print location

Creating The New Program

The beginning of the new program being created (the menu program itself) is at line 2050. Lines 2050–2220 actually write the menu program, beginning with the header which will be lines 10–30 in the new program (lines 2050–2070 in the creating program). The variable N is set equal to the number of options, and the array OP$ is DIMed to N in line 40 (2080). The array OP$ is loaded with the option names in line 50 (2090), and line 2110 of the creating program causes the OP$ array to be printed as the DATA statement of line 70 of the new program. The length of the longest option line is found in line 2120; this information is used to calculate the horizontal positioning in line 2140. The same line also calculates the vertical positioning using the number of options (N).

After displaying the menu options, the program asks for the selection to be input. Input and validation procedures (2180–2220) are the same as those used for the option number input in the creating program. Line 2220 is the end of the delayed execution part of the text file, and it remains in memory while the EXEC function continues to the last two lines of the text file. Line 3010 causes the program which has been LOADed into memory from the text file to be SAVEd to disk as a program file called MENU-PROGRAM, and the next line causes it to be RUN.

Printing of the text file is concluded by line 3040, which CLOSEs the text file. The last line of the program issues the DOS EXEC command, which executes the text file. You now have the menu program SAVEd on disk and displayed on the screen, ready to make any modifications you might wish.