Tutorial: Automate tasks with a script file

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. Using Scripts in Autocad can be very powerful and you can run them on objects in one drawing, or on many drawings. AutoCad Script function 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 script in AutoCad:

  1. Scripts are text-only (ASCII) files. You usually create them in Notepad.
  2. They have an SCR filename extension, so be sure to save them that way.
  3. Scripts use command-line syntax only. They can’t access dialog boxes, toolbar buttons, etc.

Scripts just execute commands. If you can’t do it by typing on the keyboard, you can’t do it in a script. The value of scripts is that you can use them over and over and they can do long lists of commands, even on many drawings.

If you require more automation beyond the functionality of Script in AutoCad , you may need AutoCad AutoLiSP function or other programming language. Check out our blog post comprehensive AutoLisp tutorial on How to create  a custom command for AutoCad using AutoLISP.

Follow these steps to create a script file:

  1. Set the FILEDIA system variable to 0, to stop dialog boxes that access files from opening.
  2. 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.
  3. 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.
  4. Enclose layer names or files names (and file paths) that contain spaces in quotation marks.
  5. Insert comments periodically for explanation. To insert a comment, precede the text with a semicolon.
  6. Save the file with an SCR filename extension, by typing .scr after the file name.
  7. 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:

    1. Right-click the shortcut and choose Properties.
    2. Click the Shortcut tab.
    3. 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: /b script_name
    4. Click OK.
    5. 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.

For those interested to explore Automation in AutoCad further, you will find more comprehensive AutoCad Lisp commands, AutoLISP tutorials & examples in my book, Top Customization Tips Every AutoCad Users Should Know.

The post Tutorial: Automate tasks with a script file appeared first on AutoCAD Tips Blog.