-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopensplitclose.jsx
259 lines (231 loc) · 5.9 KB
/
opensplitclose.jsx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var api = {
getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder )
{
var exts = aExtensions? aExtensions.join("|") : ".+" ;
var pattern = new RegExp ( "\\."+exts+"$", "g" );
var files = aFiles? aFiles : [];
var filterFunction = function(file)
{
return pattern.test ( file.name );
}
if ( bRecursive )
{
var foFiles = fo.getFiles();
while ( f = foFiles.shift() )
{
if ( f instanceof Folder )
{
if (includeFolder===true) files[ files.length ] = f;
this.getFiles ( f, aExtensions, true, files );
}
if ( f instanceof File && pattern.test ( f.name ) )
files[ files.length ] = f;
}
return files;
}
else
{
return fo.getFiles ( filterFunction );
}
},
}
var fo = Folder.selectDialog(), u, files;
var destination = Folder.selectDialog();
if ( fo ) {
files = api.getFiles ( fo, ["indd"], true, u, false );
for(var i = 0; i < files.length; i++){
app.open(files[i]);
// 1. Control for documents open. If true, the script launches the dialog
if(app.documents.length >0)
{
var doc = app.documents[0];
if(doc.saved==true)
{
extractdlg();
}
else
{
alert("Please save you file first before processing the script");
}
}
else
{
alert("No documents open !");
}
// 2. Gathers many infos on the document.
function pageinfos()
{
var pg = doc.pages;
var pglg = pg.length;
var pFirst = Number(pg[0].name);
var pLast = Number(pg[pglg-1].name);
var pgHeigth = doc.documentPreferences.pageHeight;
var pgWitdh = doc.documentPreferences.pageWidth;
var docname = String(doc.fullName).replace(/\.indd$/, "");
var docpath = doc.filePath;
var docfullname = doc.fullName;
var infoarr = [pglg, pFirst, pLast, pgHeigth,pgWitdh,docname,docpath,docfullname];
return infoarr;
}
// 3. Main function. First part is the dialog
function extractdlg()
{
var docfile = String(pageinfos()[7]);
var dlg = app.dialogs.add({name : "Pages Extractor 1.1 - �www.loicaigon.com"});
with(dlg)
{
var firstclmn = dialogColumns.add();
with(firstclmn)
{
var firstrow = dialogRows.add();
with(firstrow)
{
var clmn1 = dialogColumns.add();
with(clmn1)
{
var row1 = dialogRows.add();
row1.staticTexts.add({staticLabel : "Extract pages..."});
var row2 = dialogRows.add();
with(row2)
{
var r2c2 = dialogColumns.add();
with(r2c2)
{
var r2c2r1 = dialogRows.add();
var pgStart = r2c2r1.realEditboxes.add({editValue:pageinfos()[1], minWidth: 30});
}
var r2c3 = dialogColumns.add();
with(r2c3)
{
var r2c3r1 = dialogRows.add();
r2c3r1.staticTexts.add({staticLabel : "to"});
}
var r2c4 = dialogColumns.add();
with(r2c4)
{
var r2c4r1 = r2c4.dialogRows.add();
var pgEnd = r2c4r1.realEditboxes.add({editValue:pageinfos()[2], minWidth: 30});
}
}
}
}
var secondrow = dialogRows.add();
with(secondrow)
{
var clmn2 = dialogColumns.add();
with(clmn2)
{
var row2 = dialogRows.add();
with(row2)
{
var sepbox = checkboxControls.add({staticLabel: "Extract as separate pages", checkedState:true});
}
}
}
var thirdrow = dialogRows.add();
with(thirdrow)
{
var clmn3 = dialogColumns.add();
with(clmn3)
{
var row3 = dialogRows.add();
with(row3)
{
var rembox = checkboxControls.add({staticLabel: "Remove pages after extraction", checkedState:false});
}
}
}
var foutrhrow = dialogRows.add();
with(foutrhrow)
{
var clmn4 = dialogColumns.add();
with(clmn4)
{
var row4 = dialogRows.add();
with(row4)
{
var savebox = checkboxControls.add({staticLabel: "Choose other extraction folder", checkedState:true});
}
}
}
}
}
// If the user made good choices, the script operates.
{
if(pgStart.editValue >= pageinfos()[2] || pgEnd.editValue <= pageinfos()[1])
{
alert("The pages numbers may be at least "+pageinfos()[1] +" for the first page of the range and "+ pageinfos()[2] + " at maximum for the last page");
}
else
{
// If the user choose to pick a different folder, he will be asked for. Otherwise, the dafault folder is the one containing the file.
if(savebox.checkedState==true)
{
var extractfolder = destination;
if(!extractfolder)
{
exit();
}
else
{
var saveextractfolder = String(extractfolder)+"/" +String(doc.name).replace (/\.indd/, "");
}
}
else
{
var saveextractfolder = String(pageinfos()[5]);
}
var rem0 = pageinfos()[0]-1;
var rem2 = (pgStart.editValue-2);
// Variables definition regarding to the choice of the user to separate or not the extracted pages.
if(sepbox.checkedState==true)
{
var W = pgEnd.editValue-pgStart.editValue+1;
var rem1 = pgStart.editValue;
}
else
{
var W = 1;
var rem1 = pgEnd.editValue;
}
// Extraction loop
for(w=0; w<W;w++)
{
if(sepbox.checkedState==true)
{
var exportdocname = "_Pg" +(pgStart.editValue+w) +".indd";
}
else
{
var exportdocname = "_Pg"+pgStart.editValue+"_to_Pg_"+pgEnd.editValue +".indd";
}
for(var i=rem0; i>=rem1+w;i--)
{
doc.pages[i].remove();
}
for(var i=rem2+w; i>=0;i--)
{
doc.pages[i].remove();
}
var exportdoc = doc.save(File(saveextractfolder + exportdocname));
exportdoc.close(SaveOptions.no);
if(sepbox.checkedState==true && w<(pgEnd.editValue-pgStart.editValue))
{
app.open(File(docfile));
}
}
// If the user chose to remove the extracted pages from the original document, it will re open the first document then remove the unuseful pages.
if(rembox.checkedState == true)
{
app.open(File(docfile));
for(var i=pgEnd.editValue-1; i>=pgStart.editValue-1;i--)
{
doc.pages[i].remove();
}
app.activeDocument.close(SaveOptions.yes);
}
}
}
}
}
}