Adobe 0046100128056 Scripting Guide - Page 18

Running Scripts at Startup, Session and Main Script Execution

Page 18 highlights

CHAPTER 2: Scripting Features Running Scripts at Startup 18 var myDocument = app.documents.add(); myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; var myPage = myDocument.pages.item(0); var myRectangle = myPage.rectangles.add({geometricBounds:[72, 72, 144, 144]}); //Insert a custom label using insertLabel. The first parameter is the //name of the label, the second is the text to add to the label. myRectangle.insertLabel("CustomLabel", "This is some text stored in a custom label."); //Extract the text from the label and display it in an alert. var myString = myRectangle.extractLabel("CustomLabel"); alert("Custom label contained: " + myString); Running Scripts at Startup To run a script when InDesign starts, put the script in the Startup Scripts folder in the Scripts folder (for more information, see "Installing Scripts" in Adobe InDesign CS5 Scripting Tutorial). NOTE: Scripts run in the session ExtendScript engine when InDesign starts can create objects and functions that will be available to other scripts for the duration of the session. For more information, see "Session and Main Script Execution" on page 18. Session and Main Script Execution InDesign has two ways to run a JavaScript, session and main. These names correspond to the ExtendScript "engine" used to run the script. By default, when you run an InDesign JavaScript, the script is interpreted and executed by the "main" ExtendScript engine, which is destroyed when the script completes execution. Script objects created by the script do not persist. Scripts run in the session engine can create objects that persist until you close InDesign. You can refer to these objects from other scripts run in the session engine. To set the session engine as the target of an InDesign JavaScript, add the following line to the start of your script. #targetengine "session" You can create your own persistent ExtendScript interpretation and execution environment. To do this, use the #targetenging statement and provide your own ExtendScript engine name, as shown in the following script fragment: #targetengine "adobe"

  • 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
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209

C
HAPTER
2: Scripting Features
Running Scripts at Startup
18
var myDocument = app.documents.add();
myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
var myPage = myDocument.pages.item(0);
var myRectangle = myPage.rectangles.add({geometricBounds:[72, 72, 144, 144]});
//Insert a custom label using insertLabel. The first parameter is the
//name of the label, the second is the text to add to the label.
myRectangle.insertLabel("CustomLabel", "This is some text stored in a custom label.");
//Extract the text from the label and display it in an alert.
var myString = myRectangle.extractLabel("CustomLabel");
alert("Custom label contained: " + myString);
Running Scripts at Startup
To run a script when InDesign starts, put the script in the Startup Scripts folder in the Scripts folder (for
more information, see “Installing Scripts” in
Adobe InDesign CS5 Scripting Tutorial
).
N
OTE
:
Scripts run in the session ExtendScript engine when InDesign starts can create objects and
functions that will be available to other scripts for the duration of the session. For more information, see
“Session and Main Script Execution” on page 18
.
Session and Main Script Execution
InDesign has two ways to run a JavaScript,
session
and
main
. These names correspond to the
ExtendScript “engine” used to run the script.
By default, when you run an InDesign JavaScript, the script is interpreted and executed by the “main”
ExtendScript engine, which is destroyed when the script completes execution. Script objects created by
the script do not persist.
Scripts run in the session engine can create objects that persist until you close InDesign. You can refer to
these objects from other scripts run in the
session
engine. To set the
session
engine as the target of an
InDesign JavaScript, add the following line to the start of your script.
#targetengine "session"
You can create your own persistent ExtendScript interpretation and execution environment. To do this, use
the
#targetenging
statement and provide your own ExtendScript engine name, as shown in the
following script fragment:
#targetengine "adobe"