21 Replies Latest reply on Mar 30, 2009 5:05 AM by John.Kordas

    CS3 JS flag which page has empty frame

    John.Kordas Level 1
      I am trying to put together a script that will detect pages that have 2 types of elements.

      1. blank graphic frames
      2. graphic frames with images larger than the frame holding it.

      I've created a simple 5 page doc and placed images on page 1 and 3. So the frist step is can I flag that page 1 and 3 have image on them.

      This is what I have so far

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

      for (var j = myPages.length - 1; j >= 0; j--) {
      if(myPages[j].rectangles.length >= 1){
      $.writeln("page "+j+" has a frame.");
      }
      }

      And it returns the following in the console which is correct.
      page 2 has a frame.
      page 0 has a frame.

      Is there a way to check if the frame has an image and if it image fits the frame or not?
        • 1. Re: CS3 JS flag which page has empty frame
          Level 1
          Check the length of the frame's images collection. If > 0, it has an image. Then compare the image's bounds with those of the frame.

          Dave
          • 2. Re: CS3 JS flag which page has empty frame
            John.Kordas Level 1
            Thanks Dave,

            I've got it to check if there is a graphic and this seems to work but can not seem to get it to select the rectangle.

            for (var j = myPages.length - 1; j >= 0; j--) {
            if(myPages[j].rectangles.length >= 1 && myPages[j].allGraphics.length > 0 ){

            myPages[j].rectangles.select()
            }
            }

            error: myPages[2].rectangles.select is not a function

            any suggestion?
            • 3. Re: CS3 JS flag which page has empty frame
              Level 1
              Does:

              app.select(myPages[j].rectangles)

              work?

              You need to check the object model to make sure that the method you're trying to use works with the object(s) you're trying to use it with.

              The search feature in ESTK CS4's OMV is very useful for this.

              Dave
              • 4. Re: CS3 JS flag which page has empty frame
                Level 1
                Ah, you're using CS3. Well, it too has an OMV, but I'm sure then search is so valuable there.

                Dave
                • 5. Re: CS3 JS flag which page has empty frame
                  John.Kordas Level 1
                  Thanks again Dave,

                  The select part is working but once selected I'm not able to effect the frame. Using this:

                  for (var j = myPages.length - 1; j >= 0; j--) {
                  if(myPages[j].rectangles.length >= 1 && myPages[j].allGraphics.length > 0 ){
                  var myGraphic = myDoc.select(myPages[j].rectangles);
                  myGraphic.rectangle.fit (CENTER_CONTENT)

                  }
                  }

                  b var myGraphic = myDoc.select(myPages[j].rectangles);

                  myGraphic is now the variable that is selected correct?

                  Now if I want to effect that variable I should be able to do this:

                  b myGraphic.rectangle.fit (CENTER_CONTENT)

                  but I'm getting the following error.
                  undefined is not an object.

                  in past creating the rectangle and changing it using:
                  var myFrame = pages.item(0).rectangles.add({strokeWeight:0, strokeColor:myDocument.swatches.item("None")});
                  with(myFrame){
                  geometricBounds = myGetBounds(myDocument, myPage);
                  myFile = myFrame.place( File(myFile), false );
                  app.activeWindow.zoom(ZoomOptions.fitPage);

                  has worked with no problems. I seem to be having problems manipulating rectangles already created.
                  • 6. Re: CS3 JS flag which page has empty frame
                    Level 1
                    Hi John,

                    I don't understand why you're selecting with your script. You only need to reference the rectangle to operate on it.

                    But you've made up the property "rectangle" for myGraphic. Look in the OMV and you'll see that graphics don't have rectangle properties. I think you want the parent there--remember, a graphic could be in a polygon or oval or even a graphic line.

                    There are a small number of actions that require selecting, but not very many. The main reason for using a selection is to show the user what you just did, but you're in a loop so selecting seems completely pointless.

                    Dave
                    • 7. Re: CS3 JS flag which page has empty frame
                      John.Kordas Level 1
                      Thanks again Dave I've been working my way through OMV and I think I'm finally getting it. Also went back to the scripting guide.

                      This is working for me:
                      for (var j = myPages.length - 1; j >= 0; j--) {
                      if(myPages[j].rectangles.length >= 1 && myPages[j].allGraphics.length > 0 ){
                      myPages[j].rectangles.item(0).fit(FitOptions.proportionally);
                      myPages[j].rectangles.item(0).fit(FitOptions.centerContent);
                      }
                      }

                      Interestingly I found that if I use

                      myPages[j].rectangles.item(0).geometricBounds = [0,0,144,144];

                      Then rectangle that is on the right hand page ends up on the left hand page. I would of thought that 0,0 would put the rectangle at the top left corner of the right hand page not the left hand page.
                      • 8. Re: CS3 JS flag which page has empty frame
                        Level 1
                        I usually switch to spread coordinates to avoid those kinds of issues (although others swear by page coordinates). I use these two functions:
                            function saveMeasures(doc, units) {
                        
                              var uPrefs = doc.viewPreferences;
                              var uMeasures = {
                                horiz:uPrefs.horizontalMeasurementUnits,
                                vert:uPrefs.verticalMeasurementUnits,
                                rule:uPrefs.rulerOrigin,
                                zp:doc.zeroPoint
                              }
                              doc.viewPreferences.properties = {
                                horizontalMeasurementUnits:units,
                                verticalMeasurementUnits:units,
                                rulerOrigin:RulerOrigin.spreadOrigin,
                              }
                              doc.zeroPoint = [0,0];
                              return uMeasures;
                            }
                            function restoreMeasures(doc, measures) {
                              doc.viewPreferences.properties = {
                                horizontalMeasurementUnits:measures.horiz,
                                verticalMeasurementUnits:measures.vert,
                                rulerOrigin:measures.rule
                              }
                              doc.zeroPoint = measures.zp;
                            }

                        I call these like this--notice the code is inside a function; that makes it possible to use return to exit the script which in turn makes it possible to call the restore function as I exit, using the return in two different places:
                          function splitElements(aDoc) {
                        
                            var curPage = app.activeWindow.activePage;
                            var prevPage = aDoc.pages[curPage.documentOffset - 1];
                            if (prevPage === aDoc.pages[-1]) {
                              alert("Don't run this script on the first page of a document.");
                              return;
                            }
                            var uMeasures = saveMeasures(aDoc, MeasurementUnits.points);
                            var oSized = getOsetElement(prevPage);
                            if (oSized === null) return restoreMeasures(aDoc, uMeasures); // nothing to do
                            splitElement(oSized, prevPage, curPage, aDoc);
                            return restoreMeasures(aDoc, uMeasures);
                          }
                        This script needed to convert the measurement units to points because it needs to do calculations involving leading and stroke weights, both of which are provided in points.

                        Dave
                        • 9. Re: CS3 JS flag which page has empty frame
                          Dirk Becker  Level 4
                          The following script is a small modification of Dave's previously posted.

                          It illustrates two advanced javascript concepts:
                          • The nested function can refer to outside local variables of its closure.

                          • Code enclosed by a try-finally will execute the finally clause even if there is a return in between.




                          • function setupMeasures(doc)

                            {

                               var vp = doc.viewPreferences.properties;

                               var zp = doc.zeroPoint;

                               doc.viewPreferences.properties = {

                                  horizontalMeasurementUnits: MeasurementUnits.points,

                                  verticalMeasurementUnits: MeasurementUnits.points,

                            rulerOrigin: RulerOrigin.spreadOrigin,

                               }

                               doc.zeroPoint = [0,0];

                               return function ()

                               {

                                  doc.viewPreferences.properties = vp;

                                  doc.zeroPoint = zp;

                                  alert("Here!");

                               }

                            }



                            (function main()

                            {

                               var restoreMeasures = setupMeasures(app.activeDocument);

                               try {

                               // do your stuff

                                  return   42;

                               } finally {

                                  restoreMeasures();

                               }

                            })();





                            Dirk
                          • 10. Re: CS3 JS flag which page has empty frame
                            John.Kordas Level 1
                            Thanks you both for your very useful code. For the moment I've decided to stick with RulerOrigin.pageOrigin;

                            The page setup is A4 and I have a rectangle bleeding on the top,left and bottom. It is also sitting on a left hand page. What I'd like to do is extend the rectangle so that it become a double page rectangle.

                            using
                            var myBounds = myPages[j].rectangles.item(0).geometricBounds;

                            myPages[j].rectangles.item(0).geometricBounds = [myBounds[0],myBounds[1],myBounds[2],(myBounds[3]+myBounds[3])];

                            the rectangle only extend to the edge of the page on the right

                            To check I used:
                            $.writeln(myBounds[2]+" "+myBounds[3]); which returned
                            301.999999999461 209.999999999936

                            Does geometricBounds only give the bounds within the page not outside.
                            • 11. Re: CS3 JS flag which page has empty frame
                              Level 1
                              It gives you the bounds relative to the page the item is considered to be on (or associated with if entirely on the pasteboard).

                              Dave
                              • 12. Re: CS3 JS flag which page has empty frame
                                John.Kordas Level 1
                                Is it possible to get the graphic geometricbounds and compare them to the rectangle bounds? I found graphic.geometricBounds in the OMV but when I try

                                var myBounds = myPages[j].item(0).graphic.geometricBounds;

                                I get the error myPage[1] is not a function.
                                • 13. Re: CS3 JS flag which page has empty frame
                                  Level 1
                                  John,

                                  You're confusing

                                  Graphic.geometricBounds

                                  in the OMV, which tells you that a graphic object has the geometricBounds property, with how to address said graphic in a specific instance.

                                  Pages do not have a graphics (note plural) property, although they do have an allGraphics property (which gives you a list of all of them on the page). Typically, when you're looking for a specific graphic in the fashion you're doing, you need to get at it via the frame that holds it. Assuming it's a rectangle, you might write:

                                  myPages[j].rectangles[0].graphics[0].geometricBounds;

                                  even though the rectangle can only have one graphic, it is still a collection of one and can't be addressed using the singular form.

                                  Dave
                                  • 14. Re: CS3 JS flag which page has empty frame
                                    John.Kordas Level 1
                                    Thanks Dave,

                                    I just got it and was going to post I think I worked it out but you beat me to it.

                                    This give me the rectangle geometricBounds:
                                    var myBounds = myPages[j].rectangles.item(0).geometricBounds;
                                    216 296.999999999461

                                    This gives me the graphic geometricBounds in the rectangle:
                                    var myBounds = myPages[j].rectangles.item(0).graphics.item(0).geometricBounds;
                                    216 218.916666666667
                                    • 15. Re: CS3 JS flag which page has empty frame
                                      John.Kordas Level 1
                                      I'm trying to remove the rectangle on the right hand page but the value returned is not correct.

                                      myPages[j] is currently page 3 which has the rectangle that I want to expand to the right hand page. So to remove the right hand page rectangle before expnading I'm trying:

                                      var rhandpg = j+1;
                                      myPages[rhandpg].rectangles.item(0).remove();

                                      but I get the error Object invalid.

                                      if I leave it as

                                      myPages[j].rectangles.item(0).remove();

                                      The rectangle on page 3 is removed.

                                      Is this because the loop has passed page 4 and will not allow me to remove it?
                                      • 16. Re: CS3 JS flag which page has empty frame
                                        John.Kordas Level 1
                                        I decided to try a new script and use

                                        app.activeDocument.pages[4].rectangles.item(0).remove();

                                        no luck

                                        app.activeDocument.pages[3].rectangles.item(0).remove();

                                        works fine. To selected the rectangle on page 4 menually I selected the Object menu went down to Content all 3 options are greyed out (Graphic, Text, Unassigned). If it's not one of these what is it?

                                        EDIT:

                                        There is some text in the frame/rectangle once I delete the text then all 3 Content options are available.
                                        • 17. Re: CS3 JS flag which page has empty frame
                                          Peter Kahrel Adobe Community Professional & MVP
                                          >If it's not one of these what is it?

                                          Select the object and in the ESTK, run this line:
                                          >app.selection[0].constructor.name

                                          and the object's name will be printed in the console.

                                          Peter
                                          • 18. Re: CS3 JS flag which page has empty frame
                                            Level 1
                                            John,

                                            It's unclear from what you write whether you realize that page 4 is app.activeDocument.pages[3]. Remember, JavaScript starts counting from zero.

                                            For objects that have names, you can always put in an alert to test your assumptions:

                                            alert(obj.name);

                                            Dave
                                            • 19. Re: CS3 JS flag which page has empty frame
                                              John.Kordas Level 1
                                              Thanks Peter

                                              app.selection[0].constructor.name

                                              returns

                                              TextFrame

                                              I tried to edit my previous post but got replies before it made it.There is some text in the frame/rectangle once I delete the text then all 3 Content options are available.
                                              • 20. Re: CS3 JS flag which page has empty frame
                                                John.Kordas Level 1
                                                Thanks Dave,

                                                I got so caught up in the code and I didn't even look at the pages

                                                app.activeDocument.pages[4].name; returns 5

                                                So the left hand page is page 4 and the right hand page is 5 in the layout. So I'm trying to remove the text frame on page 5 and expand the rectangle on page 4.

                                                This works but is it correct?

                                                myDoc.select(myPages[rhandpg].textFrames);
                                                myDoc.selection[0].remove();
                                                • 21. Re: CS3 JS flag which page has empty frame
                                                  John.Kordas Level 1
                                                  Thanks guys I finally got it:

                                                  myPages[rhandpg].textFrames.item(0).remove();