Writing Scripts

One of my favorite things about using AutoCAD is the ability to write and run scripts to help me do repetitive tasks quickly. A script is a series of commands (just like you type into the command line) that you can save in a text file and run on any drawing whenever you need it. Since it is saved in a text file, you can easily share it with colleagues who might need to use it. I have a library of scripts for repetitive tasks we do at work on a regular basis. Here is how to get started writing a script.

Let’s get started with something simple. For example, you might want to print a bunch of drawings with specific settings. You could hit print in each drawing, change the settings, and then let it print – or you could simply run a script on each drawing to do the same thing.

HINT: When I get an idea for a script, I usually do a dry-run by typing each command into the command line to see how it works. As I do, I write the same keystrokes in Notepad to generate my script.

To create this script, open Microsoft Notepad (or another text editor). Type commands into Notepad just like you would on the command line, hitting enter after each command. Here is an example of how a printing script might look:

-plot
Y
\\networkprintlocation\MY_PRINTER_NAME
Oversize: Arch D
I
L
N
e
1:1
C
Y
Standard.ctb
Y
Y
N
N
N
Y
Y
 

Notice there is an extra return after the last “Y”. Each line return is like hitting enter on the command line. Whenever you would hit enter on the command line, hit enter in Notepad.

When you are finished save the file with a .scr filetype. For example you might call this one “print.scr” and save it somewhere on your computer or network.

To run the script in AutoCAD, type SCRIPT (or use the alias SCR) on the command line and browse to open your script. It will run immediately. If it does not run as expected, hit F2 to check where it got hung up. Depending on how complex a script is, sometimes you will need to do this a few times to debug a script.

Did you know that you can plug in AutoLISP code into your scripts? Let’s say you want to create a selection set of objects on a certain layer and change them to another layer. You could write a script like this:

(ssget "x" (list (cons 8 "WrongLayerName")))
chprop
previous

la
RightLayerName
 

Using scripts for productivity get’s even better with ScriptPro – a program Autodesk created to allow you to run scripts on multiple drawings at once.

Now that’s lazy!

22 Comments so far

  1. CadKicks.com on May 29th, 2008

    Writing Scripts…

    You’ve been kicked (a good thing) – Trackback from CadKicks.com…

  2. Binu Mathew on August 12th, 2008

    Very cute command and would like top know more about script, how can i learn. thanks for your help to CAD community

  3. Josh Jones on August 12th, 2008

    Binu Mathew,

    If you have any specific questions about writing scripts, feel free to ask them here in your comment. I’d be happy to help.

    Thanks for stopping by!

  4. [...] those of you who are new to writing scripts, I have included a simple script as an example to show you how it [...]

  5. Steve Wright on September 14th, 2008

    Is there a way to plot a layered drawing to a single pdf file. I have created a script that works great when printing directly to a printer (each layer opens, prints, closes and next one opens etc. until it gets to the last layer), but when using Adobe Acrobat the first layer prints, it doesnt close and the next layer opens as well then everything just stops. Any help or a point in the right directionwould greatly be appreciated.

  6. Josh Jones on September 15th, 2008

    For plotting a bunch of drawings to a single PDF file, you will want to use either Sheet Set Manager (simply import your sheets into a new Sheet Set) or the PUBLISH command. This is the fastest way to create a single PDF file of multiple drawing sheets.

  7. Charlie on September 25th, 2008

    Steve can you share how to plot a layerd drawing?

    I have a Distiller script that merges all plot-files i a folder to a PDF. Maby something like that will work if you tick the "plot to file" option?
    I also have batch file for a program caled PDFtk, it will merge singel pdfs into a multipage pdf.

    @ECHO OFF
    C:\PDF\pdftk.exe C:\PDF\blad\*.pdf output C:\PDF\%1-6.pdf
    DEL \Q C:\PDF\blad\*.pdf

  8. Josh Jones on September 25th, 2008

    Thanks for the batch file tip Charlie!

    You can also try printing to Cute PDF with the PUBLISH command in AutoCAD. A friend of mine uses Cute PDF all the time.

    http://www.cutepdf.com

  9. Suresh on June 4th, 2009

    Josh,

    All I need to do is change the Line Space Factor and Height of a mtext. I select the text, rightclick, properties and inthe Properties dialog box, I change the height & Line space factor. I tried with scripts, but unsuccessful so far. Can u please help me with a script or lisp program

  10. nilesh on August 19th, 2009

    Hi friends…..
    i have written one script for Plan & Profile dwg
    from this .scr i uesd to create 5 viewports & 3 text lines
    but after 60% script run autocad giving me “Fatal error”
    i think the script i have written is too much heavy …….
    alos my dwg is around 10mb
    so is there any way to simplfy script …use some variable or something like that …

  11. Millian on January 2nd, 2011

    here is my problem,
    i have about 500 circle. i need each of the circle coordinate to be extracted.
    can anyone advice me on how to go bout this?
    Thanks
    Millian

  12. Kenneth Mayfield on December 16th, 2011

    I have 1000 circles with a node located near each in a drawing. Could you give me the steps to create a script to insert a Northing dimension and a Easting dimension from the center of each circle to the nearest node of that circle. The circle identifies the specified location of an anchorbolt and the node identifies the location of the anchorbolt in the real world after placed in the foundation.

  13. Andrew on July 17th, 2012

    I know this is years late, but for the circles and nodes you would run EATTEXT and extract all of their coordinates. Then use a script to insert a block with Northing and Easting attributes at each set of coordinates. There is a great post about this somewhere else on here. I would draw a line from the attributes to the insertion point to make it easier to read.

  14. Josh Jones on July 17th, 2012

    You get it Andrew! Well said!

  15. Andrew on July 25th, 2012

    First, thanks Josh. I have gotten a lot of great stuff from your site, and I would not be where I am today, CADwise, without it. :)

    Second, I have to amend my earlier comment. You would run EATTEXT to extract the coordinates of the circles and nodes UNLESS you were using an LT version of AutoCAD, since EATTEXT is not defined in LT. You will need to place a block at each node or circle center point, then select all of the blocks (easier if you create a layer for them) and run ATTEXT. You will also need to create a template file for ATTEXT, directions for which can be found here: http://www.brianmorse.com/ASCII Point file from blocks.php

  16. maya on October 31st, 2012

    Hi,

    Is it possible to create a script.scr to find and replace text in drawings.

    thanks

  17. Andrew on November 1st, 2012

    I don’t think a script would be able to find text in drawings. You would need a LISP or VB.NET program. A LISP would be the easiest way to go by far, and Afralisp.net has some really good tutorials on writing LISPs if you want to try that route.

  18. HC on April 1st, 2013

    I wrote a script to insert text at specific coordinates in feet-inches format. My coordinates precision is 1/16, but AutoCAD keeps using a precision of 1/32. Even when I specify “9/16″ as “18/32″ AutoCAD insists on the value of “19/32″. What gives?

  19. HC on April 2nd, 2013

    Nevermind. I found the answer in a comment on the post for about writing scripts with Excel. The answer: set the OSNAPCOORD system variable. Thank you.

  20. RIAF on April 23rd, 2013

    Hey great site!! I am a newby with AutoCad and I am trying to make my life easier with it. Do you know how can you give the options to PUBLISH from within a Script. I know you can call publish, but then you get the options window. I just want to give that away in my script.

    Thanks

    RIAF

  21. Andrew on April 23rd, 2013

    Using “-PLOT” will allow you to plot from the command line. You will need to go through the process while you write your script to figure out all of the inputs it will ask for.

    If you run into dialog boxes while running a script, there are two ways around them.

    1. Set the FILEDIA system variable to 0. This turns off dialog boxes for some commands.

    2. Use a dash in front of the command (-PLOT and -LAYER for example). This will run many commands from the command line instead of their dialog boxes.

    If you try both of these methods and still get a dialog box for the command you are trying to script, it is more than likely that you will not be able to script it. Some commands, like QSELECT, do not have a command line only option and can only be run through a dialog box.

  22. Andrew on April 23rd, 2013

    Sorry, -PUBLISH is the command you will want to use, not -PLOT. You will need a sheet list in order to execute the command.

Leave a reply