Writing Scripts

kick it on CadKicks.com

One of my favorite things about using AutoCAD is the ability to write and run 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 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 code into your ? 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 for productivity get’s even better with ScriptPro – a program Autodesk created to allow you to run on multiple drawings at once.

Now that’s lazy!

Share and Enjoy:
  • Twitter
  • Facebook
  • LinkedIn
  • del.icio.us
  • Google Bookmarks
  • Live

9 Comments so far

  1. Sign up at Gravatar.com to add your picture. CadKicks.com on May 29th, 2008

    Writing Scripts…

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

  2. Sign up at Gravatar.com to add your picture. 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. Sign up at Gravatar.com to add your picture. 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. Sign up at Gravatar.com to add your picture. 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. Sign up at Gravatar.com to add your picture. 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. Sign up at Gravatar.com to add your picture. 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. Sign up at Gravatar.com to add your picture. 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. Sign up at Gravatar.com to add your picture. 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

Leave a reply