-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeTextframes_ID.jsx
613 lines (571 loc) · 17.7 KB
/
MergeTextframes_ID.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/******************************
* @title MergeText_ID
* @author Justin Putney
* @info http://putney.ajarproductions.com
* @version 1.2.0
*******************************/
/*******************************************
* begin code from ominoDialogMaker.jsx
* by David Van Brink
* source: http://omino.com/pixelblog/2008/09/21/35/
********************************************/
var D_MARGIN = 6;
var D_CONTROLHEIGHT = 20;
var D_BUTTONWIDTH = 100;
var D_CONTROLLABELWIDTH = 160;
var D_CONTROLWIDTH = 150;
var D_DIALOG_WIDTH = 400;
var S2 = 1.41421356237309504880;
// create a rectangle for a new control, walking downwards.
function _odControlShared(label,name) {
dialog = this;
var y = dialog.curYPos;
var itemHeight = D_CONTROLHEIGHT;
var itemBump = itemHeight + D_MARGIN;
if(label != "")
label += ":";
var labelCtl = dialog.add('statictext',[20,y,20 + D_CONTROLLABELWIDTH,y+itemHeight],label);
labelCtl.justify = "right";
var controlBox = new Object();
controlBox.left = 20 + D_CONTROLLABELWIDTH + 10;
controlBox.top = y;
controlBox.right = controlBox.left + D_CONTROLWIDTH;
controlBox.bottom = controlBox.top + itemHeight;
dialog.curYPos = controlBox.bottom + D_MARGIN;
return controlBox;
}
function _odControlSharedFinish(control,name,valueFieldName) {
oD = this;
oD.items[name] = control;
oD.itemValueFieldNames[name] = valueFieldName;
oD.itemNames[oD.itemNames.length] = name;
}
function _odNumber(label,name,value) {
oD = this;
var controlBox = oD._odControlShared(label,name);
var control = oD.add('edittext',controlBox,value);
control.value = value;
control.onChange = function(){this.value = (this.text) * 1.0; this.text = this.value;}; // make them all .value accessible
oD._odControlSharedFinish(control,name,"text");
return control;
}
function _odText(label,name,value) {
oD = this;
var controlBox = oD._odControlShared(label,name);
var control = oD.add('edittext',controlBox,value);
control.value = value;
control.onChange = function(){this.value = this.text; }; // make them all .value accessible
oD._odControlSharedFinish(control,name,"text");
return control;
}
function _setColorFromButton(victim,button) {
var g = victim.graphics;
var n = button.value;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, n);
g.backgroundColor = myBrush;
}
/*
color values are array of three floats, 0.0 .. 1.0.
*/
function _odColor(label,name,color) {
oD = this;
var controlBox = oD._odControlShared(label,name);
var swatchBox = [controlBox.left + 40,controlBox.top,controlBox.right,controlBox.bottom];
var buttonBox = [controlBox.left,controlBox.top,controlBox.left + 30,controlBox.bottom];
var swatch = oD.add('group',swatchBox);
var button = oD.add('button',buttonBox);
button.swatch = swatch;
button.value = color;
button.onClick = function(){
var n = doColorPicker(this.value);
this.value = n;
_setColorFromButton(swatch,this);
};
_setColorFromButton(swatch,button);
oD._odControlSharedFinish(button,name,"value");
return button;
}
/*
Add a button and static text to the dialog;
the button refers to the text "nameCtl",
and has .filePrompt and .fileExtension.
*/
function _odFileCommon(label,name,path,prompt,extension) {
var controlBox = oD._odControlShared(label,name);
var buttonWidth = 80;
var buttonBox = [controlBox.left,controlBox.top,controlBox.left + buttonWidth,controlBox.bottom];
var nameBox = [controlBox.left + buttonWidth + 10,controlBox.top,D_DIALOG_WIDTH,controlBox.bottom];
var f = new File(path);
var nameCtl = oD.add('statictext',nameBox);
var button = oD.add('button',buttonBox,'...');
button.nameCtl = nameCtl;
nameCtl.text = f.name;
button.value = f.fsName;
button.file = f;
button.filePrompt = prompt;
button.fileExtension = extension;
oD._odControlSharedFinish(button,name,"value");
return button;
}
function _odOpenFile(label,name,path,prompt,extension) {
oD = this;
var buttonCtl = _odFileCommon(label,name,path,prompt,extension);
buttonCtl.onClick = function(){
var f = this.file.openDlg(this.filePrompt);
if(f) {
this.file = f;
this.value = f.fsName;
this.nameCtl.text = f.name;
}
};
return buttonCtl;
}
function _odSaveFile(label,name,path,prompt,extension) {
oD = this;
var buttonCtl = _odFileCommon(label,name,path,prompt,extension);
buttonCtl.onClick = function(){
var f = this.file.saveDlg(this.filePrompt);
if(f) {
this.file = f;
this.value = f.fsName;
this.nameCtl.text = f.name;
}
};
return buttonCtl;
}
function _odSelectFolder(label,name,path,prompt,extension) {
oD = this;
var buttonCtl = _odFileCommon(label,name,path,prompt,extension);
buttonCtl.folder = new Folder(path); // folder, pls, not file
buttonCtl.nameCtl.text += "/";
buttonCtl.onClick = function(){
var f = this.folder.selectDlg(this.filePrompt);
if(f) {
this.folder = f;
this.value = f.fsName;
this.nameCtl.text = f.name + "/";
}
};
return buttonCtl;
}
function _odCheckbox(label,name,value,checkboxText) {
oD = this;
var controlBox = oD._odControlShared(label,name);
var control = oD.add('checkbox',controlBox,checkboxText);
control.value = value;
//control.onChange = function(){this.value = this.text;};
oD._odControlSharedFinish(control,name,"value");
return control;
}
function _odRadioButtons(label,name,value,radioChoices) {
var oD = this;
controlBox = oD ._odControlShared(label,name);
var itemHeight = controlBox.bottom - controlBox.top;
result = oD.add('edittext',controlBox,value); // hidden text field to control it...
result.onChange = function(){
var i;
this.value = this.text;
for(i = 0; i < this.buttons.length; i++) {
var button = this.buttons[i];
button.value = (button.theChoice == this.text);
}
}
result.hide();
result.value = value;
result.buttons = new Array();
var i;
for(i = 0; i < radioChoices.length; i++) {
var choice = radioChoices[i];
if(i > 0) {
var bump = itemHeight + D_MARGIN;
controlBox.top += bump;
controlBox.bottom += bump;
oD.curYPos += bump;
}
// each radiobutton object pokes its choice into the ersatz control,
// so it looks like a simple value.
// ("Grouping" appears to be by adjacent additions only. Nice!)
var rb = oD .add('radiobutton',controlBox,choice);
rb.value = choice == value;
rb.theChoice = choice;
rb.theGroupErsatzControl = result;
rb.onClick = function(){this.theGroupErsatzControl.value = this.theChoice;};
result.buttons[result.buttons.length] = rb;
}
oD._odControlSharedFinish(result,name,"text");
return result;
}
function _odMenu(label,name,value,menuChoices) {
var oD = this;
controlBox = oD ._odControlShared(label,name);
var itemHeight = controlBox.bottom - controlBox.top;
var control = oD.add('dropdownlist',controlBox,menuChoices);
// I couldnt discern how to get this from the "items" array, so I stash menuChoice for later. dvb08.
control.menuChoices = menuChoices;
control.value = value;
// set the initial selection index
var index = 0;
for(var i = 0; i < menuChoices.length; i++) {
if(value == menuChoices[i])
index = i;
}
control.selection = index;
control.onChange = function() {
this.value = this.selection.text;
} // make them all .value accessible
oD._odControlSharedFinish(control,name,"value");
}
function _odSectionLabel(label) {
var oD = this;
var b2 = new Object();
b2.left = D_MARGIN;
b2.top = oD.curYPos;
b2.right = b2.left + D_DIALOG_WIDTH;
b2.bottom = b2.top + D_CONTROLHEIGHT;
oD.curYPos += D_CONTROLHEIGHT + D_MARGIN;
oD.add('statictext',b2,label + ':',{multiline:true});
}
function _odBoxedText(lines,text) {
var oD = this;
var width = D_DIALOG_WIDTH;
var height = lines * 16;
var b2 = new Object();
var b = new Object();
b.top = oD.curYPos;
b.bottom = b.top + height + 2 * D_MARGIN;
b.left = D_MARGIN;
b.right = b.left + width;
oD.curYPos = b.bottom + D_MARGIN;
var panel = oD.add('panel',b);
b2.left = D_MARGIN;
b2.top = D_MARGIN;
b2.right = b2.left + width - 2 * D_MARGIN;
b2.bottom = b2.top + height;
panel.add('statictext',b2,text,{multiline:true});
}
function _odSeparator() {
var oD = this;
var height = oD.groupGap;
var barWidth = oD.ominoDialogWidth;
if(barWidth) {
var b = new Object();
b.top = oD.curYPos + height / 2;
b.bottom = b.top;
b.left = D_MARGIN;
b.right = b.left + barWidth;
var barHeight = 2;
b.top -= barHeight / 2;
b.bottom = b.top + barHeight;
oD.add('panel',b);
}
oD.curYPos += height;
}
function _odAppendGap() {
oD = this;
oD.curYPos += oD.groupGap;
}
function appendOKCancel(dialog) {
var y = dialog.curYPos;
var cancelRect = new Object();
var okRect = new Object();
cancelRect.left = D_MARGIN
cancelRect.top = y;
cancelRect.right = cancelRect.left + D_BUTTONWIDTH;
cancelRect.bottom = cancelRect.top + D_CONTROLHEIGHT;
okRect.left = cancelRect.right + D_MARGIN + D_MARGIN;
okRect.top = y;
okRect.right = okRect.left + D_BUTTONWIDTH;
okRect.bottom = okRect.top + D_CONTROLHEIGHT;
var cancelBtn = dialog.add('button',cancelRect,'Cancel',{name:'cancel'});
var okBtn = dialog.add('button',okRect,'OK',{name:'ok'});
cancelBtn.theDialog = dialog;
cancelBtn.onClick = function(){this.theDialog.close(0);}; // 0 on cancel
okBtn.theDialog = dialog;
okBtn.onClick = function(){this.theDialog.close(1);}; // 1 on ok
dialog.curYPos = okRect.bottom + D_MARGIN;
}
function trimDialogBounds(dialog) {
var xMax = 20;
var yMax = 20;
var n = dialog.children.length;
var i;
for(i = 0; i < n; i++) {
var aChild= dialog.children[i];
var aChildBounds = aChild.bounds;
if(aChildBounds.right > xMax)
xMax = aChildBounds.right;
if(aChildBounds.bottom > yMax)
yMax = aChildBounds.bottom;
}
dialog.bounds.right = dialog.bounds.left + xMax + D_MARGIN;
dialog.bounds.bottom = dialog.bounds.top + yMax + D_MARGIN;
// actually... allow bottom gaps.
dialog.bounds.bottom = dialog.curYPos + dialog.bounds.top;
}
function newOminoDialog(name) {
var oD = new Window('dialog',name,[100,100,500,500]);
oD.curYPos = 20;
oD.groupGap = 12;
oD.itemNames = new Array();
oD.item
oD.items = new Array();
oD.itemValueFieldNames = new Object(); // to poke a value into the dialog, each control's appropriate field, like "text" or "value"
oD.ominoDialogWidth = D_DIALOG_WIDTH;
oD.gap = _odAppendGap;
oD.number = _odNumber;
oD.string = _odText;
oD.radioButtons = _odRadioButtons;
oD.checkbox = _odCheckbox;
oD.sectionLabel = _odSectionLabel;
oD.separator = _odSeparator;
oD.boxedText = _odBoxedText;
oD.color = _odColor;
oD.openFile = _odOpenFile;
oD.selectFolder = _odSelectFolder;
oD.saveFile = _odSaveFile;
oD.menu = _odMenu;
oD.set = _odSet;
oD.run = _odRun;
oD.get = _odGet; oD._odControlShared = _odControlShared;
oD._odControlSharedFinish = _odControlSharedFinish;
return oD;
}
function _odGet() {
var values = new Object();
var name;
for(name in this.items) {
var value = this.items[name].value;
values[name] = value;
}
return values;
}
function _odSet(values) {
var oD = this;
if(!values)
return;
for(var p in values) {
var value = values[p];
var item = oD.items[p];
if(!item)
continue;
var itemValueFieldName = oD.itemValueFieldNames[p];
if(!itemValueFieldName)
continue;
item[itemValueFieldName] = value;
item.notify('onChange'); // to get the refresh
}
}
function _odRun() {
var oD = this;
if(!oD.finishingTouches) {
oD.separator(oD);
oD.gap();
appendOKCancel(oD);
oD.gap();
trimDialogBounds(oD);
oD.finishingTouches = true;
}
var resultCode = oD.show();
if(resultCode != 1) // cancel
return null;
var result = oD.get();
return result;
}
/*******************************************
* end code from ominoDialogMaker.jsx
********************************************/
/*******************************************
* begin code for Merging Text
********************************************/
var sortProp = "top";
var doc = app.activeDocument;
var sel = doc.selection;
var tfs = new Array();
var separator = "[><]";
var fitToContent = true;
var fitToSelection = false;
var minversion = 5; //for dialog to run
var version = parseInt(app.version.split(".").shift());
var coordTop = new Array();
var coordBottom = new Array();
var coordRight = new Array();
var coordLeft = new Array();
var applyTo = "selected frames";
var defaultIgnoreLocked = true;
var ignoreLocked = defaultIgnoreLocked;
init();
function init(){
if(version >= minversion) runDialog();
else run();
}
function run(){
if(applyTo == "selected frames") {
tfs = collectFields(sel);
if (tfs.length == 1) {
alert("You must select more than one textframe.");
return;
} else if (tfs.length < 1) {
alert("No textframes selected.");
return;
} else {
tfs = tfs.sort(sortFields);
assemble(tfs);
}
} else if(applyTo == "current page") {
tfs = collectFields(app.activeWindow.activePage.textFrames);
if (tfs.length == 1) {
alert("The current page only has one textframe.");
return;
} else if (tfs.length < 1) {
alert("The current page does not have any textframes.");
return;
} else {
tfs = tfs.sort(sortFields);
assemble(tfs);
}
} else if(applyTo == "all pages") {
var storedPage = app.activeWindow.activePage;
for(var i=0; i < doc.pages.count(); i++){
app.activeWindow.activePage = doc.pages.item(i); //allows proper fitting on r-hand spread pgs
tfs = collectFields(doc.pages.item(i).textFrames);
tfs = tfs.sort(sortFields);
assemble(tfs);
}
app.activeWindow.activePage = storedPage;
}
}
function runDialog()
{
var omd = newOminoDialog("Merge Textframes");
omd.menu("Apply to","applyto","selected frames",["selected frames",
"current page", "all pages"]); //need a way to do "selected pages"
omd.separator();
omd.checkbox("Ignore locked layers", "ignorelocked", defaultIgnoreLocked , "(applies to pages)");
omd.separator();
omd.radioButtons("Sort by","sort","left",["top","left","bottom","right"]);
omd.separator();
omd.string("Separator","separator","\\n");
omd.boxedText(3,"\\r = hard return, \\n = soft return, \\t = tab \n\nUse hard return(s) if you want to preserve paragraphs styles");
omd.separator();
omd.checkbox("Fit Frame to", "afit", fitToContent , "merged content");
omd.checkbox("", "sfit", fitToSelection , "selection size");
var result = omd.run();
if(result != null) {
separator = result.separator;
separator = replace(separator, "\\r", "\r");
separator = replace(separator, "\\t", "\t");
separator = replace(separator, "\\n", "\n");
sortProp = result.sort;
fitToContent = result.afit;
fitToSelection = result.sfit;
applyTo = result.applyto;
ignoreLocked = result.ignorelocked;
run();
}
}
function collectFields(selArr) {
var tArr = new Array();
selArr = (ignoreLocked) ? getUnlockedLayerItems(selArr) : selArr;
for(var i=0; i < selArr.length; i++){
var t = selArr[i];
if (t == undefined) continue;
if(t.constructor.name == "TextFrame"){
tArr.push(t);
var bounds = t.geometricBounds;
//store coordinates for content fitting
coordTop.push(bounds[0]);
coordLeft.push(bounds[1]);
coordBottom.push(bounds[2]);
coordRight.push(bounds[3]);
}
}
return tArr;
}
function getUnlockedLayerItems(inArr) {
var outArr = new Array();
for(var i=0; i < inArr.length; i++){
if(!inArr[i].itemLayer.locked) outArr.push(inArr[i]);
}
return outArr;
}
function assemble(inArr){
var arr = inArr;
var targFrame = arr[0];
//var page = targFrame.page; //FIX
for (var i=arr.length-1; i > 0; i--){
var storedLayer = arr[i].itemLayer;
var storedLayerLock = storedLayer.locked;
if(separator.length > 0) {
arr[i-1].textStyleRanges.lastItem().contents += separator;
}
arr[i].previousTextFrame = arr[i-1];
arr[i].itemLayer.locked = false;
arr[i].locked = false; //just in case
arr[i].remove(); //fails if layer is locked
storedLayer.locked = storedLayerLock;
}
targFrame.nextTextFrame = null;
if(fitToSelection) {
var selectionBounds = new Array();
//must account for right-hand page on spread
var offsetBounds = [0, 0, 0, 0];
if(app.activeWindow.activePage.side == PageSideOptions.RIGHT_HAND && applyTo == "all pages") {
offsetBounds = app.activeWindow.activePage.bounds;
}
selectionBounds[0] = coordTop.sort(sortNumbers).shift();
selectionBounds[1] = coordLeft.sort(sortNumbers).shift() + offsetBounds[3] - offsetBounds[1];
selectionBounds[2] = coordBottom.sort(sortNumbers).pop();
selectionBounds[3] = coordRight.sort(sortNumbers).pop();
targFrame.geometricBounds = selectionBounds;
}
if(fitToContent) {
targFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
}
function sortNumbers(a,b){
return a-b;
}
function sortFields(a,b) {
var tProp = sortProp;
var aN = getCoordinateValue(a, tProp);
var bN = getCoordinateValue(b, tProp);
var returnVal = aN - bN;
if(returnVal == 0) {
//second level sorting
if(tProp == "top" || tProp == "bottom") tProp = "left";
else if(tProp == "left" || tProp == "right") tProp = "top";
aN = getCoordinateValue(a, tProp);
bN = getCoordinateValue(b, tProp);
returnVal = aN - bN;
}
if(tProp == "right" || tProp == "bottom") returnVal *= -1; //reverse
return returnVal;
}
function getCoordinateValue(obj, type) {
var returnVal;
var bounds = obj.geometricBounds;
switch(type){
case "left":
returnVal = bounds[1];
break;
case "top":
returnVal = bounds[0];
break;
case "right":
returnVal = bounds[3];
break;
case "bottom":
returnVal = bounds[2];
break;
}
return returnVal;
}
function replace(searchStr, findStr, replaceStr){
var tArr = searchStr.split(findStr);
var returnStr = tArr.join(replaceStr);
return returnStr;
}
/*******************************************
* end code for Merging Text
********************************************/
//end file