0 Replies Latest reply on May 7, 2014 12:30 PM by schiuma24

    xml not going to the right frame

    schiuma24 Level 1

      Here's the deal, Im trying to automate a layout.

       

      I have managed to:

       

      • Create the right number of pages
      • Create text frames
      • Populate text frames

       

      The populate text frame workflow is the following:


      Create a text frame with a script label to later match it with an xml attribute

      For example <myXMLelement output class = "sidebar"> would go into a text frame with a sidebar_content script label.

       

      The Problem:

       

      I have several "sidebar" elements on a page and I created several tex frames with its corresponding label for them,

      and all the XML elements get into the layout....BUT ON THE SAME TEXT FRAME!!!! $%@#%^&@

      the first "sidebar" gets into de first frame and then gets replaced by the next "sidebar" and so on.

       

       

      here is the complete code (the red part is where I think the problem is):

       

       

      main();

       

       

      function main(){

       

       

      ///////////TEMPLATE SELECTION DIAGLOG

      var myWorkFolder = Folder("/Applications/Adobe InDesign CS5.5/Scripts/Scripts Panel/Samples/JavaScript"); // working folder 

      var showWorkFolder = Boolean(true); // shows(true)/hides(false) working folder in dialog 

      var myPath = Folder("/Applications/Adobe InDesign CS5.5/Scripts/Scripts Panel/Samples/JavaScript");

      var strWinTitle = String("Library opener"); 

      var strWorkFolder = String("Working folder:"); 

      var strSelectLib = String("Select Template:"); 

      var strNoFiles = String("-- No libraries found --"); // shows if no libraries found 

       

      ///////////////////////// SCRIPT ///////////////////////// 

       

      var myFiles = myWorkFolder.getFiles("*.indt"); 

      var myFilesNo = myFiles.length; 

      var myLibFiles = Array(); 

       

      if(!myFilesNo){ 

           myLibFiles[0] = strNoFiles; 

      }else{ 

           for(var i=0; i < myFilesNo; i++){ 

                myLibFiles.push(myFiles[i].displayName.substr(0,myFiles[i].displayName.lastIndexOf("."))) ; 

           } 

       

      var myDialog = app.dialogs.add({name:strWinTitle, canCancel:true}); 

           with(myDialog){ 

                with(dialogColumns.add()){ 

                     if(showWorkFolder){ 

                          with(dialogRows.add()){ 

                               with(borderPanels.add()){ 

                                     

                               } 

                          } 

                     } 

                     with(dialogRows.add()){ 

                          with(borderPanels.add()){ 

                               with(dialogColumns.add()){ 

                                    staticTexts.add({staticLabel:strSelectLib}); 

                               } 

                               with(dialogColumns.add()){ 

                                    var myLib = dropdowns.add({stringList:myLibFiles, selectedIndex:0}); 

                               } 

                          } 

                     } 

                } 

           } 

       

      if(myDialog.show() == true){ 

           if(myFilesNo){ 

                app.open(File(myFiles[myLib.selectedIndex]),true); 

           } 

       

      myDialog.destroy();

      ///////END OF DIALOG////////////////////////////////////////////////

       

       

      var myDoc = app.activeDocument;

      /////opes xml file/////////////////////////////////

      myDoc.importXML(File.openDialog());

      ////////////////////////////////////////////////

        var fileSep = "/";

       

      //Where fileSep is "/" you can change it though if you are on windows.

       

       

      if(File.fs=="Windows")

      {

           fileSep = "\\";

      }

      if (myDoc.pages.length != 1 )

          {

            alert("This script must be run on an empty doc");

          exit();

      }

          //discover how many pages

        var xml = myDoc.xmlElements[0];

      //////*****MAKES XPATH HAPPY AND COOPERATIVE*****////////////////////////////////////

      if( !xml.xmlAttributes.itemByName("xmlns:xml").isValid )

      xml.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" );

      if( !xml.xmlAttributes.itemByName("xmlns:aid").isValid )

      xml.xmlAttributes.add( "xmlns:aid", "http://ns.adobe.com/Adobe InDesign/4.0/" );

      if( !xml.xmlAttributes.itemByName("xmlns:aid5").isValid )

      xml.xmlAttributes.add( "xmlns:aid", "http://ns.adobe.com/Adobe InDesign/5.0/" );

      ////////////////////////////////////////////////////////////////////////////////////////// ///   

       

        var ditas= xml.evaluateXPathExpression("//page[string-length(@id) > 1]/ancestor::dita");

        var maxPage =0;

        var minPage = ditas[0].xmlElements.item("learningBECContent").xmlElements.item("learningBECContentbody" ).xmlElements.item("page").xmlAttributes.itemByName("id").value.replace("p","").replace(" ","");

        myDoc.documentPreferences.startPageNumber  = parseInt(minPage);

       

       

           for(x=1;x < ditas.length; x++)

      {

       

       

       

       

      try{

       

       

      var page = ditas[x].xmlElements.item("learningBECContent").xmlElements.item("learningBECContentbody" ).xmlElements.item("page").xmlAttributes.itemByName("id").value.replace("p","").replace(" ","");

      //alert(page);

      maxPage = Math.max(minPage,maxPage);

      }

      catch(e){

          page = "dita "+ x + " no page specified";

        }

       

       

       

       

      }

      $.writeln(maxPage + " Max page in this xmlfile");

       

       

        // add that many pages

      for (x=minPage; x <= maxPage;x++){

          if(x==1)

              myPage = myDoc.pages[0];

              else

              myPage = myDoc.pages.add();

              }

       

       

          ////////ADDS TEXT FRAMES TO A.-Master (default master spread)                       

                             var myDoc = app.activeDocument;

                             //var myMasterPage = myDoc.masterSpreads.itemByName("A-Master");

                             var myMainBodyRight = myDoc.textFrames.add();

                             var mySideBarRight1 = myDoc.textFrames.add();

                             var mySideBarRight2 = myDoc.textFrames.add();

                             var mySideBarRight3 = myDoc.textFrames.add();

                             var myMainBodyLeft = myDoc.textFrames.add();

                             var mySideBarLeft1 = myDoc.textFrames.add();

                             var mySideBarLeft2 = myDoc.textFrames.add();

                             var mySideBarLeft3 = myDoc.textFrames.add();

       

       

                           

       

        ///////////FRAMES PARAMETERS////////////////////////////////////////////////////////////////

                         

                             ///////////RIGHT SIDE//////////////////////////

                             myMainBodyRight.geometricBounds = ["0.4224 in", "3.5066 in", "10.985 in", "8.506 in"];

        mySideBarRight1.geometricBounds = ["0.4224 in", "15.34 in", "2.93 in", "18 in"];

        mySideBarRight2.geometricBounds = ["3.09 in", "15.34 in", "5.6 in", "18 in"];

        mySideBarRight3.geometricBounds = ["5.77 in", "15.34 in", "8.2 in", "18 in"];

                            myMainBodyRight.label = "body_content";

        mySideBarRight1.label = "sidebar_content";

                             mySideBarRight2.label = "sidebar_content";

        mySideBarRight3.label = "sidebar_content";

       

       

                     

                             ///////////LEFT SIDE///////////////////////////

                            myMainBodyLeft.geometricBounds = ["0.4224 in", "10 in", "10.9 in", "15 in"];

        mySideBarLeft1.geometricBounds = ["0.4224 in", ".5 in", "2.93 in", "3.5 in"];

        mySideBarLeft2.geometricBounds = ["3.09 in", ".5 in", "5.6 in", "3.5 in"];

        mySideBarLeft3.geometricBounds = ["5.77 in", ".5 in", "8.2 in", "3.5 in"];

                            myMainBodyLeft.label = "body_content";

        mySideBarLeft1.label = "sidebar_content";

                             mySideBarLeft2.label = "sidebar_content";

        mySideBarLeft3.label = "sidebar_content";

       

       

       

       

                            /////////GET THE XML IN//////////////////////////////////////////////////////////////////

           

                                              var allTextFrames = toArray(myDoc.textFrames);

                                              var bodyTextFrames = selectWhere("body_content", "label", allTextFrames);

                                              var sidebarTextFrames = selectWhere("sidebar_content", "label", allTextFrames);

                                                 

                                              

                                              var xml = myDoc.xmlElements[0];

       

       

                                              var bodyDitas= xml.evaluateXPathExpression("//dita[not(@outputclass='sidebar')]");

                                              var sidebarDitas= xml.evaluateXPathExpression("//dita[@outputclass='sidebar']");

       

       

       

       

       

       

                                              $.writeln( ditas.length + " ditas");

                                              $.writeln( bodyDitas.length + " body ditas");

                                              $.writeln( sidebarDitas.length + " sidebar ditas");

                                              $.writeln( bodyTextFrames.length + " body_content textframes in this doc");

                                              $.writeln( sidebarTextFrames.length + " sidebar_content textframes in this doc");

       

       

                                              //Fill TextFrames on page[//dita//page/@id] with ditas based on //dita/@outputclass until you run out of frames or content

                                              for(x=0;x < Math.min(allTextFrames.length, ditas.length) ;x++)

                                              {

                                                

       

       

                                              try{

                                                  var type= ditas[x].xmlAttributes.itemByName("outputclass").value;

                                                  var page = ditas[x].xmlElements.item("learningBECContent").xmlElements.item("learningBECContentbody" ).xmlElements.item("page").xmlAttributes.itemByName("id").value.replace("p","").replace(" ","");

                                              }

                                              catch(e){

                                                  page = "1";

                                                  }

                                              $.writeln("proc page "+ page + " as "+ type);

                                              try{

                                              if(type == "sidebar")

                                                  {

                                                    var pageBodyTextFrames = selectWhere("sidebar_content", "label", myDoc.pages[page-parseInt(minPage)].textFrames); 

                                                     

                                                      }

                                              else

                                                  {

                                              var pageBodyTextFrames = selectWhere("body_content", "label", myDoc.pages[page-parseInt(minPage)].textFrames);

                                                  }

                                              var newTextFrame = pageBodyTextFrames[0];

                                              ditas[x].placeXML(newTextFrame);

                                              }

                                              catch(e){

                                                  $.writeln("Error on page "+page+ " discarding");

                                                  $.writeln(e.message);

                                                  }

       

       

       

       

                                                  }

                                             

                                              //app.doScript(new File(app.activeScript.parent.fsName+fileSep+"FindChangeByList.jsx"), ScriptLanguage.javascript);

       

       

                                                  }

       

       

                                              function addThreadedTextFrame(page, frame){

                                                 

                                              var threadTextFrame = page.textFrames.add({geometricBounds:[0, 0, 1 + ' in', 1 + ' in']});

                                              threadTextFrame.move( [20, 0 ] );

                                              frame.nextTextFrame = threadTextFrame;

                                              return threadTextFrame;

                                              }

       

       

                                              function selectWhere(value, key, array){

                                                  var i = array.length; var t; var filtered = [];

                                                  while(i--){

                                                          t = array[i];

                                                          if(t && t[key] == value){

                                                              filtered.push(t);

                                                          }

                                                  }

                                                  return filtered;

                                              }

       

       

                                              function toArray(objects){

                                                  var i = objects.length; var array = [];

                                                  while(i--){

                                                          array.push(objects[i]);

                                                  }

                                                  return array;

                                              }

       

       

                                              function countTheseElements(xmlfrag, xpath){

                                              var c = 0;

                                              for(x=0; x < xmlfrag.length;x++)

                                                  {

                                                      $.writeln(xmlfrag[x].xmlElement[0].length);

                                                     

                                                      }

       

       

       

       

       

       

                                                        

                              }