9 Replies Latest reply on May 21, 2008 6:17 AM by (Pål_Nesteby)

    Looping through anchored frames

      Hi!
      I have a script that loops through all the anchored objects (textframes etc.) in a document and releases them. Before I release them I need to cut out the content of the textframe etc. and paste it to the anchorpoint so that the content of the textframe becomes a part of the main story. This is because I want to make XML and I don't want to have several Story-elements in my xml.

      For textframes I have this:
      For Each s In myDoc.Stories
      For Each o In s.TextFrames
      o.Select()
      o.AnchoredObjectSettings.ReleaseAnchoredObject()
      Next o
      Next s

      I have tried to use: o.contents.select, but that doesn't seem to work...I also wonder how I can paste it to the right place?
      Can anyone help me with tips?

      In advance thanks

      Pål, Oslo
        • 1. Re: Looping through anchored frames
          Level 1
          first - DON'T USE For...Each - it have limit to 255 iterations

          '############
          ' script start
          '############
          set myInDi=createobject("InDesign.Application.CS3")
          set myDoc=myInDi.ActiveDocument
          for a=mydoc.stories.count to 1 step -1
          set mystory=mydoc.stories.item(a)
          if mystory.textframes.count>0 then
          for b=mystory.textframes.count to 1 step -1
          set myAnchoredTF=mystory.textframes.item(b)
          call myInDi.Select(myAnchoredTF.Texts.Item(1))
          call myInDi.Copy
          call myInDi.Select(myAnchoredTF.Parent)
          call myInDi.Paste
          next
          end if
          next
          '############
          ' script end
          '############

          this script replace Anchored TextFrames with its contents
          probably .move could be used - but I prefer old good copy&paste ;)

          if you need - I have custom tools to export contents of indesing file as XML

          robin

          --
          www.adobescripts.com
          • 2. Re: Looping through anchored frames
            Level 1
            Tank you very much!
            This was really good help!!

            :-)

            Greetings!
            • 3. Re: Looping through anchored frames
              Level 1
              Hi again!
              It's strange, I've noticed some problems with the script...
              First I had to insert an "On error resume next" because I sometimes got an COMException. -> "cannot Copy/Cut due to invalid selection state"
              This is my script now:

              Sub return_objects()

              Dim myIndesign As Object
              Dim myDoc As InDesign.Document

              Dim o As InDesign.TextFrame
              Dim s As InDesign.Story

              myIndesign = CreateObject("InDesign.Application.CS3")
              myDoc = myIndesign.ActiveDocument
              For a = myDoc.Stories.Count To 1 Step -1
              s = myDoc.Stories.Item(a)
              If s.TextFrames.Count > 0 Then
              For b = s.TextFrames.Count To 1 Step -1
              o = s.TextFrames.Item(b)
              'putting on a vbcr on the back of the text
              o.InsertionPoints.Item(-1).Contents = vbCr
              Call myIndesign.Select(o.Texts.Item(1))
              'To prevent the COMException
              On Error Resume Next
              Call myIndesign.Copy()
              Call myIndesign.Select(o.Parent)
              'Putting on a vbcr on the front
              o.Parent.InsertionPoints.Item(1).Contents = vbCr
              Call myIndesign.Paste()
              Next
              End If
              Next
              End
              End Sub

              I put on the vbcr to preserve the formatting of the text that was inside the textframe.
              I thought that I fixed the script with the "On error resume next", but now I see that some places in the document parts of the text is missing...!! The same text from the same textframes is missing every time I run it!
              Do you know why this happens?

              I really hope you've got a clue! because I haven't!
              • 4. Re: Looping through anchored frames
                Level 1
                Can the reason be problems with the memory?
                Is there someway to flush the clipboard so that I get more memory to work with?

                I have also noticed that this is most usual when objects are anchored inside another anchored object, but I've also seen it in just one anchored TextFrame. The sript moves the content of the TF after its anchorpoint, but the last part of the text is missing...

                Pål
                • 5. Re: Looping through anchored frames
                  Peter Kahrel Adobe Community Professional & MVP
                  Pål,

                  I use the function listed below to incorporate the text in inlines into the main text. It moves text rather than copying&pasting, so the problem you encountered shouldn't occur. It handles multiply embedded inlines. It shouldn't be too hard to convert it to VB.

                  Peter

                  function inlines (doc)
                  
                     {
                     var st = doc.stories;
                     for (var i = doc.stories.length-1; i > -1; i--)
                        while (st[i].textFrames.length > 0 )
                           {
                           var ix = st[i].textFrames[-1].parent.index;
                           st[i].textFrames[-1].texts[0].move (LocationOptions.after,
                              st[i].insertionPoints[ix]);
                           st[i].textFrames[-1].locked = false;
                           st[i].textFrames[-1].remove();
                           }
                     }
                  • 6. Re: Looping through anchored frames
                    Level 1
                    Thank you very much!
                    This seems to be a more secure way!
                    Excellent!:-)
                    By the way, do you know how I can check the text I'm moving to be sure that it has an paragraph symbol at the end? I need to put on a carriage return if it hasn't to keep its original paragraphstyle!

                    Here is the code in Visual Basic.NET

                    Sub return_objects2()

                    Dim myIndesign As Object
                    Dim myDoc As InDesign.Document

                    Dim st As InDesign.Stories
                    Dim ix As Object

                    myIndesign = CreateObject("InDesign.Application.CS3")
                    myDoc = myIndesign.ActiveDocument

                    st = myDoc.Stories

                    For a = myDoc.Stories.Count To 1 Step -1
                    While st(a).textFrames.count > 0
                    ix = st(a).textFrames(-1).parent.index
                    st(a).textFrames(-1).texts(1).move(InDesign.idLocationOptions.idAfter, st(a).insertionPoints(ix))
                    st(a).textFrames(-1).locked = False
                    st(a).textFrames(-1).delete()
                    End While
                    Next
                    End
                    End Sub

                    Again thank you very much!

                    Pål
                    • 7. Re: Looping through anchored frames
                      Level 1
                      if st(a).textFrames(-1).texts(1).characters.item(-1).contents = vbCR then
                      else
                      end if

                      but you need to be sure that you have all text visible in your TexFrames
                      if not - you need to check ParentStory of TextFrame:

                      if st(a).textFrames(-1).PARENTSTORY.texts(1).characters.item(-1).contents = vbCR then
                      else
                      end if


                      robin

                      --
                      www.adobescripts.com
                      • 8. Re: Looping through anchored frames
                        Level 1
                        Thanks Robin!
                        Parentstory worked excellent!

                        Now I can start making XML and XMLRules!

                        Pål
                        • 9. Re: Looping through anchored frames
                          Level 1
                          By the way Robin, I saw that your first suggestion also worked when I used:
                          Call myIndesign.Select(o.ParentStory.Texts.Item(1))

                          instead of:
                          Call myIndesign.Select(o.Texts.Item(1))

                          Since I added a vbcr the text moved outside the textframe so it became overset.

                          :-)