This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReplace-Dummies.jsx
295 lines (244 loc) · 8.35 KB
/
Replace-Dummies.jsx
1
(function (thisObj) { /** * This is a workaround to get hte job done until * JM-Kat 2014 * @todo fix bug not workaound * @param {Document} doc the current doc * @return {nothing} */ // var docpath = doc.filePath; /*written by fabiantheblind 4 JM-2011*/ /** * own include 2013 replacing JSXses include. Does not work well with a linter * #include "./meta/glue code.jsx"; */ var script_file = File($.fileName); // get the location of the scriptfile var script_file_folder = script_file.parent; var script_file_path = script_file.path; // get the path var file_to_read = File(script_file.path + "/meta/glue code.jsx"); var content = ""; // alert(file_to_read); if (file_to_read.exists) { // if it is really there file_to_read.open('r'); // open it content = file_to_read.read(); // read it eval(content); } else { alert("The file 'glue code.jsx' is missing"); return; } var dummies_data = { "DEBUG": false, "WARNINGS": true, "alldummies": [], "doc": null }; main(); function main() { var d; // test for a doc try { d = app.activeDocument; dummies_data.doc = d; } catch (e) { if (dummies_data.WARNINGS) alert( "No no no, you have no document.\nMaybe you should drink some coffee" ); return; } get_dummies(); // alert(dummies_data.alldummies); var myItemsList = []; var myItemsListElement; try { myItemsListElement = d.xmlElements.item(0).xmlElements.item("itemsList"); } catch (e) { if (dummies_data.WARNINGS) alert( "you have no xml structure.\nuse the MPO_Importer"); return; } var progress_win = new Window ("palette"); var progress = progress_bar(progress_win, myItemsListElement.xmlElements.length -1, 'Replacing dummies'); for (var i = 0; i < myItemsListElement.xmlElements.length; i++) { myItemsList[i] = myItemsListElement.xmlElements.item(i).markupTag.name; } for (var j = 0; j < myItemsList.length; j++) { var artnr = myItemsList[j].substring(4); placeData(d, artnr); progress.value = j; } progress.parent.close();// important close it!! } function placeData(d, theItem) { var myRuleSet = new Array(new placeImages(theItem, d)); var elements = d.xmlElements; __processRuleSet(elements.everyItem(), myRuleSet); } function placeImages(theItem, 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"); if (dummies_data.DEBUG) alert("myImages length: " + myImages.xmlElements .length); if (!myImages.xmlElements.length) { // if(dummies_data.WARNINGS) alert("Your Article has no referenced images"); return; } for (var i = 0; i < dummies_data.alldummies.length; i++) { var dummy = dummies_data.alldummies[i]; if (strcmp(theItem, dummy.artnr) === 0) { var myString = myImages.xmlElements.item(0).xmlAttributes.item(1).value; try{ dummy.rectangle.place(File(d.filePath + "/" + myString)); }catch(e){ if(dummies_data.WARNINGS) alert("The image '" + myString +"' odes not exist.\nDid you download the right image package from the MPO?"); continue; } dummy.rectangle.fit(FitOptions.CENTER_CONTENT); dummy.rectangle.fit(FitOptions.PROPORTIONALLY); dummy.rectangle.label = ''; // dummy.rectangle.fit(FitOptions.FRAME_TO_CONTENT); } } }; } function get_dummies() { // var alldummies = []; /** * loop all items * */ // alert(docpath); var doc = dummies_data.doc; if (doc === null) { if (dummies_data.WARNINGS) alert( "You have no doc (in function get_dummies())"); return; } var progress_win = new Window ("palette"); var progress = progress_bar(progress_win, doc.allPageItems.length, 'Searching document for dummies'); for (var i = 0; i < doc.allPageItems.length; i++) { progress.parent.children[0].text = 'Searching document for dummies'; progress.value = i+1; var one_item = doc.allPageItems[i]; // isolate if (one_item instanceof Rectangle) { /* check wheather we have a rectangle*/ var rect = one_item; // for clearance var label_str = rect.label; // extract label /** * We are expecting an json object with this structure * { * importid: 23344455556, * artnr : "J01233445", * type: "image", * imagedata : { * path:"", * folder:"" * } * } * */ // alert(label_str); // eval(label_str); // var res = confirm(label_str); // if (res === false){ // return; // } // var reg = new RegExp("^\({"); // if(/^\(\{uuid/.test( label_str )){ var item_info = null; try { item_info = eval(label_str); // this should be an json object } catch (e) { if (e instanceof SyntaxError) { if (dummies_data.DEBUG) alert("Syntax error" + e.message); continue; } else if (e instanceof URIError) { if (dummies_data.DEBUG) alert("URi error" + e.message); continue; } else if (e instanceof RangeError) { if (dummies_data.DEBUG) alert("Range error" + e.message); continue; } else if (e instanceof ReferenceError) { if (dummies_data.DEBUG) alert("Reference error" + e.message); continue; } else if (e instanceof TypeError) { if (dummies_data.DEBUG) alert("Type error" + e.message); continue; } else if (e instanceof EvalError) { if (dummies_data.DEBUG) alert("Eval error" + e.message); rect.label = ""; continue; } else { alert("Error " + e.message); continue; } } if ((item_info !== undefined) && (item_info !== null)) { if (item_info.hasOwnProperty("type")) { if (item_info.type == "dummy") { progress.parent.children[0].text = "found one!"; if (item_info.hasOwnProperty("artnr")) { dummies_data.alldummies.push({ "rectangle": rect, "artnr": item_info.artnr.substring(8) }); } else { /* if not hasOwnProperty artnr*/ continue; } } else { /* if not a dummy */ continue; } } else { /* if not hasOwnProperty type */ continue; } } else { /* if item_info undefined or null */ continue; } } else { /* if not a rectangle */ continue; } } // end if I loop allPageItems progress.parent.close(); } /** * found here http://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp * @param {String} str1 [description] * @param {String} str2 [description] * @return {Number} 0 if true */ function strcmp(str1, str2) { // http://kevin.vanzonneveld.net // + original by: Waldo Malqui Silva // + input by: Steve Hilder // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + revised by: gorthaur // * example 1: strcmp( 'waldo', 'owald' ); // * returns 1: 1 // * example 2: strcmp( 'owald', 'waldo' ); // * returns 2: -1 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1)); }/** * Taken from ScriptUI by Peter Kahrel * @usage * var progress_win = new Window ("palette"); * * var progress = progress_bar(progress_win, 100, 'my Progress'); * * progress.value = i++; * progress.parent.close();// important close it!! * * @param {Palette} w the palette the progress is shown on * @param {Number} stop [description] * @return {Progressbar} [description] */function progress_bar (w, stop, labeltext) {var txt = w.add('statictext',undefined,labeltext);var pbar = w.add ("progressbar", undefined, 1, stop); pbar.preferredSize = [300,20];w.show ();return pbar;}})(this);