Adobe 65010248 Scripting Guide - Page 49

Adding features to “Hello World”, Accessing and referencing objects

Page 49 highlights

CHAPTER 6: Scripting with VBScript Accessing and referencing objects 49 Adding features to "Hello World" Next, we create a new script that makes changes to the Illustrator document you created with your first script. The second script demonstrates how to: ➤ Get the active document. ➤ Get the width of the active document. ➤ Resize the text frame item to match the document's width. If you closed the Illustrator document without saving it, run your first script again to create a new document. Follow these steps: 1. Copy the following script into your text editor, and save the file. Set appRef = CreateObject("Illustrator.Application") 'Get the active document Set documentRef = appRef.ActiveDocument Set sampleText = documentRef.TextFrames(1) ' Resize the TextFrame item to match the document width sampleText.Width = documentRef.Width sampleText.Left = 0 2. Run the script. Accessing and referencing objects When you write a script, you must first decide which file, or Document, the script should act on. Through the Application object, the script can create a new document, open an existing document, or act on a document that is already open. The script can create new objects in the document, operate on objects that the user selected, or operate on objects in one of the object collections. The following sections illustrate techniques for accessing, referencing, and manipulating Illustrator objects. Obtaining objects from collections Generally, to obtain a reference to a specific object, you can navigate the containment hierarchy. For example, to use the myPath variable to store a reference to the first PathItem in the second layer of the active document: Set myPath = appRef.ActiveDocument.Layers(2).PathItems(1) The following scripts demonstrate how to reference an object as part of a document: Set documentRef = appRef.ActiveDocument Set pageItemRef = documentRef.PageItems(1)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

C
HAPTER
6: Scripting with VBScript
Accessing and referencing objects
49
Adding features to “Hello World”
Next, we create a new script that makes changes to the Illustrator document you created with your first
script. The second script demonstrates how to:
Get the active document.
Get the width of the active document.
Resize the text frame item to match the document’s width.
If you closed the Illustrator document without saving it, run your first script again to create a new
document.
Follow these steps:
1.
Copy the following script into your text editor, and save the file.
Set appRef = CreateObject("Illustrator.Application")
'Get the active document
Set documentRef = appRef.ActiveDocument
Set sampleText = documentRef.TextFrames(1)
' Resize the TextFrame item to match the document width
sampleText.Width = documentRef.Width
sampleText.Left = 0
2.
Run the script.
Accessing and referencing objects
When you write a script, you must first decide which file, or
Document
, the script should act on. Through
the
Application
object, the script can create a new document, open an existing document, or act on a
document that is already open.
The script can create new objects in the document, operate on objects that the user selected, or operate
on objects in one of the object collections. The following sections illustrate techniques for accessing,
referencing, and manipulating Illustrator objects.
Obtaining objects from collections
Generally, to obtain a reference to a specific object, you can navigate the containment hierarchy. For
example, to use the
myPath
variable to store a reference to the first
PathItem
in the second layer of the
active document:
Set myPath = appRef.ActiveDocument.Layers(2).PathItems(1)
The following scripts demonstrate how to reference an object as part of a document:
Set documentRef = appRef.ActiveDocument
Set pageItemRef = documentRef.PageItems(1)