1 Reply Latest reply on Jan 27, 2011 4:22 AM by Green4ever

    (JS) Locating the current parentTextFrame from the selection?

    Green4ever Level 3

      Hi Everyone,

       

      In my xml workflow, I will find the xml element (<fig></fig>) through xpath (using glue code.jsx) and placing the figures with calculated bounds.

       

      First I'm selecting the text(xml element) and placing first image & and moving the xml element perfectly, problem occurs while the placing the second

      image and caption. I cannot locate the current parentTextFrame, previous parentTextFrame is acting as the current parentTextFrame.

       

      Help me to acheive this....

       

      Here is my full code and see bottom of this message i have pointed out where i need help ......

      #include "D:/Program Files/Adobe/Adobe InDesign CS4/Scripts/XML Rules/glue code.jsx"
      //==================================================================
      //For placement of Figures
      //
      //
      //==================================================================
      myDoc = app.activeDocument;
      var myCount = 0;
      var myFolder = Folder.selectDialog("Select image folder");
      var figcapdis=prompt("Enter the space between Figure and Figure Caption (pts)", 8, "Figure Caption");
      var figcap=parseInt(figcapdis);
      
                var myRuleSet = new Array (new FindByAttribute("//fig"));
                with(myDoc){
                var elements = xmlElements;
                __processRuleSet(elements.item(0), myRuleSet);
                     if(myCount > 0){
                          myCount = 0;
                          }//end if
                     }//end with
      
      //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      function FindByAttribute(myXPath){
           this.name = "FindByAttribute";
           //XPath will match on every description in the XML structure.
           this.xpath = myXPath;
           this.apply = function(myElement, myRuleProcessor){
                var selectPoint = myElement.texts.item(0).select(SelectionOptions.replaceWith);          
                var tFs = app.selection[0].parentTextFrames[0];
                var myFrameBounds = tFs.geometricBounds;
                var myWidth = (myFrameBounds[3]-myFrameBounds[1]); 
                var myHeight = (myFrameBounds[2]-myFrameBounds[0]);
                var mySelectionHOffset = myDoc.selection[0].lines[0].horizontalOffset;
                var mySelectionBaseline = myDoc.selection[0].lines[0].baseline;
                var myCurrentPage=tFs.parent;
                app.activeWindow.activePage = myCurrentPage;
                var figName = myElement.xmlElements.item('graphic').xmlAttributes.item('xlink:href').value;
                var figType = myElement.xmlElements.item('graphic').xmlAttributes.item('mime-subtype').value;
                          
                var myFigPath = myFolder+"/"+figName+"."+figType;
                if (File(myFigPath).exists)
                {
                     alert("Image exists");
                     var myFig = myCurrentPage.place(new File(myFigPath));
                     var myFigBounds = myFig[0].geometricBounds;
                     var figHeight = (myFigBounds[2]-myFigBounds[0]);
                     var figwidth = (myFigBounds[3]-myFigBounds[1]);
                     var myParent = myFig[0].parent;
                     var myFreeSpace = myHeight-mySelectionBaseline;
                     var myBounds = [myFigBounds[2]+figcap,0,myFigBounds[2]+25,myWidth];
                     var myCaptionFrame = myCurrentPage.textFrames.add({geometricBounds:myBounds});
                     myCaptionFrame.placeXML(myElement);
                     myCaptionFrame.fit(FitOptions.frameToContent);
                     var item1 = myParent;
                     var item2 = myCaptionFrame;
                     var myGroup = myCurrentPage.groups.add([item1, item2]);
                     myGroup.label = figName;
                     var myGroupBound = myGroup.geometricBounds;
                     var myGroupHeight = (myGroupBound[2]-myGroupBound[0]);
                          if(figHeight<myFreeSpace)
                          {
                               var movePoint = [mySelectionHOffset, mySelectionBaseline+12];
                               //var movePoint = [mySelectionBaseline+24,mySelectionHOffset, mySelectionBaseline+16+figHeight,mySelectionHOffset+figwidth];
                               myGroup.move(movePoint);
                               //myFig[0].geometricBounds = setFigBounds;
                               }//end of figHegiht  less than Free space
                          else
                          {
                               if (myCurrentPage == myDoc.pages[-1]){
                                    myDoc.pages.add();
                                    myCurrentPage = myDoc.pages.nextItem(myCurrentPage);
                                    }
                               myCurrentPage = myDoc.pages.nextItem(myCurrentPage);
                               var nextTF = tFs.nextTextFrame;
                               var nextTFBounds = nextTF.geometricBounds;
                               myGroup.move(myCurrentPage, [nextTFBounds[1], nextTFBounds[0]]);
                               }//end of else
                     myGroup.textWrapPreferences.textWrapMode=TextWrapModes.BOUNDING_BOX_TEXT_WRAP;
                     myGroup.textWrapPreferences.textWrapOffset=[24, 6, 24, 6];
                     }//end of if -->file exists check
                else 
                {
                     alert(figName+"."+figType+" doest not exists");
                     }
                //var myFigGroup = 
              //var myParaStyle = app.documents.item(0).paragraphStyles.item(newStyle);
              //myElement.texts.item(0).applyParagraphStyle(myParaStyle, true);
              myCount++;
              }//end of this.apply
           return true;
      }
      

      I am having problem in the below line

      var tFs = app.selection[0].parentTextFrames[0];

      I want the parent text frame of the current selection, but it takes the first selection's parent text Frame.

      Note: The text frames are in continuous thread...

      Looking forward your replies....    

       

      Message was edited by: Green4ever