11 Replies Latest reply on Jan 26, 2015 3:12 AM by Stamm

    Is there ANY way to get the absolute bounds(including effects) of an object?

    Stamm Level 1

      I am currently working on a xml export script. I have stumbled across this problem:

       

      Our tool cant work with drop shadows, so I need a png for that purpose. So I am checking on a page item for applied effects(in our case only drop shadow) and if it returns true, it is exported as a png.

       

          function testEffects(object){

              if(object.transparencySettings.dropShadowSettings.mode === ShadowMode.drop)

                  return true;

              else

                  return false;

          }

       

      So far so good. The problem is, that the bounds of the object, doesn't reflect the size of the .png. A workaround is easy. Place the exported .png somewhere on the page, get the bounds of this, width and height are defined. The big problem now: the correct x and y are missing.

       

      If the shadow is on the bottom or/and right side (you could test this with the angle and the shadow offset) the x and y stay the same. But what if the shadow is all around the object? And moreover, a little more on the left than on the right. How can I possibly get the right x and y coordinates, so that everything is perfectly aligned?

       

      Does anyone have an idea on this?

       

      Best regards and thanks in advance

        • 1. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
          Laubender Adobe Community Professional & MVP

          @Stamm – I would group the page item with a rectangle which geometric bounds that exceeds the drop shadow effect and work with that group.

           

          Uwe

          • 2. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
            Stamm Level 1

            I think I understand what you mean, but I need the objects to be perfectly precise. There can't even be a difference of 0.05mm, as you would see this on print products. When you say "exceed the drop shadow effect" I guess you mean, that the rect is bigger than the rect with the shadow effect, but not precisely the same amount as the size of the .png, right?

            • 3. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
              Laubender Adobe Community Professional & MVP

              Right. But if you like to be precise, I think you have no chance in InDesign alone to calculate the right amount.

               

              Let me go into details here:

              My inital idea was to get the maximum dimensions and position of the "helper" rectangle (group dimensions) right. The relative position of the page item with the drop shadow within does not matter.

               

              1. Example of an page item that you have to export to PNG:

              1-PageItemWithEffect-DropShadow.png

              2. The exported PNG placed and aligned below the original page item:

              2-Placed-ExportedPNG.png

               

              As you can see, exporting to PNG (often, not always) is not a precise thing.

               

              3. A safe zone for my helper rectangle could be this like (no assumptions made of the kind of the effect, nore any calculations or read-outs of the properties of the drop shadow, like size, angle, x- and y-offsets).

               

              3-SaveZoneWithoutKnowingAnythingAboutTheEffect.png

               

              4. Another attempt to get a better result is calculating a clipping path from the placed PNG in InDesign. This method also would not be as precise as you like (add a saftey zone around the bounds of that clipping path). Maybe its very inprecise, because the effect is very subtile:

               

              4-TheBetterGuess-DoAClippingPath.png

               

              But it might be a better guess than before.

               

              And then we could do the following by the help of PhotoShop:

               

              1. Isolate the page item you want to export to a new layer.

              2. Make all other layers none-printing

              3. Export the whole page to PDF with printing objects only

              4. Render the PDF in PhotoShop to at least 300 ppi (or more, if you want more precission) with background transparency on.

              5. Do a selection of all drawn pixels in PhotoShop.

              6. Read out the size and position of the selection to InDesign

               

              For the PhotoShop part use BridgeTalk…

               

              Uwe

              • 4. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                Laubender Adobe Community Professional & MVP

                What I do not understand: Why do you need the precise bounds of the effect (plus the pageItem itself)?
                Is the pageItem with the drop shadow anchored? If it's not and you export the PNG with transparency set, it should make no difference. By calculating the helper rectangle, you always get the right postion of the grouped PNG relative to the page or the spread.

                 

                Uwe

                • 5. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                  Stamm Level 1

                  I'm not quite sure if we understand each other. The only thing I need is the exact x and y positions for the polygon/rectangle/ellipse with a drop shadow.

                   

                  Following example:

                   

                  Screen Shot 2015-01-09 at 14.00.17.png

                  (not 100% accurate). We are using different forms so we can easily adjust the shadows. Below the Content it looks like this:

                   

                  Screen Shot 2015-01-09 at 14.00.23.png

                   

                  So if the x and y of the object are SLIGHTLY offset, it will look like this:

                   

                  Screen Shot 2015-01-09 at 14.00.40.png

                   

                  I hope it's understandable now. The only thing I need it perfect x and y coordinates. As I said, if the shadow is just on 1 side, then it is no problem, as you can then align it on 1 side, but it needs to work with all kinds of shadows.

                   

                  Thank you

                  • 6. Re: Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                    Laubender Adobe Community Professional & MVP

                    @Stamm – I think, I did understand you from the beginning. :-)

                     

                    Here my proof of concept with a snippet that is working on a selected pageitem with a drop shadow. There are no checks about the kind of selection, if it's anchored (that will break the script) and other things. Tested with InDesign CS5.5 so the resulting PNG is always 72 ppi with transparency.

                     

                    If you like to test this snippet, save the snippet with an appropriate name before running, because the exported PNG will be exported to the folder where your script snippet file lies.

                     

                    1. Before executing the snippet:

                    BeforeExecutingSnippet-PageItemWithEffect-DropShadow.png

                     

                    2. After executing the snippet:

                    AfterExecutingSnippet-PlacedPNGatSamePosition.png

                     

                    Note 1: The original selected pageItem will be removed!

                    Note 2: In this scenario, you do not need to know the exact bounds of the effect.

                     

                    Note 3: The clue is: Export the selected object two times. The first time to get the safety zone. The second time grouped with a rectangle that is the size and position of the safety zone. Move the placed PNG to the position of the group. Remove the group.

                     

                    Note 4: I tested this snippet with InDesign CS5.5! In InDesign CS6 and above you can refine the export to a higher resolution and you have to turn ON transparency by purpose (that all in the pngExportPreferences of the app). In InDesign CS5.5 transparency is automatically set and you cannot change resolution (72 ppi only by default).

                     

                    Here the code:

                     

                    //Path for the exported file is where the script file lies
                    
                    main();
                    
                    function main(){
                    var myDoc = app.documents[0];
                    var myCurrentSpread = app.documents[0].layoutWindows[0].activeSpread;
                    
                    var whatToCheck = myDoc.selection[0];
                    var selGB = whatToCheck.geometricBounds;
                    
                    var _myScriptFile = new File($.fileName);
                    
                    var myIDAsString = String(whatToCheck.id);
                    var myDateString = String(Date.now());
                    
                    var myFileToExport = File(_myScriptFile.path +"/"+ myIDAsString +"-"+myDateString+".png");
                    whatToCheck.exportFile(ExportFormat.PNG_FORMAT, myFileToExport);
                       
                    app.select(null);
                    
                    myCurrentSpread.place(myFileToExport);
                    
                    var myPlacedObject = myCurrentSpread.pageItems.itemByID(returnMaxOfArray(myCurrentSpread.pageItems.everyItem().id)).graphics[0];
                    var myPlacedObjectGBs = myPlacedObject.geometricBounds;
                    var myWidth = myPlacedObjectGBs[3]-myPlacedObjectGBs[1];
                    var myHeight = myPlacedObjectGBs[2]-myPlacedObjectGBs[0];
                    
                    myFileToExport.remove();
                    myPlacedObject.parent.remove();
                    
                    var newRect = myCurrentSpread.rectangles.add({geometricBounds : [selGB[2]-myHeight,selGB[3]-myWidth,selGB[0]+myHeight,selGB[1]+myWidth]});
                    var newGroup = myCurrentSpread.groups.add([newRect,whatToCheck]);
                    newGroup.exportFile(ExportFormat.PNG_FORMAT, myFileToExport);
                    myCurrentSpread.place(myFileToExport);
                    
                    
                    
                    myPlacedObject = myCurrentSpread.pageItems.itemByID(returnMaxOfArray(myCurrentSpread.pageItems.everyItem().id));
                    myPlacedObject.move([ newRect.geometricBounds[1],newRect.geometricBounds[0] ]);
                    newGroup.remove();
                    };
                    
                    function returnMaxOfArray(myArray){
                        function numSortMaxToMin(b,a){return Number(a) - Number(b)};
                        return myArray.sort(numSortMaxToMin)[0];
                        };
                    

                     

                    Hope, that helps…

                     

                    Uwe

                    • 7. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                      Pickory Level 3

                      Hello,

                       

                      Very good Uwe.

                       

                      Stamm, if you want a very tight fitting PNG, I think you will have to write a plugin.

                       

                      P.

                       

                      Edited for this.

                      DropSadow.indd 1

                       

                      Screen shot 2015-01-10 at 19.23.17.png

                      • 8. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                        Stamm Level 1

                        Hey,

                         

                        wow, it works perfectly! As of now, I have NO clue at all, how it works, but I will figure it out and then include it into my script! This is perfect! Thank you really much!

                         

                        Best regards

                         

                        Fred

                        • 9. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                          Stamm Level 1

                          Hey Uwe,

                           

                          what normally is an easy practise, I just can't get it done. Your code works flawlessly, with one disadvantage: The pageItem ist not that the same index, as the old object. I have tried to move the new object, but I can't get it done. With

                           

                          var theIndex = whatToCheck.index;

                           

                          I get the index to which position I need to move my object. But I can't get the move function to work... I tried different parameters, but it wont work. Would you be so kind to tell me, how I can now move my new object to the same index as before? I tried different variations with

                           

                          myPlacedObject.move(LocationOptions.after/before, (tried 100 different things here));

                          • 10. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                            Laubender Adobe Community Professional & MVP

                            @Stamm – you could use the methods sentBack() or bringForward() of the Rectangle class to move the placed image container back and forward in the stack of page items.

                             

                            Uwe

                            • 11. Re: Is there ANY way to get the absolute bounds(including effects) of an object?
                              Stamm Level 1

                              Perfect. For everyone who is struggling with this, I used this now:

                               

                              theIndex = whatToCheck.index;

                               

                              //the rest of the code which Uwe posted

                               

                              if(theIndex > 0)

                                  for(i = myPlacedObject.index ; i < theIndex ; i = myPlacedObject.index)

                                      myPlacedObject.sendBackward();

                                 

                              This will send the placedObject to the previous position in the layer.