9 Replies Latest reply on Jun 2, 2010 9:33 AM by John Hawkinson

    CS4 - Can I base a loop in my script on page numbers in myDoc?

    BRUNNEBA

      I am looking for a script that will go through pages 100-150 in myDoc, and place specific text file contents on each page.  Here is a snippet of my script that is currently working....

       

      function Get_Bullets(myFacility)
        {  var myString;
        
            if (myFacility = "SoldierField")
         {   
           filename = "/c/SoldierFieldt.txt";  //This is just a file on my C drive. 
           myFileObj = new File(filename);    
           if (myFileObj.open("r"))
            {
             theText = myFileObj.read();
             myFileObj.close();
             myString = theText.replace(/#/g,"\r"); // This replaces all # signs in the file with carriage returns so I can apply unique paragraph styles. 
            }
           else
             {
              alert ("Unable to open file "+filename);
             } 

               }
         else
          { alert("Made it to Bon Air");
         }  

        return myString;
           }

       

       

      The contents of myString need to go into the same text frame (same location) on each page, but the page corresponds to the file name.

       

      i.e. Soldier Field text goes on page 100,  Wrigley Field goes on page 101, etc.  

      How can I loop through the pages with a script and place the correct file on the text frame of the corresponding page?

        • 1. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
          [Jongware] Most Valuable Participant

          Since InDesign internally starts counting at 0, page number 100 is

           

          app.activeDocument.pages[99]

           

          but

           

          The contents of myString need to go into the same text frame (same location) ..

           

          might be a problem. You cannot "select a frame by location". If there is just one single frame on each of these pages, then it comes down to

           

          app.activeDocument.pages[99].textFrames[0]

           

          but if there are, or might be, more than one, you'll have to devise a way to uniquely identify the correct frame. In a rather bad case scenario, you'll have to loop over *all* text frames on each page, comparing (for example) top left coordinates. (A couple of days ago, there was a question on exact measurement representation -- which I could not answer to the poster's full satisfaction, by the way -- and it comes down to "watch out for comparing exact measurements with Javascript.)

           

          myFrameOfInterest = findFrameOnPage (99, "10mm", "15mm");
          
          function myFrameOfInterest (pageNumber, leftpos, toppos)
          {
            var i;
            for (i=0; i<app.activeDocument.pages[pageNumber].textFrames.length; i++)
              if (app.activeDocument.pages[pageNumber].textFrames[i].geometricBounds[0] == toppos &&
                 app.activeDocument.pages[pageNumber].textFrames[i].geometricBounds[1] == leftpos)
                 return app.activeDocument.pages[pageNumber].textFrames[i];
            return null;
          }
          
          • 2. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
            BRUNNEBA Level 1

            Interesting.  I used an alert to show the exact geometric bounds of my frame.  I know that my frame falls on the same location on every page, so I don't need a function for it. This thing does some really crazy things when I try to populate the frame with text.  When the Get_bullets function below runs, the frame at the bounds listed takes on the correct text from my function, but the frame changes fonts and spacing to that of some other frame on the page that is totally unrelated!  The frames aren't even touching each other!     Do you have any idea why this may be happening?  

             

             

            //Get_Bullets is a function that simply returns the text I want to put into the frame.

             

            var myDoc = app.activeDocument;
            var myPages = myDoc.pages;

            with myPages[9](
            for(j=0; j < myPages[9].textFrames.length; j++)
               {var myTextFrame = myPages[9].textFrames[j]
                 if (myTextFrame.geometricBounds = ["3.9146in", "4.0201in", "10.25in", "7.875in"])  

                myTextFrame.contents = Get_Bullets(9);
              }
                  )

             

            Thanks so much for your help!  I just started javascript and InDesign, and you keep pointing me in the right direction.

             

            If I run this....

            var myDocument = app.documents.item(0);
            var myTextFrame = app.selection[0];

            myTextFrame.contents = Get_Bullets(9);

             

            It works perfectly, but again I need it to loop.

            • 3. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
              [Jongware] Most Valuable Participant

              Not tried any further, but perhaps this will help you get on your way?

               

              if (myTextFrame.geometricBounds = ...

               

              To test for equality, you use double ='s:

               

              if (myTextFrame.geometricBounds == ...

               

              ---

              Theoretically, if you put text into an existing textframe using its 'contents' properties, the formatting ought to be taken from the default text formatting (the one that is 'active' when you create a new text frame). At least, I think it works that way.

              If all your formatting is defined through text styles, you need another line

               

              myTextFrame.insertionPoints[-1].appliedParagraphStyle = "..your style name";

               

              right before you fill it with contents.

              1 person found this helpful
              • 4. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
                BRUNNEBA Level 1

                I have it working now!   It seems for me the textFrame id was a much better property to use to get to the specific frame, since I knew that the ID would not change, even if  some other frame is overlapping it will work.

                 

                The geometric bounds I guess just leaves too much room for error!

                • 5. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
                  John Hawkinson Level 5

                  You might also find the Script Label to be a handy property here, since you have more control over it than the ID.    

                  • 6. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
                    BRUNNEBA Level 1

                    The label really worked best in my case.  Here is another simple script that is really driving me nuts: My question is in comments.

                     

                    var myDoc = app.activeDocument;
                    var myTextFrames = myDoc.textFrames;

                    for (i = myTextFrames.length-1; i >= 0 ; i--) 
                    {var myFrame = myTextFrames[i];
                      var id = myFrame.label;
                       if (id == "PlacementDemo")
                    {txt = myFrame.contents.replace("2006-2009","2007-2010");  // I know the contents are changed here.
                      myFrame.contents = txt;  // Here, nothing happens!  The contents of the frame do not change. Why?!!!
                    }
                       }

                    • 7. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
                      John Hawkinson Level 5

                      Try posting your script with Java syntax highlighting. It makes it easier to read.

                       

                      Anyhow, your script worked fine for me. I suggest adding these $.writeln statements so you can see what's going on:

                       

                      var myDoc = app.activeDocument;
                      var myTextFrames = myDoc.textFrames;
                      
                      for (i = myTextFrames.length-1; i >= 0 ; i--) 
                      {var myFrame = myTextFrames[i];
                        var id = myFrame.label;
                           $.writeln("id: "+id);
                         if (id == "PlacementDemo") {
                              var txt = myFrame.contents.replace("2006-2009","2007-2010");
                                 // I know the contents are changed here.
                              $.writeln("replaced:: "+txt);
                            myFrame.contents = txt;
                            // Here, nothing happens!  The contents of the frame do not change. Why?!!!
                        }
                      }
                      0;
                      

                       

                      Also, you should "var txt" instead of "txt" so you don't use a global variable.

                       

                      Can you post a screenshot? Maybe you have something special, like non-ASCII charcters in your text frame. Or you are using an endash instead of a hyphen, so you replace string doens't match?

                       

                      You say you "know" the contents are changed. How do you know that? Show us, don't tell us...

                      1 person found this helpful
                      • 8. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
                        BRUNNEBA Level 1

                        That worked perfectly!  "var txt" must have been the issue.

                         

                        I was using alerts to see the values of "id," so that's why I said I knew the value was changing.  Thanks for the $.writeIn tip!   That is a much better way to see what's going on.

                        • 9. Re: CS4 - Can I base a loop in my script on page numbers in myDoc?
                          John Hawkinson Level 5

                          Hmm. It worked for me without the 'var txt' (I tested your script as you had written it), but "Go figure!"

                          Glad you're all set.