-
Notifications
You must be signed in to change notification settings - Fork 1
/
Place All Images.jsx
339 lines (240 loc) · 7.48 KB
/
Place All Images.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
/*
written by fabiantheblind 4 JM-2011
*/
#include "./meta/glue code.jsx";
var obj = new Object();
obj.DEBUG = false;
obj.WARNINGS = true;
main();
function main() {
var d;
// test for a doc
try { var d = app.activeDocument; } catch(e){
if(obj.WARNINGS) alert("No no no, you have no document.\nMaybe you should drink some coffee");
return;
}
var myItemsList = new Array;
var myList;
var myPageName;
var myPage;
myList = d.pages.everyItem().name;
try{
var myItemsListElement = d.xmlElements.item(0).xmlElements.item("itemsList");
}catch(e){
if(obj.WARNINGS) alert("you have no xml structure.\nuse the MPO_Importer");
return;
}
try{
for (var i =0 ;i < myItemsListElement.xmlElements.length;i++){
myItemsList[i] = myItemsListElement.xmlElements.item(i).markupTag.name;
}
}catch(e){
if(obj.WARNINGS) alert("you have no xml structure.\nuse the MPO_Importer");
return;
}
myUI(d, myPage,myPageName, myList,myItemsList);
}
function set_label(obj,str){
try{
obj.label = str;
}catch(e){
}
}
function myGetColumns(dument, myPage){
var myPageWidth = dument.documentPreferences.pageWidth;
var myPageHeight = dument.documentPreferences.pageHeight
var myPageColumnCount= myPage.marginPreferences.columnCount;
var myPageColumnGutterWidth= myPage.marginPreferences.columnGutter;
var myX1 = myPage.marginPreferences.left;
var myY1 = myPage.marginPreferences.top;
var myX2 = myX1
+((myPageWidth-myPage.marginPreferences.left
-myPage.marginPreferences.right
-(myPageColumnGutterWidth* (myPageColumnCount-1))) /myPageColumnCount);
var myY2 = myPageHeight - myPage.marginPreferences.bottom;
return [myY1, myX1, myY2, myX2];
}
function myGetBounds(dument, myPage){
var myPageWidth = dument.documentPreferences.pageWidth;
var myPageHeight = dument.documentPreferences.pageHeight
if(myPage.side == PageSideOptions.leftHand){
var myX2 = myPage.marginPreferences.left;
var myX1 = myPage.marginPreferences.right;
}
else{
var myX1 = myPage.marginPreferences.left;
var myX2 = myPage.marginPreferences.right;
}
var myY1 = myPage.marginPreferences.top;
var myX2 = myPageWidth - myX2;
var myY2 = myPageHeight - myPage.marginPreferences.bottom;
return [myY1, myX1, myY2, myX2];
}
function placeData(d , myPage , theItem){
var myRuleSet = new Array(new placeImages(theItem, myPage, d));
with(d){
var elements = xmlElements;
__processRuleSet(elements.everyItem(), myRuleSet);
}
}
function placeImages(theItem, myPage, d){
this.name = "placeImages";
this.xpath = "//artikel[@iArtikelNr='Art-Nr. "+theItem +"']";
this.apply = function(myElement, myRuleProcessor){
var myNullObjStyle = d.objectStyles.item(0);
var myImages = myElement.xmlElements.item("images");
var myGroup = new Array;
var myTempBounds = new Array;
myTempBounds = myGetColumns(d, myPage);
var myY1 = myTempBounds[0];
var myX1 = myTempBounds[1];
var myY2 = myTempBounds[2];
var myX2 = myTempBounds[3];
if(obj.DEBUG) alert(myImages.xmlElements.length);
if(!myImages.xmlElements.length){
if(obj.WARNINGS) alert("Your Article has no referenced images");
exit();
}
for (var i = 0; i<myImages.xmlElements.length ; i++){
var myImgFrame = myPage.rectangles.add();
set_label(myImgFrame, "Art-Nr. "+theItem);
//myImgFrame.appliedObjectStyle = d.objectStyles.item(0);
try{
myImgFrame.geometricBounds = [myY1+(i*10),myX1,myY2+(i*10),myX2];
var myString = myImages.xmlElements.item(i).xmlAttributes.item(1).value;
myImgFrame.place(File(checkOS(myString)));
myImgFrame.fit(FitOptions.CENTER_CONTENT);
myImgFrame.fit(FitOptions.PROPORTIONALLY);
myImgFrame.fit(FitOptions.FRAME_TO_CONTENT);
myImgFrame.applyObjectStyle(myNullObjStyle);
myGroup.push(myImgFrame);
}catch(e){
// alert("WARNING! \r THERE IS AN IMAGE MISSING! " +e );
myImgFrame.geometricBounds = [myY1+(i*10),myX1,myY2+(i*10),myX2];
myImgFrame.fillColor = d.swatches.item(2);
myImgFrame.fillTint = 42;
myGroup.push(myImgFrame);
}
}
var myBG = myPage.rectangles.add();
set_label(myBG, "Delete me");
with(myBG){
geometricBounds = [myY1,myX1,20,myX2];
fillColor = d.swatches.item(1);
applyObjectStyle(myNullObjStyle);
}
// myBG.fit(FitOptions.FRAME_TO_CONTENT);
// myUlFrame.appliedObjectStyle = d.objectStyles.item(0);
// myUlFrame.applyObjectStyle(myNullObjStyle);
var myTextFrame = myPage.textFrames.add();
set_label(myTextFrame, "Delete me");
myTextFrame.nonprinting = true;
with(myTextFrame){
geometricBounds = [myY1,myX1,myY2,myX2];
applyObjectStyle(myNullObjStyle);
if (myImages.xmlElements.length == 0) {
contents = theItem + " has no images";
}
else {
contents = theItem;
}
}
myTextFrame.paragraphs.everyItem().appliedParagraphStyle = d.paragraphStyles.item("ERROR");
myTextFrame.paragraphs.everyItem().characters.everyItem().appliedCharacterStyle = d.characterStyles.item("ERROR");
myTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
myGroup.push(myBG);
myGroup.push(myTextFrame);
myPage.groups.add(myGroup);
};
}
/*
* a function to check the operating system
*
* @returns true if the OS is windows
*/
function checkOS(myString){
var myOS = $.os;
var myOSSubString = myOS.charAt(0);
var myOSBoolean;
if (myOSSubString == "w" || myOSSubString == "W") {
myOSBoolean = true;
}
else {
myOSBoolean = false;
}
if (myOSBoolean == true) {
//this is for windows
var myHREFString = myString;
var myHREFSubString = myHREFString.substring(8);
return myHREFSubString;
} else {
// this is for macintosh
return myString;
}
}
function myUI(d, myPage,myPageName, myList,myItemsList){
var myNumOItems = 0;
var myDialog = app.dialogs.add({name:"PLACE ALL IMAGES", canCancel:true});
with(myDialog){
//Add a dialog column.
with(dialogColumns.add()){
//Create a border panel.
var itemSelector = enablingGroups.add({staticLabel: "place item ",
checkedState: true,minWidth :250});
with(itemSelector){
with(dialogColumns.add()){
//Create a pop-up menu ("dropdown") control.
var myArtikelDropdown = dropdowns.add({
stringList: myItemsList
//selectedIndex:0
});
}
}
//Create another border panel.
var pageSelector = enablingGroups.add({
staticLabel: "choose page",
checkedState: true,
});
with (pageSelector) {
with (dialogColumns.add()) {
staticTexts.add({
staticLabel: "chose page to place"
});
}
with (dialogColumns.add()) {
//Create a pop-up menu ("dropdown") control.
var myPageDropdown = dropdowns.add({
stringList: myList,
selectedIndex: 0
});
}
// var myAddPage = checkboxControls.add({
// staticLabel: "or create a new page"
// // checkedState: true
// });
}
}
//Display the dialog box.
if(myDialog.show() == true){
// var myPage;
// var myPageName;
// page selector box
if(pageSelector.checkedState!=true ){
myPage = d.pages.add();
myPageName = myPage.name;
}else {
// myPage = myList[myPageDropdown.selectedIndex];
myPageName = myList[myPageDropdown.selectedIndex];
}
var preTheItem = myItemsList[myArtikelDropdown.selectedIndex];
if(obj.DEBUG) alert(preTheItem);
var theItem = preTheItem.substring(4);
myDialog.destroy();
myPage = d.pages.item(myPageName);
placeData(d,myPage,theItem);
}else{
myDialog.destroy();
if(obj.DEBUG) alert("all that thinking for nothing? Better luck nexttime!");
}
}
}