8 Replies Latest reply on May 29, 2013 11:57 PM by hurix@123

    Move xml element into Lib Item

    hurix@123 Level 1

      Hi,

       

      I'm currently trying to build a script to move a ELEMENT  into anchored objects with a specific object style.

       

      Here's my Script

       

      myXMLElements = myGetXMLElements("//div[@class='boxed-text']");

      ---

      ---

      ***

      ---

      myAnchorFrame = myLib.assets.item("Box").placeAsset(myElement.insertionPoints[0])[0];

      //    myAnchorFrame = myLib.assets.item("Box").placeAsset(myXMLElements[i].insertionPoints[0])[0];

       

       

          myElement.move(LocationOptions.after, myAnchorFrame.textFrames[0].insertionPoints[0]);

       

      ERROR: The element can not moved to the destination

       

      any idea.

       

      Hurix

        • 1. Re: Move xml element into Lib Item
          hurix@123 Level 1

          Any suggestion please

          • 2. Re: Move xml element into Lib Item
            Vamitul Level 4

            did you iterate your elements from the back?

            somwhere in your code i'm guessing there is something like:

            for (var i=0; i<myXMLElements.length;i++){

            bla bla

             

             

            change that to

            for (var i=myXMLElements.length-1; i>=0;i--){

            blah

             

             

            ps. if you mark your own reply again as correct, i'm not gonna answer any more of your questions.

            • 3. Re: Move xml element into Lib Item
              hurix@123 Level 1

              Hi,

               

              Same Error was occur while iterate my elements.

               

              i have a doubt, my Lib Asset was plasing in Inline. Whether I have to place display.

               

              Below is my code

               

              var myXMLElements = new Array;

              myXMLElements = myGetXMLElements("//div[@class='boxed-text']");

              if (myXMLElements.length > 0) {

              //if (myXMLElements.length > 0)

              //for(lp=0;lp<=myXMLElements.length-1; lp++){

              for(lp=myXMLElements.length-1; lp>=0; lp--){

                          var myElement = myXMLElements[lp];

                          alert(myElement.getElements());

                  myLib = app.libraries.item(0);

              myAnchorFrame = myLib.assets.item("Box").placeAsset(myElement.insertionPoints[0])[0];

              //    myAnchorFrame = myLib.assets.item("Box").placeAsset(myXMLElements[i].insertionPoints[0])[0];

               

               

                  myElement.move(LocationOptions.after, myAnchorFrame.textFrames[0].insertionPoints[0]);

                  myAnchorFrame.textFrames[0].parentStory.characters.item(-1).remove();

                  myAnchorFrame.appliedObjectStyle = myDoc.objectStyles.item("Box");

                  myAnchorFrame.fit(FitOptions.FRAME_TO_CONTENT);

               

              Hurix

              • 4. Re: Move xml element into Lib Item
                Vamitul Level 4

                ok.. now i see.

                let's say myElements is a collection of boxes, and your library item is a different kind of box (let's call it yellow Box).

                your code does this:

                takes a box (let's call it Blue Box) from the collection and puts the yellow box inside it. then it tries to move the blue box inside the yellow box. Guess now you can see why it won't work.

                 

                for it to work you should either:

                put the yellow box next to the blue box by getting the last insertion point BEFORE or the first insertionPoint AFTER myElement

                or

                duplicate the content of myElement inside the anchorFrame, then remove the original without the anchor marker.

                (second method not recomanded, especialy when working with xml).

                 

                anyway..

                replace this lines:

                myAnchorFrame = myLib.assets.item("Box").placeAsset(myElement.insertionPoints[0])[0];

                //    myAnchorFrame = myLib.assets.item("Box").placeAsset(myXMLElements[i].insertionPoints[ 0])[0];

                    myElement.move(LocationOptions.after, myAnchorFrame.textFrames[0].insertionPoints[0]);

                 

                 

                with this:

                 

                var myStory=myElement.parentStory;
                var firstIP=myElement.insertionPoints[0].index+1; // after inserting the anchor, the text shifts by one character
                var lastIP=myElement.insertionPoints[-1].index+1;
                
                var myAnchorFrame=myLib.assets.item("Box").placeAsset(myElement.insertionPoints[0])[0];
                myStory.insertionPoints.itemByRange(firstIP,lastIP).move(LocationOptions.after, myAnchorFrame.textFrames[0].insertionPoints[0]);
                

                 

                debug as needed (it's 9 in the morning here, haven't had my coffee yet, coding without it should be considered an act of terrorism, so beware of bugs,mistypes etc), enjoy.

                • 5. Re: Move xml element into Lib Item
                  hurix@123 Level 1

                  Yes

                   

                  i had some changes my script

                   

                  for(lp=0;lp<=myXMLElements.length-1; lp++){

                   

                  ***

                   

                   

                  it works fine, But its move(Boxed Text Element)  to the lib item Box Text Style is UNTAGED. it was not in XML Structured, it was in plain text, which is applied by paragraphStyle.

                   

                  Can you please sugest me.

                   

                  Hurix

                  • 6. Re: Move xml element into Lib Item
                    Vamitul Level 4

                    not sure i can.

                    instead of move, try duplicate.

                    tell me if it's taged.

                    • 7. Re: Move xml element into Lib Item
                      hurix@123 Level 1

                      No it was not taged.

                       

                       

                      Hurix

                      • 8. Re: Move xml element into Lib Item
                        hurix@123 Level 1

                        I try below code

                         

                        it works but the lib.asseet.placeAsset was placing Firstpage of the Document.

                         

                        var myAnchorFrame=myLib.assets.item("Box").placeAsset(app.documents[0]);

                        myAnchorFrame[0].textFrames[0].placeXML(myElement);

                         

                         

                        Hurix