Adobe 27510753 Scripting Guide - Page 120

Basic document management, Creating a new document, Opening a document, Closing a document

Page 120 highlights

112 Working with Documents in JavaScript Adobe InDesign CS2 Scripting Guide Basic document management In almost all situations, your script needs to either open or create a document, save it, and then close it. Creating a new document If a document does not already exist, you must create one. To create a document: //MakeDocument.jsx //An InDesign CS2 JavaScript //Creates a new document. var myDocument = app.documents.add(); The document.add method can take two optional parameters, as shown in the following script: //MakeDocumentWithParameters.jsx //An InDesign CS2 JavaScript //Shows how to use the parameters of the document.open method. //The first parameter (showingWindow) controls the visibility of the //document. Hidden documents are not minimized, and will not appear until //you add a new window to the document. The second parameter (documentPreset) //specifies the document preset to use. The following line assumes that //you have a document preset named "Flyer" on your system. var myDocument = app.documents.add(true, app.documentPresets.item("Flyer")); Opening a document The following example script shows how to open an existing document: //OpenDocument.jsx //An InDesign CS2 JavaScript //Opens an existing document. You'll have to fill in your own file path. app.open(File("/c/myTestDocument.indd")); You can choose to prevent the document from displaying (hide it) by setting the showingWindow parameter of the open method to false (the default is true). You might want to do this to improve performance of a script. To show a hidden document, create a new window, as shown in the following script: //OpenDocumentInBackground.jsx //An InDesign CS2 JavaScript //Opens an existing document in the background, then shows the document. //You'll have to fill in your own file path. var myDocument = app.open(File("/c/myTestDocument.indd"), false); //At this point, you could do things with the document without showing the //document window. In some cases, scripts will run faster when the document //window is not visible. //When you want to show the hidden document, create a new window. var myLayoutWindow = myDocument.windows.add(); Closing a document The close method closes a document: //CloseDocument.jsx //An InDesign CS2 JavaScript //Closes the active document. app.activeDocument.close(); //Note that you could also use: //app.documents.item(0).close();

  • 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

112
Working with Documents in JavaScript
Adobe InDesign CS2 Scripting Guide
Basic document management
In almost all situations, your script needs to either open or create a document, save it, and then close it.
Creating a new document
If a document does not already exist, you must create one. To create a document:
//MakeDocument.jsx
//An InDesign CS2 JavaScript
//Creates a new document.
var myDocument = app.documents.add();
The
document.add
method can take two optional parameters, as shown in the following script:
//MakeDocumentWithParameters.jsx
//An InDesign CS2 JavaScript
//Shows how to use the parameters of the document.open method.
//The first parameter (showingWindow) controls the visibility of the
//document. Hidden documents are not minimized, and will not appear until
//you add a new window to the document. The second parameter (documentPreset)
//specifies the document preset to use. The following line assumes that
//you have a document preset named "Flyer" on your system.
var myDocument = app.documents.add(true, app.documentPresets.item("Flyer"));
Opening a document
The following example script shows how to open an existing document:
//OpenDocument.jsx
//An InDesign CS2 JavaScript
//Opens an existing document. You’ll have to fill in your own file path.
app.open(File("/c/myTestDocument.indd"));
You can choose to prevent the document from displaying (hide it) by setting the
showingWindow
parameter
of the
open
method to false (the default is true). You might want to do this to improve performance of a
script. To show a hidden document, create a new window, as shown in the following script:
//OpenDocumentInBackground.jsx
//An InDesign CS2 JavaScript
//Opens an existing document in the background, then shows the document.
//You’ll have to fill in your own file path.
var myDocument = app.open(File("/c/myTestDocument.indd"), false);
//At this point, you could do things with the document without showing the
//document window. In some cases, scripts will run faster when the document
//window is not visible.
//When you want to show the hidden document, create a new window.
var myLayoutWindow = myDocument.windows.add();
Closing a document
The
close
method closes a document:
//CloseDocument.jsx
//An InDesign CS2 JavaScript
//Closes the active document.
app.activeDocument.close();
//Note that you could also use:
//app.documents.item(0).close();