Skip to main content
Participating Frequently
July 11, 2012
Question

How would I scale every object to some percent?

  • July 11, 2012
  • 1 reply
  • 4429 views

Hi,

I'm working on a script that creates elements, and occasionally changes the size of the page. I want to add a parameter to my Dialog box called "scale." It should be a percentage value, and it should simply scale all objects to that percentage.

The point of this parameter would be to accomodate InDesign Page size limit. The only way I can think of to implement this would be to multiply every single number I have by that percent. However, this seems like a lot of work, and it seems like the sub-optimal approach.

Is there some other way to implement this?

This topic has been closed for replies.

1 reply

Larry G. Schneider
Community Expert
Community Expert
July 11, 2012

One approach might be to group all the items on the page, scale the group and then ungroup.

Community Expert
July 12, 2012

And to refine this approach that Larry mentioned:

do the grouping of the objects layer by layer, so you will not have all the objects in one layer after ungrouping.

To start  all that, you do not work directly with the page object, but with the window object and set its activePage to the page you want. As a second ingredience of this recipe we have the "activeLayer" object of the window object.

var myDoc = app.documents[0];

var myActivePage = myDoc.windows[0].activePage = myPageThatIsChangedInSize;

//Loop through all the layers of "myDoc":

for(var n=0;n<myDoc.layers.length;n++){

    var myActiveLayer = myDoc.windows[0].activeLayer = myDoc.layers;

    //Case 1: there are more than 1 page items in the active layer:

    if(myActiveLayer.pageItems.length>1){

        //Unlock all pageItems:

        myActiveLayer.pageItems.everyItem().locked = false;

        //Add a group with all page items on the active layer:

        var myGroup = myActivePage.groups.add(myActiveLayer.pageItems.everyItem());

        //myGroup.scale();

        //myGroup.ungroup();

        };

    //Case 2: there is only 1 page item in the active layer and you cannot group 1 single item:

    if(myActiveLayer.pageItems.length==1){

        //No need to group something, just scale:

        myActiveLayer.pageItems[0].locked = false;

        //myActiveLayer.pageItems[0].scale();

        };

    };

As center of the scaling you have to use the center of your active page.
Caution: With that approach all objects on the pasteboard are also scaled.

Of course you can further refine the script and store the IDs of all locked pageItems in an array for locking again after scaling them.

Hope that helps…

Uwe

Trevor:
Legend
July 12, 2012

Uwe

Nice answer but won't you run into problems of grouping groups if there are groups already on the page, which is if I remember a bit complicated.

Trevor