Adobe 27510753 Scripting Guide - Page 32

JavaScript, Start InDesign.

Page 32 highlights

24 Getting Started with InDesign Scripting Adobe InDesign CS2 Scripting Guide 2. Use the CommandButton tool to create a new button on the default form. Double-click the button to open the Code window. 3. Enter the following code (between the Private Sub and End Sub lines defining the code under the button). The lines preceded by Rem are comments and are ignored by the scripting system. They're included to describe the operation of the program. As you look through the script, you'll see how we create, then address, each object in turn. You do not need to enter the comments. Rem Hello World Rem Declare variable types (optional if Option Explicit is off). Dim myInDesign As InDesign.Application Dim myDocument As InDesign.Document Dim myPage As InDesign.Page Dim myTextFrame As InDesign.TextFrame Rem End of variable type declarations. Set myInDesign = CreateObject("InDesign.Application.CS2") Rem Create a new document. Set myDocument = myInDesign.Documents.Add Rem Get a reference to the first page. Set myPage = myDocument.Pages.Item(1) Rem Create a text frame. Set myTextFrame = myDocument.TextFrames.Add Rem Specify the size and shape of the text frame. myTextFrame.GeometricBounds = Array("0p0", "0p0", "18p0", "18p0") Rem Enter text in the text frame. myTextFrame.Contents = "Hello World!" 4. Save the form. 5. Start InDesign. 6. Return to Visual Basic and run the program. 7. Click the button you created earlier. InDesign creates a new publication, creates a new text frame, and then enters the text. JavaScript To create a JavaScript: 1. Using any text editor (including InDesign or the ExtendScript Tookit), enter the following text: //Hello World! var myDocument = app.documents.add(); with(myDocument){ var myPage = myDocument.pages.item(0); with(myPage){ //Create a new text frame and assign it to the variable "myTextFrame" var myTextFrame = textFrames.add(); //Specify the size and shape of the text frame. myTextFrame.geometricBounds = [ "0p0", "0p0", "18p0", "18p0"]; //Enter text in the text frame. myTextFrame.contents = "Hello World!" } } 2. Save the text as a plain text file with the file extension .jsx in the Scripts folder inside the Presets folder in your InDesign folder. 3. Test the script by double-clicking the script name in the InDesign Scripts palette.

  • 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
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

24
Getting Started with InDesign Scripting
Adobe InDesign CS2 Scripting Guide
2.
Use the CommandButton tool to create a new button on the default form. Double-click the button to
open the Code window.
3. Enter the following code (between the
Private Sub
and
End Sub
lines defining the code under the
button). The lines preceded by
Rem
are comments and are ignored by the scripting system. They’re in-
cluded to describe the operation of the program. As you look through the script, you’ll see how we create,
then address, each object in turn. You do not need to enter the comments.
Rem Hello World
Rem Declare variable types (optional if Option Explicit is off).
Dim myInDesign As InDesign.Application
Dim myDocument As InDesign.Document
Dim myPage As InDesign.Page
Dim myTextFrame As InDesign.TextFrame
Rem End of variable type declarations.
Set myInDesign = CreateObject("InDesign.Application.CS2")
Rem Create a new document.
Set myDocument = myInDesign.Documents.Add
Rem Get a reference to the first page.
Set myPage = myDocument.Pages.Item(1)
Rem Create a text frame.
Set myTextFrame = myDocument.TextFrames.Add
Rem Specify the size and shape of the text frame.
myTextFrame.GeometricBounds = Array("0p0", "0p0", "18p0", "18p0")
Rem Enter text in the text frame.
myTextFrame.Contents = "Hello World!"
4. Save the form.
5. Start InDesign.
6.
Return to Visual Basic and run the program.
7.
Click the button you created earlier. InDesign creates a new publication, creates a new text frame, and then
enters the text.
JavaScript
To create a JavaScript:
1.
Using any text editor (including InDesign or the ExtendScript Tookit), enter the following text:
//Hello World!
var myDocument = app.documents.add();
with(myDocument){
var myPage = myDocument.pages.item(0);
with(myPage){
//Create a new text frame and assign it to the variable "myTextFrame"
var myTextFrame = textFrames.add();
//Specify the size and shape of the text frame.
myTextFrame.geometricBounds = [ "0p0", "0p0", "18p0", "18p0"];
//Enter text in the text frame.
myTextFrame.contents = "Hello World!"
}
}
2.
Save the text as a plain text file with the file extension
.jsx
in the Scripts folder inside the Presets folder in
your InDesign folder.
3.
Test the script by double-clicking the script name in the InDesign Scripts palette.