Adobe 65010248 Scripting Guide - Page 50

Creating new objects, Working with selections, Selecting artwork objects

Page 50 highlights

CHAPTER 6: Scripting with VBScript Accessing and referencing objects 50 In the script below, the variable pageItemRef will not necessarily refer to the same object as the above script, since this script includes a reference to a layer: Set documentRef = appRef.ActiveDocument Set pageItemRef = documentRef.Layers(1).PageItems(1) VBScript indexes start at 1 for object collections; however, VBScript allows you to specify whether array indexes start at 1 or 0. For information on specifying the index start number for arrays, see any VBScript textbook or tutorial. Creating new objects You can use a script to create new objects. To create objects that are available from collection objects, use the collection object's Add method: Set myDoc = appRef.Documents.Add() Set myLayer = myDoc.Layers.Add() Some collection objects do not have an Add method. To create an object of this type, define a variable and use the CreateObject method. For example, the following code creates a new CMYKColor object using the variable name newColor: Set newColor = CreateObject ("Illustrator.CMYKColor") Working with selections When the user makes a selection in a document, the selected objects are stored in the document's selection property. To access all selected objects in the active document: Set appRef = CreateObject ("Illustrator.Application") Set documentRef = appRef.ActiveDocument selectedObjects = documentRef.Selection Depending on what is selected, the selection property value can be an array of any type of art objects. To get or manipulate the properties of the selected art items, you must retrieve the individual items in the array. To find out an object's type, use the typename property. The following sample gets the first object in the array, then displays the object's type: Set appRef = CreateObject ("Illustrator.Application") Set documentRef = appRef.ActiveDocument selectedObjects = documentRef.Selection Set topObject = selectedObjects(0) MsgBox(topObject.Typename) The MsgBox method does not display a dialog when the script is run from the Illustrator Scripts menu (File > Scripts). The first object in a selection array is the selected object that was last added to the page, not the last object selected. Selecting artwork objects To select an artwork object, use the object's Selected property.

  • 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
50
In the script below, the variable
pageItemRef
will not necessarily refer to the same object as the above
script, since this script includes a reference to a layer:
Set documentRef = appRef.ActiveDocument
Set pageItemRef = documentRef.Layers(1).PageItems(1)
VBScript indexes start at 1 for object collections; however, VBScript allows you to specify whether array
indexes start at 1 or 0. For information on specifying the index start number for arrays, see any VBScript
textbook or tutorial.
Creating new objects
You can use a script to create new objects. To create objects that are available from collection objects, use
the collection object’s
Add
method:
Set myDoc = appRef.Documents.Add()
Set myLayer = myDoc.Layers.Add()
Some collection objects do not have an
Add
method. To create an object of this type, define a variable and
use the
CreateObject
method. For example, the following code creates a new
CMYKColor
object using
the variable name
newColor
:
Set newColor = CreateObject ("Illustrator.CMYKColor")
Working with selections
When the user makes a selection in a document, the selected objects are stored in the document’s
selection
property. To access all selected objects in the active document:
Set appRef = CreateObject ("Illustrator.Application")
Set documentRef = appRef.ActiveDocument
selectedObjects = documentRef.Selection
Depending on what is selected, the
selection
property value can be an array of any type of art objects. To
get or manipulate the properties of the selected art items, you must retrieve the individual items in the
array. To find out an object’s type, use the
typename
property.
The following sample gets the first object in the array, then displays the object’s type:
Set appRef = CreateObject ("Illustrator.Application")
Set documentRef = appRef.ActiveDocument
selectedObjects = documentRef.Selection
Set topObject = selectedObjects(0)
MsgBox(topObject.Typename)
The
MsgBox
method does not display a dialog when the script is run from the Illustrator Scripts menu
(File > Scripts).
The first object in a selection array is the selected object that was last
added
to the page, not the last object
selected.
Selecting artwork objects
To select an artwork object, use the object’s
Selected
property.