8 Replies Latest reply on Feb 8, 2009 4:36 PM by (Dave_Saunders)

    Indesign para2txtframe script problem


      I've adapted Peter Kahrel's script "footnotes2textframes" for my own problem.
      I would like to move paragraphs from a list of figures in a single story to separate textframes. I use these later as captions for image files in a particular book.

      Here is my adaptation:

      function paragraphs2textframes()
      var myDoc = app.activeDocument;

      // process all
      for( var i = 0; i < myDoc.stories[0].paragraphs.length; i++ )

      // get text of paras in the story
      var myParagraphs = myDoc.stories[0].paragraphs[i].contents;
      for( var j = 0; j < myParagraphs.length; j++ )

      // create new text frame for each note and label each frame
      myTframe = myDoc.textFrames.add(
      { label : 'temp' + String(i)+String(j) } );

      // move paragraph text to frame
      myParagraphs[j].texts[0].move( LocationOptions.after, Story);


      Please help.
      Many thanks.

        • 1. Re: Indesign para2txtframe script problem
          Level 2
          You don't say what goes wrong, but it is almost certainly caused by your iterating upwards through the paragraphs. When you move the text of paragraph zero elsewhere, every paragraph in the story shuffles up a position.

          Also, you have two iterations through the same paragraphs inside each other, which must also be a cause of a problem or two.

          And, "Story" is the name of an object class (in the move statement). You need to be moving to a particular instance of a story (presumabley myTframe.parentStory.

          You should close off the for i loop after the grabbing of the contents -- you could also do away with that whole loop and use:

          var myParagraphs = myDoc.stories[0].paragraphs.everyItem().contents

          Expect that you've used a terribly confusing name for an array of the contents of the paragraphs. Later in the script you completely forget that myParagraphs is an array of strings and try to refer to it as though it were an array of paragraph references. So make it:

          var myContents = myDoc.stories[0].paragraphs.everyItem().contents

          Now you should use something like:

          for (var j = 0; myContents.length > j; j++) {
          myTframe = myDoc.textFrames.add({label:'temp' + String(j)});
          myParagraphs[0].move(LocationOptions.after, myTframe.parentStory);

          At least, that's assuming I know what you're trying to do. Your use of the loop inside the loop was doomed to failure because the second time through the story would be empty.

          Hope this helps.

          • 2. Re: Indesign para2txtframe script problem
            (David_Milson) Level 1
            Dear Dave,

            Many thanks for your quick reply. As you noticed, yes, I am a beginner to Java, after many hours of repetitive tasks.

            I do believe you understand what I'm trying to do, so here goes:

            I tried your suggestions, and additionally changed the last myParagraphs to myContents as above, but now get this error on the last line:
            myContents[0].move is not a function.

            Here is the script now:
            function paragraphs2textframes()

            var myDoc = app.activeDocument;
            var myContents = myDoc.stories[0].paragraphs.everyItem().contents;
            for( var j = 0; myContents.length>j; j++ )

            myTframe = myDoc.textFrames.add({ label : 'temp' + String(j) } );

            myContents[0].move( LocationOptions.after, myTframe.parentStory);


            Thanks for your time.
            • 3. Re: Indesign para2txtframe script problem
              Level 2
              You can't move a JavaScript string. You need references to the paragraphs. I think I slightly screwed up above.

              Indeed, you don't seem to be using the contents at all, so:

              var myParas = myDoc.stories[0].paragraphs;
              for (var j = 0; myParas.length > j; j++) {
              myTF = myDoc.textFrames.add({label: 'temp' + j}); // String(j) not needed

              • 4. Re: Indesign para2txtframe script problem
                (David_Milson) Level 1
                Dear Dave,

                Good! My apologies for taking up time on your Sunday.

                Now it works, but always through one-half, then stops. The console comes up with 'undefined' for the second half.

                • 5. Re: Indesign para2txtframe script problem
                  (David_Milson) Level 1

                  I changed the j++ argument to j+1

                  Works now.

                  Thanks for your help, and your time.
                  Can you recommend a good beginner Java book?

                  • 6. Re: Indesign para2txtframe script problem
                    [Jongware] Most Valuable Participant
                    >I changed the j++ argument to j+1
                    >Works now.

                    That's amazing. 'j++' is a shortcut for 'j+1' -- there should not be any difference at all.[*]

                    >..a good beginner Java book..

                    There are tons of sites with good on-line help. You only have to remember that everything relating to browsers is worthless :-)
                    But tinkering with other people's scripts is a really good way to start programming! "What if I change
                    i this

                    [*] Huh. If I read that a 2nd time, it doesn't make sense at all. "j++" increments j immediately, while "j+1" does nothing to j (not in the context you are using it).
                    • 7. Re: Indesign para2txtframe script problem
                      Fred Goldman Level 3
                      I found this to be a great book:

                      JavaScript: The Definitive Guide

                      As jongware said, you can skip all the chapters about Window, Document
                      and Frame objects and ignore all the HTML stuff.
                      • 8. Re: Indesign para2txtframe script problem
                        Level 2
                        Ah. Of course. First, j++ is not equivalent to j+1. It is the equivalent of j = j+1.

                        The reason you only got half the paragraphs in my version is because each time around the loop the story got shorter by one paragraph and while j went up one, myStory.paragraphs.length came down by one. And so they met half way through the story -- hmm, this is starting to sound like Gerard Hoffnung's famous speech to the Oxford Union.

                        But in your version, j never changed and so when myStory.paragraphs.length reached zero, the loop stopped.