19. How to Write a Script File

by Ralph Grabowski

The last two lessons, we looked at the Diesel macro language, which has a programming logic as unclear as the acronym's meaning: "direct interactively evaluated string expression language." This lesson, we go the other extreme and look at AutoCAD's clearest customization possibility: the script.

What is a Script?

Script programming was introduced to AutoCAD with version 1.4, w-w-a-a-y-y back in 1983. A script does one thing and one thing only: it mimics what I type at the keyboard. Anything I type in AutoCAD that shows up at the 'Command:' prompt can be put into a script file. That includes AutoCAD commands, their option abbreviations, my responses, and -- significantly -- AutoLISP code.

The purpose of the script is to reduce keystrokes by placing the keystrokes in a file. Think of it as a predecessor to the toolbar macro. A script file to draw a line and a circle looks like this:

Script files have an extension of SCR. Script files are in plain ASCII. For that reason, I don't use a word processor, such as WordPad, Write, or Word. Instead, I use NotePad to write scripts. Sometimes, when I feel like a DOS Power User, I even write scripts at the DOS prompt:

When I'm done, I press [F6] or [Ctrl]+Z to finish editing and close the file.

The Drawbacks to Scripts

A limitation to scripts is that only one script file can be loaded into AutoCAD at a time. However, a script file can call another script file. Or, I can use some other customization facility to load script files with a single mouse click, such as toolboxes, menu macros, and AutoLISP routines.

Another limitation is that scripts stall when they encounter invalid command syntax. I sometimes have to go through the code-debug cycle a few times to get the script correct. It is useful to have an AutoCAD reference text on hand, such as my own "The Illustrated AutoCAD Quick Reference," which lists all command names and their options. Some third-party developers have attempted to write "script creation" software.

There are two limitations to scripts that are significant in this age of GUIs (graphical user interfaces): I cannot record mouse movements; and I cannot have a script controlling a dialog box.

Strictly Command-Line Oriented

For this reason, just about all commands that display a dialog box have a command line version available in AutoCAD. However, AutoCAD is inconsistent in how I employ these commands. There are, in fact, five different ways to going about this:

1. Some commands have different names, such as (dialog box version in brackets): -Layer (Layer or DdLModes in AutoCADs older than 2000), ChProp (DdChProp), and FileOpen (Open). Generally, in older versions of AutoCAD, all commands that start with 'Dd' have a non-dialog box version available with a different name; in newer versions of AutoCAD, the hyphen ( - ) prefix indicates the command-line version of the command.

Sometimes it's a command and other times it's a system variable. For example, the DdRModes dialog box controls about a dozen system variables. If my script needs to change a layer, I would use the Layer command -- or better yet -- the CLayer system variable, as follows:

2. Some commands need system variable FileDia turned off (set to 0). This forces any command that displays the Open File or Save File dialog boxes -- such as DxfIn, Script, and VSlide -- to prompt me for the filename at the command line. Thus, my script file would include the following line to turn off dialog boxes:

When FileDia is turned off, I use the ~ (tilde) as a filename prefix to force the display of the dialog box. For example:

3. Prior to AutoCAD 2000, the Plot command has its own system variable to turn off its dialog box: when CmdDia = 0, I see prompts at the command line, like this:

Thus, I would type:

As of AutoCAD 2000, the CmdDia command not longer works. Instead, use the -Plot command, as follows:

Command: -plot
Detailed plot configuration? [Yes/No] <No>:

An alternative to controlling the plot process is to pre configure a bunch of PCP files. These hold "Plot Configuration Parameters" and can be accessed via a pair of system variables:

4. Some commands use the - (hyphen) as a prefix to force the command line version. To use these in a script file, I prefix the hyphen, as follows:

Instead of the Multiline Edit Tools dialog box, AutoCAD displays the following prompt:

I leave it as an exercise to the reader to figure out the meaning of those 12 options!

5. Some commands have no command-line version. The list primarily consists of commands related to OLE and rendering: OleLinks, Render, Lights, Scene, etc. There is no way to control these from a script file.

Script Commands and Modifiers

There are a grand total of four commands that relate specifically to scripts. In fact, these commands are of absolutely no use for any other purpose. In rough order of importance, these are:

Script.
The Script command performs double-duty: (1) it loads a script file; and (2) immediately begins running it. Use it like this:

Remember to turn off (set to 0) the FileDia system variable, so that the prompts appear at the command line, instead of the dialog box.

RScript.
Short for "repeat script," this command re-runs whatever script is currently loaded in AutoCAD. A great way to creating an infinite loop. There are no options:

Story Time: When AutoCAD first introduced AutoLISP in version 2.17, it was a rudimentary form of LISP that Autodesk called "Expressions and Variables." It was so weak, it lacked conditional expressions, such as if-then, while, and repeat. In fact, Autodesk CEO John Walker said there was no way to create a loop in Expressions and Variables. Third-party programmers proved him wrong by using the RScript command to repeat AutoLISP functions.

Resume.
This command resumes a paused script file. I pause a script file by pressing the Backspace key. Again, no options:

Delay.
To create a pause in a script file without human intervention, I use the Delay command along with a number. The number specifies the pause in milliseconds, where 1,000 milliseconds equal one second. The minimum delay is 1 millisecond; the maximum is 32767 milliseconds, which is just under 33 seconds. While I could use Delay at the 'Command:' prompt, that makes little sense; instead, Delay is used in a script file to wait while a slide file is displayed or to slow down the script file enough for humans to watch the process, like this:

In addition to these four script-specific commands, there are some special characters and keys. The most important special characters are invisible: both the space and the carriage return (or end-of-line) represent me pressing the Spacebar and Enter key. In fact, both are interchangeable. But the tricky part is that they are invisible. Sometime, I'll write a script that requires a bunch of blank space because the command requires that I press the Enter key several times in a row. AttEdit is an excellent example:

; Edit the attributes one at a time:  
attedit    1,2

How many spaces are there between 'attedit' and the coordinates '1,2'? I'll wait while you count them ...

For this reason, it is better to place one script item per line, like this:

Now it's easier to count those four spaces, since there is one per blank line.

; (semi-colon).
You probably already noticed that the semi-colon lets me insert comments in a script file. AutoCAD ignores anything following the semi-colon.

' (apostrophe).
Scripts can be run transparently during a command. I simply prefix the Script command to run a script while another command is active, like this:

The double angle bracket >> is AutoCAD's way of reminding me that I've got two commands on the go. In fact, all four of AutoCAD's script-specific commands are transparent, even 'Delay. That lets me create a delay during the operation of a command -- as if I needed an excuse to run AutoCAD slower!

* (asterisk).
There is one special case where the asterisk gets used. When I prefix the VSlide command with the *, AutoCAD "pre-loads" it to provide faster slide viewing performance:

Backspace
is the key I already mentioned for pausing a script file.

Esc
stops a script file dead in its tracks; use the RScript command to start it up again from the beginning (In Release 12, LT R2, and earlier, press Ctrl+C, instead, to stop the script).


Tailoring AutoCAD Part 18 | Part 19 | Part 20 is the next tutorial.

Comments on this tutorial series? Tell me about it.

Return to Contents.

All contents copyright 1995-9 XYZ Publishing, Ltd. . All rights are reserved. No material may be reproduced electronically or in print without written permission from XYZ Publishing, PO Box 3053, Sumas WA, 98295-3053, unless otherwise noted.