9 Replies Latest reply on Nov 15, 2010 1:53 PM by [Jongware]

    Placing TextFrames in CS5 with JavaScript

    PeterPow

      Hello guys,

       

      I'm trying to place a textframe with the following javascript:

       

      var myDoc = app.documents[0];

      myDoc.pages.itemByID(1234).textFrames.add();

       

      the page "1234" is the second page in a spread, but the frame is always placed on the first page and not the one selected in the script. Is this a known issue with CS5 server or do I have to change something in my scripts?

      Thanks in advance!

        • 1. Re: Placing TextFrames in CS5 with JavaScript
          Olav Martin Kvern Level 3

          Hi PeterPow,

           

           

          Are you certain that the id of the page is 1234? That's not a typical InDesign ID. If it's page 2, then try:

           

          var myDoc = app.documents.item(0);

          myDoc.pages.item(1).textFrames.add();

           

          Thanks,

           

          Ole

          • 2. Re: Placing TextFrames in CS5 with JavaScript
            PeterPow Level 1

            Sorry for that, the ID was only an example, in my script the page object is initialized correctly.

             

            Nevertheless the problem seems to be connected to the fact, that CS5 changed the hirarchy of parent elements. There is no page any more as a parent of a page item but only a spread. When I insert a textframe on a page and set the geometric bounds relative to the page, the server interprets this as relative coordinates to the spread. That is why my page item is placed on the first page of the spread when changing the bounds. Is there a possibility to tell the server to do it the other way round or do I have to change all of my scripts?

            Thanks!

            • 3. Re: Placing TextFrames in CS5 with JavaScript
              Olav Martin Kvern Level 3

              Hi PeterPow,

               

              Yes, the hierarchy has changed in CS5. The parent of page items that are "loose" on the page (i.e., not contained inside a group, page item, or text) is the spread. But there's a new property, parentPage, that will give you the page containing the page item. This works even if the item is inside a group or other container.

               

              Thanks,

               

              Ole

              • 4. Re: Placing TextFrames in CS5 with JavaScript
                PeterPow Level 1

                I know the property parentPage, but thanks anyway

                 

                The problem is not to get the page of a page item but to set the position of it by changing the geometricBounds property. Our script works by changing this property relative to the containing page. Because of the fact, that the page is not in the parents hirarchy any more I obviously have to change the geometricBounds property relative to the spread.  My question was, if there is a flag that can be set to make the geometricBounds property to be interpreted relative to the containing page.

                Thanks

                • 5. Re: Placing TextFrames in CS5 with JavaScript
                  Olav Martin Kvern Level 3

                  Hi PeterPow,

                   

                  If you set the rulerOrigin property of the document (in viewPreferences) to RulerOrigin.pageOrigin, the bounds will be interpreted relative to the page (that is, 0,0 will be the upper-left corner of the page). Sorry it's taking me so long to guess what the problem is--there are lots of ways to do these things, and I'm not yet clear on which approach you're using.

                   

                  It sounds to me as if you're:

                   

                  * Getting a reference to the parent of an existing page item

                  * Using that reference to create a new page item

                  * Setting the geometric bounds of the new page item

                   

                  Is that correct?

                   

                  Thanks,

                   

                  Ole

                  • 6. Re: Placing TextFrames in CS5 with JavaScript
                    PeterPow Level 1

                    Our application allows it to create a page item on a page or on a spread. In case of a spread the application works correctly but when I am trying to place a text frame on a page and setting the geometricBounds they are interpreted also relative to the spread. The document view preference rulerOrigin doesn't change anything. But this problem occurs in CS5, CS4 worked correctly with this script. May that be a bug in the CS5 server and can you maybe reproduce the error with this script:

                     

                    var doc = app.documents[0];

                    doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;


                    var page = doc.spreads.firstItem().pages.add(LocationOptions.AFTER, doc.spreads.firstItem().pages.firstItem());

                    var tf = page.textFrames.add();

                    tf.geometricBounds = [0, 0, 100, 100];

                     

                    the textframe should be created on the second page with the xy-pos 0,0. But the result is a textframe placed on the first page of the spread.

                    Thanks.

                    • 7. Re: Placing TextFrames in CS5 with JavaScript
                      Olav Martin Kvern Level 3

                      Hi PeterPow,

                       

                      One thing I see is that if the document starts with page 1 on a right hand page (recto), and the first spread (the one containing page 1) is set to allow pages to shuffle, then this:

                       

                      var page = document.spreads.firstItem().pages.add(LocationOptions.AFTER, document.spreads.firstItem().pages.firstItem());

                       

                      ...will add a new page after page 1, thereby creating a new spread. The rectangle is drawn on the correct page--page 2, the first page of the second spread. A subsequent call to the line above will add a page at the start of the second spread (because it's being added after the first spread) and that page (page 2) will get the rectangle.

                       

                      If, on the other hand, the document starts with (a left-hand/verso) page 2, the new page is added at the end of the spread, and the new page (3) gets the rectangle.

                       

                      This seems to be working correctly. You might want to have your script check page.side (is it a recto page?) and spread.allowPageShuffle to see if the added page will be added to this spread or to the start of the next spread.

                       

                      Hope this makes sense!

                       

                      Thanks,

                       

                      Ole

                      1 person found this helpful
                      • 8. Re: Placing TextFrames in CS5 with JavaScript
                        PeterPow Level 1

                        Hi Olav,

                         

                        you are right, I tried this again with a newly created document in CS5 and everything works as expected. The only difference to my previously tested document was the fact, that this document was converted from CS4 to CS5. In this case the option for shuffeling pages has no effect, it simply doesn't work. But thats ok for me if this document remains the only one not working

                        Thanks for your help anyway!

                        • 9. Re: Placing TextFrames in CS5 with JavaScript
                          [Jongware] Most Valuable Participant
                          .. the fact, that this document was converted from CS4 to CS5 ..

                           

                          Try exporting this document to IDML then opening this again. It might clear out all left-overs from the older version.