11 Replies Latest reply on Jan 6, 2009 9:01 AM by Skempy

    [JS CS3] Releasing Anchored Objects

    Skempy Level 1
      Hello,

      The following script places a libray item as an anchored object...

      var myDoc = app.documents[0];
      var myFrame = app.documents[0].textFrames.item("myTarget");
      var myInsertPoint=myFrame.parentStory.paragraphs.item(0).insertionPoints.item(0)
      var myLibraryItem=app.libraries[0].assets[0]; myFrame=myLibraryItem.placeAsset(myInsertPoint);
      with(myFrame[0].anchoredObjectSettings){
      anchoredPosition = AnchorPosition.anchored;
      anchorPoint = AnchorPoint.topLeftAnchor;
      horizontalReferencePoint = AnchoredRelativeTo.anchorLocation;
      horizontalAlignment = HorizontalAlignment.leftAlign;
      anchorXoffset = 0;
      anchorYoffset = 0;
      }

      How do I now release my anchored library item and then send it behind the text in the text frame "myTarget" ?

      Thanks

      Simon Kemp
        • 1. Re: [JS CS3] Releasing Anchored Objects
          Loic.Aigon Adobe Community Professional
          Hi,
          In the UI, it seems that you can't reorder a anchored object. It's like it's place over the text frame and there is nothing you can do about it.
          So, I would say that as a anchored object, no single line of code will reorder the item to be under the text frame myTarget.
          If you detach the anchored object, you can use the geometricBounds I guess to copy/paste and then move the object to the original position and under the textFrame myTarget.
          Problem is, it won't be anchored anymore.
          I don't know what is your final objective but you may get what you want using another approach than anchored object.
          Loic
          • 2. Re: [JS CS3] Releasing Anchored Objects
            Skempy Level 1
            Loic,

            Thanks for the reply.

            I can get the Geometric Bounds of the anchored frame, duplicate it, use the Geometric Bounds to move the duplicate, send the duplicate to the back and remove the original anchored text frame. This does the job but I thought there may be a simpler way.

            You can "Release" the anchored frame in the UI but I can't find how to do this with javascript.

            I am aiming for a (vertical)gradient fill behind paragraph of text relative to the height of the paragraph.

            Simon
            • 3. Re: [JS CS3] Releasing Anchored Objects
              Loic.Aigon Adobe Community Professional
              That's what I suspected. You may use another approach which is converting text to table. Hence, you will have a cell that you will be able to custom (fill with gradient). This cell will behave like a anchored object.
              Have a look on this.
              Hear you soon
              Loic
              • 4. Re: [JS CS3] Releasing Anchored Objects
                Loic.Aigon Adobe Community Professional
                Only problem, it will be much less convenient if you edit text.
                • 5. Re: [JS CS3] Releasing Anchored Objects
                  Harbs. Level 6
                  textFrame.anchoredObjectSettings.releaseAnchoredObject ()

                  --
                  Harbs
                  http://www.in-tools.com
                  • 6. Re: [JS CS3] Releasing Anchored Objects
                    Level 1
                    Harbs,

                    Holy smoke. How long has that been hiding there!!!!!

                    Thanks,

                    Dave
                    • 7. Re: [JS CS3] Releasing Anchored Objects
                      Skempy Level 1
                      Thanks Harbs, this has made things simpler.

                      I have heard your name mentioned several times today in the InDesign Secrets podcast (and Dave's too).

                      I am almost there with the script (my scripting is clumsy but I can usually get things to work mainly with help from this forum.

                      My last problem is that I am re-applying geometric bounds to the released anchored object as it is still being referenced as myAnchoredFrame.

                      Is it possible to release ALL the anchored objects in one go? Or how do I dump the reference to myAnchoredFrame once it has been released?

                      var myTextFrame = app.documents[0].textFrames.item("myTarget");
                      var myParagraphs = myTextFrame.paragraphs
                      for (var i = 0; i <= myParagraphs.length-1; i++) {
                      var nLines = myParagraphs.item(i).lines.length;
                      var myAnchoredFrame = myTextFrame.paragraphs.item(i).insertionPoints.item(0).textFrames.add();
                      var myHeight = nLines*myParagraphs[i].leading*.353
                      myAnchoredFrame.geometricBounds = [0, 0, myHeight, (myTextFrame.geometricBounds[3]-myTextFrame.geometricBounds[1])];
                      myAnchoredFrame.fillColor = "Gradient";
                      myAnchoredFrame.gradientFillAngle = 90;
                      myTextFrame.texts.item(0).recompose;
                      with(myAnchoredFrame.anchoredObjectSettings){
                      anchoredPosition = AnchorPosition.anchored;
                      anchorPoint = AnchorPoint.topLeftAnchor;
                      horizontalReferencePoint = AnchoredRelativeTo.anchorLocation;
                      horizontalAlignment = HorizontalAlignment.leftAlign;
                      verticalReferencePoint = VerticallyRelativeTo.capheight;
                      verticalAlignment = VerticalAlignment.topAlign;
                      anchorXoffset = 0;
                      anchorYoffset = -1;
                      pinPosition = false
                      }
                      myAnchoredFrame.anchoredObjectSettings.releaseAnchoredObject ()
                      myAnchoredFrame.sendToBack();
                      }

                      Thanks again

                      Simon Kemp
                      • 8. Re: [JS CS3] Releasing Anchored Objects
                        Harbs. Level 6
                        Hi Dave,

                        I believe it was added in CS3. Glad to be of help! :)

                        Simon,

                        No you can't release all the frames at once. You need to loop through
                        them all. What's the problem with your code. I didn't study it well, but
                        you seem to get a new reference during each iteration of your loop.

                        By the way, the "recompose" statement is doing nothing. You need to
                        either write recompose(), or just take it out. It's not necessary for CS3.

                        --
                        Harbs
                        http://www.in-tools.com
                        • 9. Re: [JS CS3] Releasing Anchored Objects
                          Skempy Level 1
                          Harbs

                          If I comment out the two lines
                          myAnchoredFrame.anchoredObjectSettings.releaseAnchoredObject();
                          myAnchoredFrame.sendToBack();

                          Then gradient frames are placed correctly behind each paragraph of text within by labelled text frame.

                          By un-commenting the two lines in order to release the anchored frames and send them behind the text the placement of the frames goes wrong.

                          With four paragraphs the first gradient filled frame is placed correctly behind the first paragraph, the next two gradient filled frames are placed in the top lefthand corner of the page and the forth gradient filled frame is placed to the left of myTextFrame.

                          My orginal script which makes duplicates of the anchored frames and then moves them into position works OK but I would really like to use the releaseAnchoredObject(); method.

                          Any further help would be greatly appreciated.

                          One last thing, is there a CS3 version of the Scripting Reference PDF?

                          Thanks

                          Simon.
                          • 10. Re: [JS CS3] Releasing Anchored Objects
                            Harbs. Level 6
                            Hi Simon,

                            Without debugging your script, I don't know off hand where things are
                            going off the rails, but I'm always suspicious of "with" statements. You
                            can try stepping through the script in the ESTK to see where things are
                            going wrong.

                            A simple way around your problem would be to build an array of your
                            anchored frames as you go and then after creating them all you'd release
                            them in a separate loop.

                            --
                            Harbs
                            http://www.in-tools.com
                            • 11. Re: [JS CS3] Releasing Anchored Objects
                              Skempy Level 1
                              Harbs

                              Many thanks for all your help.
                              Your array of anchored frames technique did the trick.
                              And I have learnt a few new things on the way.

                              The working script now looks like this...

                              var myTextFrame = app.documents[0].textFrames.item("myTarget");
                              var myParagraphs = myTextFrame.paragraphs
                              var myWidth = (myTextFrame.geometricBounds[3]-myTextFrame.geometricBounds[1])/2
                              var myAOs = new Array();

                              for (var i = 0; i <= myParagraphs.length-1; i++) {

                              var nLines = myParagraphs.item(i).lines.length;
                              var myHeight = nLines*myParagraphs[i].leading*.353
                              var myAnchoredFrame = myTextFrame.paragraphs.item(i).insertionPoints.item(0).textFrames.add();
                              myAnchoredFrame.geometricBounds = [0, 0, myHeight, myWidth];
                              with(myAnchoredFrame.anchoredObjectSettings){
                              anchoredPosition = AnchorPosition.anchored;
                              anchorPoint = AnchorPoint.topLeftAnchor;
                              horizontalReferencePoint = AnchoredRelativeTo.anchorLocation;
                              horizontalAlignment = HorizontalAlignment.leftAlign;
                              verticalReferencePoint = VerticallyRelativeTo.capheight;
                              verticalAlignment = VerticalAlignment.topAlign;
                              if (i%2 == 0)
                              {
                              anchorXoffset = 0
                              }
                              else
                              {
                              anchorXoffset = -myWidth
                              }
                              anchorYoffset = -1
                              pinPosition = false
                              myAOs[i] = myAnchoredFrame
                              }
                              } // Loop through paragraphs

                              for (var j = 0; j <=myAOs.length-1;j++){
                              myAOs[j].anchoredObjectSettings.releaseAnchoredObject ()
                              myAOs[j].sendToBack();
                              myAOs[j].fillColor = "Gradient";
                              myAOs[j].gradientFillAngle = 90;
                              }

                              Thanks again

                              Simon.