Macros and Scripts

This is the second of three blogs explaining how to manage system variables 

  1. This blog demonstrates a level of automation to change the variables and store them in toolbars.  

  1. The final blog demonstrates a simple LISP program to change the variables and how to load these simple programs into AutoCAD. 

Scripts 

When saving the variable settings to an external file from the Systems Variable dialog box, you can save either a filtered list (by using the search with wildcards) or a selected list by selecting multiple variables (using the <CTRL> or <Shift> key to select more than one). If only one variable is selected, then all will be saved, as shown. 

 

 

You have the option of file formats, as shown: 

  • SVF is for reading (restoring) variables in the Systems Variable dialog box. 

  • SCR is for restoring the variables through the AutoCAD Script command. 

 

 

 

Script Files 

To use the script file, type Script in the Command Line, then browse to the script file (with a *.scr extension). Select the file to open it. AutoCAD reads each line as if it were typed in at the command line. It skips any line that starts with a semi-colon (;) – this usually precedes variables that are read-only and cannot be changed. 

You can also develop macros using such script formats. Similar to scripts, a macro is a series of keystrokes executed when the macro is launched. Scripts run the commands from the contents of a text file whereas macros run the commands from a text string within the macro, which is stored in the program. 

Macros can be defined as commands through the CUI interface where the following can be done, as shown: 

  1. Create a new command. 

  1. Search for commands (which then can be edited). 

  1. Name the new command. 

  1. Create the macro: 

a. Type in the commands exactly as you would in the command line. 

b. “^C^C” means <CTRL> C, <CTRL> C, which cancels any ongoing command. 

c. A space acts the same as an <Enter> key. 

  1. Assign an icon, or button image, to the command. 

  1. Drag the new command into the Quick Access bar. 

Note: you can also drag the command directly to the Quick Access Bar while it is listed in the CUI interface, as shown. 

 

With the new command created, you can now type it in the command line to be executed. For example, you can create a new command “FILEDIA-ON”. The macro would be  

“^C^CFiledia 1  

This would cancel previous commands, invoke the FILEDIA system variable and set it to 1. Note the space at the very end, which acts as pressing the spacebar, which in AutoCAD is the same as the <Enter> key. 

The macros / commands can be added to other toolbars. You can create a new toolbar directly in the CUI interface through the right-click menu, as shown. Then you can drag any combination of commands into the toolbar. For example, you may have a toolbar named “System Variables”, and in it a series of macros, some for turning on variables and others for turning off variables. 

 

Conclusion 

Creating macros is a great way to streamline your AutoCAD workflows. With a bit of imagination, you can string together a series of commands and set variables. They can be added to the Quick Access Toolbar, or you can create other toolbars to house these macros 

 

About the Author

Jeff Morris

Learning Content Developer<br><br>Jeff specializes in infrastructure tools such as Civil 3D and Infraworks, delivering training classes and contributing to the learning guides for these Autodesk software applications. Jeff has worked for several Autodesk resellers and has had roles of both CAD and BIM Manager with Civil and Architectural firms.

Follow on Linkedin More Content by Jeff Morris
Previous Article
Using Fields in AutoCAD
Using Fields in AutoCAD

Learn how to use fields to automate the area of a region.

Next Article
System Variables in AutoCAD
System Variables in AutoCAD

Learn how system variables can optimize your AutoCAD experience with this first blog in a series of 3 posts.

Get Autodesk Courseware Now

Learn More