5 Replies Latest reply on Apr 4, 2016 11:02 AM by Silly-V

    Clipping mask interferes with position?

    Sturm359 Level 1

      I've got a script that takes a piece of artwork and groups it with a rectangle that represents the maximum size for the artwork. It then takes that group--let's call it artGroup--and pastes it into another document, lining it up with another rectangle of the exact same size in that document. We'll call this rectangle in the target document targetArea.

       

      Now, normally this is not a problem. I simply set artGroup's position to the same as targetArea's position, thusly:

       

      artGroup.position = targetArea.position;
      

       

      This works in most cases. The rectangles in both artGroup and targetArea line up precisely. Now, I'm aware that .position is actually the coordinates of the top-left corner of the object in question. Since both rectangles are exactly the same dimensions, this is usually not a problem. But there are times when the artwork has a clipping path and the objects inside the clipping path are too big for the "maximum size" rectangle (thus, the need for us to clip it to a smaller area).

       

      When such artwork is placed by my script, it reads artGroup.position as the absolute top-left corner, including the large artwork inside the clipping path. This makes the artwork off-center, as you can see here:

       

      Screen Shot 2016-03-24 at 3.39.35 PM.png

      As far as I can tell, I cannot use .geometricBounds, either, as it also includes the clipped artwork. What can I do?

        • 1. Re: Clipping mask interferes with position?
          Silly-V Adobe Community Professional

          Really quickly size up your active or temp-made artboard to the selection (which is going to be your clipping bounds) and take measurements of that artboard. Then you can re-fit the used artboard back to its place, or if it was a temp artboard, delete it.

          • 2. Re: Clipping mask interferes with position?
            Sturm359 Level 1

            Finally got around to this one again after putting out some other fires.

             

            The selection you see in the image in my initial post was just to make the rectangle that doesn't have a stroke nor fill visible, so you could see how offset it was from the target rectangle (the one with the black stroke). When the script is running, it won't actually be "selecting" anything.

             

            I could make it do so, sure, but how would that help? AFAIK, there is no way to get the current selection's bounds, position, size, etc. It doesn't exist as a class instance with properties and methods.

             

            I went ahead and tried your suggestion anyway, and had the script create a temporary artboard at the same coordinates as the artGroup's .geometricBounds. As expected, however, it was larger than that blank rectangle (well, either of them, really, since they are the same size), because the clipped artwork was affecting the artGroup's .geometricBounds. Any other ideas?

            • 3. Re: Clipping mask interferes with position?
              Silly-V Adobe Community Professional

              No, you'd use the Fit to Artwork Bounds command to fit the new artboard to the selection.

              • 4. Re: Clipping mask interferes with position?
                Sturm359 Level 1

                Silly-V wrote:

                 

                No, you'd use the Fit to Artwork Bounds command to fit the new artboard to the selection.

                 

                Perfect! It took me a while to figure out that the Document object is the one that has the .fitArtboardToSelectedArt(); method. After some playing around with it, that got it to work! The final result:

                 

                // Copy the art group.
                var copiedGroup = allGroup.duplicate(workLayer, ElementPlacement.PLACEATEND);
                
                
                // Position the art group.
                // copiedGroup.position = destinationFrame.position;
                // Above method does not always work, so it has been commented out.
                // To account for art that contains clipping masks whose clipped artwork
                //   falls outside the imprint area, we must use the "Artboard" method:
                copiedGroup.selected = true;
                var tempArtBoard = workDoc.artboards.add(copiedGroup.geometricBounds);
                // This new, temporary artboard is the second one, so it's index is 1.
                workDoc.fitArtboardToSelectedArt(1);
                // Using a temporary artboard as an intermediary. Set its boundary
                //   to that of the selected artwork. Then get its difference from
                //   the target frame. That's how much we need to move the copied art.
                var offsetX = targetFrame.geometricBounds[0] - tempArtBoard.artboardRect[0];
                var offsetY = targetFrame.geometricBounds[1] - tempArtBoard.artboardRect[1];
                copiedGroup.translate(offsetX, offsetY);
                // Then do some clean up.
                tempArtBoard.remove();
                workDoc.selection = null;
                

                 

                Thanks so much for your help, Silly-V!

                • 5. Re: Clipping mask interferes with position?
                  Silly-V Adobe Community Professional

                  Glad it's helped you!