-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMPO Importer.jsx
625 lines (457 loc) · 15.7 KB
/
MPO Importer.jsx
1
//// author fabiantheblind////// You must copy the file "glue code.jsx" from the XML Rules folder// (inside the Scripts folder inside your InDesign folder)// to the folder meta below the older containing this script,// or provide a full path to the file in the next line.// Copyright (C) 2011 Fabian "fabiantheblind" Morón Zirfas// http://www.the-moron.net// info [at] the - moron . net// This program is free software: you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation, either version 3 of the License, or// any later version.// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License// along with this program. If not, see <http://www.gnu.org/licenses/.#include "./meta/glue code.jsx";//#include "./meta/processXML.jsx";#include "./meta/processXML_v02.jsx";#include "./meta/setupStyles.jsx";var obj = new Object(); obj.mode; obj.xmlexist; obj.rmvxml; main();// ------------function myGetFileName() { var myFolder = app.activeDocument.filePath; var myFile = new File( myFolder+'/log.txt' ) // if ( myFile == null ){exit()}; return myFile; } function mapManually(myDoc){ //myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelNr"),myDoc.paragraphStyles.item("ARTIKEL_Nr")); myDoc.xmlImportMaps.add(myDoc.xmlTags.item("laufNummer"),myDoc.characterStyles.item("laufNummer")); myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelBezeichnungMitLN"),myDoc.paragraphStyles.item("ARTIKEL_Bezeichnung")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelBezeichnung"),myDoc.paragraphStyles.item("ARTIKEL_Bezeichnung")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelBeschreibung"),myDoc.paragraphStyles.item("ARTIKEL_Beschreibung")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelText"),myDoc.paragraphStyles.item("ARTIKEL_Text")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelText_Aufzaehlung"),myDoc.paragraphStyles.item("ARTIKEL_Text")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelZeileANBPmitLN"),myDoc.paragraphStyles.item("ARTIKEL_Zeile_ANBP_mit_LN")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelZeileANPmitLN"),myDoc.paragraphStyles.item("ARTIKEL_Zeile_ANP_mit_LN")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelZeileABPmitLN"),myDoc.paragraphStyles.item("ARTIKEL_Zeile_ABP_mit_LN")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelZeileANBPohneLN"),myDoc.paragraphStyles.item("ARTIKEL_Zeile_ANBP_ohne_LN")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelZeileANPohneLN"),myDoc.paragraphStyles.item("ARTIKEL_Zeile_ANP_ohne_LN")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("artikelZeileABPohneLN"),myDoc.paragraphStyles.item("ARTIKEL_Zeile_ABP_ohne_LN")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("laufNummerBild"),myDoc.paragraphStyles.item("LAUFNUMMER_am_Bild")) ; myDoc.xmlImportMaps.add(myDoc.xmlTags.item("preis"),myDoc.paragraphStyles.item("PREIS")) ; myDoc.mapXMLTagsToStyles();}function main(){ // Check for a document try{ var myDoc = app.activeDocument; }catch(e){ alert("No no no, you have no document.\nMaybe you should drink some coffee"); return; } var numOfItems; var myDate = new Date(); try{ var myLogFile = myGetFileName(); }catch(e){ alert("You need to save your docuemnt"); myDoc.save(); myDoc = app.activeDocument; var myLogFile = myGetFileName(); } var myFileContent = myLogFile.read(); var myErrorLog = myFileContent +"\n"+"Starting Log file at "+myDate +"\n"; var myItemsList = new Array; var myList; var myPageName; var myPage; with(myDoc){ viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS; viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS; viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN; } makeMetaStyle(myDoc);// var myParStyles = makeParstylesArray(myDoc);// var myCharStyles = makeCharstylesArray(myDoc); myList = myDoc.pages.everyItem().name; myErrorLog = myErrorLog + "Got these Pages: "+ myList.toString() + "\n"; // set the XMLImport preferences prcs_xmlImportPref(myDoc); var myAppendDialoge = app.dialogs.add({name:"Import Modes", canCancel:true}); with(myAppendDialoge){ //Add a dialog column. with(dialogColumns.add()){ with(dialogRows.add()){ var modeDropdown = dropdowns.add({ stringList: ["Quick And Dirty","Advanced","Xml Import Only","Place all Prices"], selectedIndex:0,minWidth:75 }); } // end of first row with(dialogRows.add()){ var xmlExistChkBx = checkboxControls.add({checkedState:false, staticLabel:"xml structure exists"}); } // end of second row with(dialogRows.add()){ var xmlRmvChkBx = checkboxControls.add({checkedState:true, staticLabel:"remove xml @ end"}); } // end of second row } // end of first column with(dialogColumns.add()){ var gutter = staticTexts.add({staticLabel:" ",minWidth:25}); } } // end of -->with(myAppendDialoge) //Display the fist dialog box. if(myAppendDialoge.show() == true){ // true means quick and dirty // false means advanced obj.mode = modeDropdown.selectedIndex; obj.xmlexist = xmlExistChkBx.checkedState; obj.rmvxml = xmlRmvChkBx.checkedState; myAppendDialoge.destroy(); // alert("DEBUG: "+obj.mode +" "+obj.xmlexist ); var myRoot; if(!obj.xmlexist){ // alert("DEBUG: xml does not exist call the import"); try { var myXMLFile = File.openDialog("Choose your .xml file"); myErrorLog = myErrorLog + "Got this xml file: "+ myXMLFile.displayName.toString() + "\n"; myRoot = myDoc.importXML(myXMLFile); } catch (e) { alert("ERROR: :( Sorry, your XML Document seems broken.\n" + e); myErrorLog = myErrorLog + e.toString() + "\n"; exit(); } try{ mapManually(myDoc); }catch(e){ alert("ERROR: :( Sorry, you don't have the right styles in your document\nit will look wired\n" + e); myErrorLog = myErrorLog + e.toString() + "\n"; }// Count all items in.// this isnt essential for the script numOfItems = prcs_countItems(myDoc); myErrorLog = myErrorLog + "Got "+ myList.toString() + " items in this xml \n"; try{ prcs_makeAttributesFromInfo(myDoc); }catch (e){ alert("ERROR: No i could not make your Attributes for processing the xml" + e); myErrorLog = myErrorLog + e.toString() + "\n"; exit(); } try{ prcs_sortGroups(myDoc); }catch(e){ alert("ERROR: Could not move Elements to group" + e); myErrorLog = myErrorLog + e.toString() + "\n"; exit(); } try{ // first we move the normal ones to the front than the focus small stays at end prcs_sortInGroupByPriority(myDoc); }catch(e){ alert("ERROR: Could not move the normal Elements in group" + e); myErrorLog = myErrorLog + e.toString() + "\n"; exit(); } try{ prcs_makeImgElement(myDoc); }catch (e){ alert("ERROR: Could not move the image Elements into new Element group" + e); myErrorLog = myErrorLog + e.toString() + "\n"; exit(); } try{ prcs_makeItemList(myDoc); }catch (e){ alert("ERROR: Could not make the list of items. the single import wont work" + e); myErrorLog = myErrorLog + e.toString() + "\n"; exit(); } // ------------------------- // From here on it does something on the page }// end of xmlimport into the doc //if it already exist the whole thing got skipped } else { // if the user pressed cancel // stop the whole thing myAppendDialoge.destroy(); return; } var myItemsListElement = myDoc.xmlElements.item(0).xmlElements.item("itemsList"); for (var i =0 ;i < myItemsListElement.xmlElements.length;i++){ myItemsList[i] = myItemsListElement.xmlElements.item(i).markupTag.name; } myUI(myDoc, myPage,myPageName , myList,myItemsList,myErrorLog,myLogFile,obj.mode); }function myUI(myDoc, myPage,myPageName, myList,myItemsList, myErrorLog, myLogFile, mode){ var myNumOItems = 0; var minW = 400; var myDialog = app.dialogs.add({name:"XMLImporter", canCancel:true,minWidth:minW}); with(myDialog){ //Add a dialog column. with(dialogColumns.add()){ //Create a border panel. var allSelector = enablingGroups.add({ staticLabel: "place all and remove", checkedState: true, minWidth: minW }); with (allSelector) { var myPlaceAll = checkboxControls.add({ staticLabel: "place every item!", checkedState: true }); var myClearStructureCheckbox = checkboxControls.add({ staticLabel: "remove all xml elements after import?", checkedState: true }); } var pageSelector = enablingGroups.add({ staticLabel: "choose page", checkedState: false, minWidth: minW }); 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 groupSelector = enablingGroups.add({staticLabel: "place group", checkedState: false,minWidth :minW}); with (groupSelector) { with (dialogColumns.add()) { staticTexts.add({ staticLabel: "chose group to place" }); } with (dialogColumns.add()) { //Create a pop-up menu ("dropdown") control. var myGroupDropdown = dropdowns.add({ stringList: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], selectedIndex: 0 }); } staticTexts.add({ staticLabel: "choose scope:" }); var myRadioButtonGroup = radiobuttonGroups.add(); with (myRadioButtonGroup) { var myLeftRadioButton = radiobuttonControls.add({ staticLabel: "focus", checkedState: true }); var myCenterRadioButton = radiobuttonControls.add({ staticLabel: "normal" }); var myRightRadioButton = radiobuttonControls.add({ staticLabel: "small" }); } } var itemSelector = enablingGroups.add({staticLabel: "place single item", checkedState: false,minWidth :minW}); 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 tbAndPrSelector = enablingGroups.add({staticLabel: "tables or prices", checkedState: false,minWidth :minW}); with(tbAndPrSelector){ with(dialogColumns.add()){ with(dialogRows.add()){ var myPlaceTablesCheckbox = checkboxControls.add({ staticLabel: "place all tables on selected page?"// checkedState: true }); } with(dialogRows.add()){ var myPlacePricesCheckbox = checkboxControls.add({ staticLabel: "add all Prices from xml structure?"// checkedState: true }); } with(dialogRows.add()){ var myPlaceTablesExitCheckbox = checkboxControls.add({ staticLabel: "end after placing the tables or prices?", checkedState: true }); } } } } //Display the dialog box. if(obj.mode == 1){ if(myDialog.show() == true){ // var myPage;// var myPageName;// page selector box if(pageSelector.checkedState!=true ){ myPage = myDoc.pages.add(); myPageName = myPage.name; }else {// myPage = myList[myPageDropdown.selectedIndex]; myPageName = myList[myPageDropdown.selectedIndex]; } if(tbAndPrSelector.checkedState == true && myPlacePricesCheckbox.checkedState==true){// myPage = myDoc.pages.add();// myPageName = myPage.name; } if(tbAndPrSelector.checkedState == true){ var placeTables; if(myPlaceTablesCheckbox.checkedState==true){ placeTables = true; placePrices = false; }else{ placeTables = false; } var placePrices; if(myPlacePricesCheckbox.checkedState==true){ placePrices = true; placeTables = false; }else{ placePrices = false; } var exitAfterTables; if(myPlaceTablesExitCheckbox.checkedState==true){ exitAfterTables = true; }else{ exitAfterTables = false; } } if(placeTables ==true ){ var myNewPage = myDoc.pages.item(myPageName); for(var i = 0; i <11;i++){ prcs_placeAllTables(myDoc,myNewPage,i); } if(exitAfterTables == true){ exit(); } } if(placePrices ==true ){ prcs_placeAllPrices(myDoc); if(exitAfterTables == true){ exit(); } } var placeAllBool; var focusBool; var normalBool; var smallBool; var itemBool; var groupSelector; if(allSelector.checkedState !=true){ placeAllBool = false; clearStructure = false; } if((myPlaceAll.checkedState == true) && (allSelector.checkedState ==true)){ placeAllBool = true; focusBool = false; normalBool = false; smallBool = false; itemBool = false;// groupSelector = 0; } else{ if(groupSelector.checkedState = true){ groupSelector = myGroupDropdown.selectedIndex; placeAllBool = false; if(myRadioButtonGroup.selectedButton==0){ focusBool = true; normalBool = false; smallBool = false; itemBool = false; }else if(myRadioButtonGroup.selectedButton==1){ focusBool = false; normalBool = true; smallBool = false; itemBool = false; }else if(myRadioButtonGroup.selectedButton==2){ focusBool = false; normalBool = false; smallBool = true; itemBool = false; } } var theItem; if(itemSelector.checkedState == true){ placeAllBool = false; focusBool = false; normalBool = false; smallBool = false; itemBool = true; var preTheItem = myItemsList[myArtikelDropdown.selectedIndex]; var theItem = preTheItem.substring(4); } } var clearStructure; if((myClearStructureCheckbox.checkedState==true) && (allSelector.checkedState ==true)){ clearStructure = true; }else{ clearStructure = false; } myDialog.destroy(); myPage = myDoc.pages.item(myPageName); // call function placeData // in processXML.jsx prcsXMLv02_placeData(myDoc, myPage, groupSelector, placeAllBool, focusBool, normalBool, smallBool,itemBool,theItem,myErrorLog,myLogFile); if(clearStructure ==true ){ myDoc.xmlElements.everyItem().remove(); } }else{ myDialog.destroy(); var myFile = myLogFile; var myData = myErrorLog; prcs_writeData (myFile, myData ); alert("DEBUG: all that thinking for nothing? Better luck nexttime!"); } // end of dialog show } else if (obj.mode == 0){ myPage = myDoc.pages.add(); myPageName = myPage.name; placeAllBool = true; focusBool = false; normalBool = false; smallBool = false; itemBool = false; theItem = null; // call function placeData // in processXML.jsx prcsXMLv02_placeData(myDoc, myPage, groupSelector, placeAllBool, focusBool, normalBool, smallBool,itemBool,theItem,myErrorLog,myLogFile); if(obj.rmvxml){ myDoc.xmlElements.everyItem().remove(); } // end of --> else if (obj.mode == 0) } else if (obj.mode == 2){ alert("Your import is done"); }else if (obj.mode == 3){ prcs_placeAllPrices(myDoc); if(obj.rmvxml){ myDoc.xmlElements.everyItem().remove(); } exit(); }; } }// end of myUI method//---------- functions taken from RecordFindChange_CS3-CS5.jsx