A script is a macro, a list of commands that you can run all at once, and as many times as necessary, allowing you to automate tasks that would take a long time if you did them manually. Scripts can be very powerful and you can run them on objects in one drawing, or on many drawings. Scripts have been around for many years and many people have a library of many scripts that they use.
See my tip, Record Actions with the Action Recorder, for a way to record your actions in AutoCAD 2009 and later.
Here are 3 important points that you need to know about scripts:
- Scripts are text-only (ASCII) files. You usually create them in Notepad.
- They have an SCR filename extension, so be sure to save them that way.
- Scripts use command-line syntax only. They can’t access dialog boxes, toolbar buttons, etc.
Follow these steps to create a script file:
- Set the FILEDIA system variable to 0, to stop dialog boxes that access files from opening.
- Run through the steps that you want to automate, using the command line only. Write down (or type in Notepad) the steps. You can copy your command line entry directly to Notepad. Press F2 to open the AutoCAD Text Window for that purpose.
- Press Enter at the end of each command or use a blank space, which is the equivalent of pressing Enter. The script reads every space, so you need to get it exactly right! The script is easier to read if you put each command on its own line.
- Enclose layer names or files names (and file paths) that contain spaces in quotation marks.
- Insert comments periodically for explanation. To insert a comment, precede the text with a semicolon.
- Save the file with an SCR filename extension, by typing .scr after the file name.
- Set FILEDIA back to 1.
To run and test the script file from within a drawing, use the SCRIPT command. A dialog box opens, where you can choose your script file. Click Open and the script runs.
Let’s say that you want to run a script file on more than one drawing. You can use the OPEN, CLOSE, and QSAVE commands to open drawings, run some commands, save the drawings, and then close them. You can still start the script from within the 1st drawing, but you can also start a script file as you open AutoCAD.
To do so, you change the expression that Windows uses to open AutoCAD. The best way to do this is to use the shortcut on your Desktop. Follow these steps:
- Right-click the shortcut and choose Properties.
- Click the Shortcut tab.
- At the end of the existing expression (which reads something like C:\Program Files\AutoCAD 2009\acad.exe) add a space and then the following:
- Click OK.
- Double-click the shortcut to open AutoCAD and run the script.
Autodesk provides a free program for running script files on multiple drawings, called ScriptPro. The file is called scriptpro.exe.