Your first Illustrator script
Your first Illustrator script
Section titled “Your first Illustrator script”The traditional first project in any programming language is displaying the message “Hello World!” In this example, you create a new Illustrator document, then add a text frame containing this message. Follow these steps:
- Start any text editor (for example, Notepad).
- Type the following code:
Rem Hello WorldSet appRef = CreateObject("Illustrator.Application")Rem Create a new document and assign it to a variableSet documentRef = appRef.Documents.AddRem Create a new text frame item and assign it to a variableSet sampleText = documentRef.TextFrames.AddRem Set the contents and position of the TextFramesampleText.Position = Array(200, 200)sampleText.Contents = "Hello World!"
- Save the file as text-only in a folder of your choice, using the file extension
.vbs
. - To test the script, do one of the following:
- Double-click the file.
- Start Illustrator, choose File > Scripts > Other Scripts, and navigate to and run your script file.
Adding features to “Hello World”
Section titled “Adding features to “Hello World””Next, we create a new script that makes changes to the Illustrator document you created with your first script. Our second script demonstrates how to:
- Get the active document.
- Get the width of the active document.
- Resize the text frame to match the document’s width.
If you already closed the Illustrator document, run your first script again to create a new document.
Follow these steps:
- Copy the following script into your text editor, and save the file:
Set appRef = CreateObject("Illustrator.Application")'Get the active documentSet documentRef = appRef.ActiveDocumentSet sampleText = documentRef.TextFrames(1)' Resize the TextFrame item to match the document widthsampleText.Width = documentRef.WidthsampleText.Left = 0
- Run the script.