Skip to main content
any2cards
Known Participant
July 8, 2020
Answered

inDesign Script to Modify Width & Height values for all matching objects

  • July 8, 2020
  • 2 replies
  • 3895 views

I use inDesign to create cards for board games.  Each individual "page" is a card.  Each card has multiple objects on it.  Because each of my inDesign files can have hundreds if not thousands of pages (cards), it can be incredibly cumbersome if I want to say move the location of a object, as doing it manually can literally take hours.

 

In order to address this issue, I wrote the code below to automate moving an object at a specific location of x,y,w,h.  I prompt for this location, and then prompt for an x and y offset, allowing me to move all matching objects to a new location automatically.

 

Here is the code I am using:

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Set key variables to determine what objects are moved, by how much, and in what directions
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var xvalue = prompt("Enter X Value", "0");
x = parseFloat(xvalue);
var yvalue = prompt("Enter Y Value", "0");
y = parseFloat(yvalue);
var wdvalue = prompt("Enter W Value", "0");
wd = parseFloat(wdvalue);
var htvalue = prompt("Enter H Value", "0");
ht = parseFloat(htvalue);
var toMoveHorizontallyvalue = prompt("Enter X Move Value", "0");
toMoveHorizontally = parseFloat(toMoveHorizontallyvalue);
var toMoveVerticallyvalue = prompt("Enter Y Value", "0");
toMoveVertically = parseFloat(toMoveVerticallyvalue);
////////////////////////////////////////////////////////////////////////
/// Gather all spreads (pages) in indd file
////////////////////////////////////////////////////////////////////////
var myDoc = app.documents[0];
for(var p = 0; p < myDoc.pages.length; p++){
    var found = false;
    //////////////////////////////////////////////////////////////////////////////
    /// Move all text frames that match variables
    //////////////////////////////////////////////////////////////////////////////
    for(var t = 0; t < myDoc.pages[p].textFrames.length; t++){
        var frameGB = myDoc.pages[p].textFrames[t].geometricBounds;
        var height = parseFloat(frameGB[2]).toFixed(2)-parseFloat(frameGB[0]).toFixed(2);
        var width = parseFloat(frameGB[3]).toFixed(2)-parseFloat(frameGB[1]).toFixed(2);
        if(parseFloat(frameGB[0]).toFixed(2) == parseFloat(y).toFixed(2) && parseFloat(frameGB[1]).toFixed(2) == parseFloat(x).toFixed(2) && parseFloat(height).toFixed(2) == parseFloat(ht).toFixed(2) && parseFloat(width).toFixed(2) == parseFloat(wd).toFixed(2)){
            myDoc.pages[p].textFrames[t].move([frameGB[1]+toMoveHorizontally, frameGB[0]+toMoveVertically]);
            myDoc.recompose();
        }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////
    /// Move all GROUP text frames that match variables
    //////////////////////////////////////////////////////////////////////////////////////////////
    for(var g = 0; g < myDoc.pages[p].groups.length; g++){
        for(var t = 0; t < myDoc.pages[p].groups[g].textFrames.length; t++){
            var frameGB = myDoc.pages[p].groups[g].textFrames[t].geometricBounds;
            var height = parseFloat(frameGB[2]).toFixed(2)-parseFloat(frameGB[0]).toFixed(2);
            var width = parseFloat(frameGB[3]).toFixed(2)-parseFloat(frameGB[1]).toFixed(2);
            if(parseFloat(frameGB[0]).toFixed(2) == parseFloat(y).toFixed(2) && parseFloat(frameGB[1]).toFixed(2) == parseFloat(x).toFixed(2) && parseFloat(height).toFixed(2) == parseFloat(ht).toFixed(2) && parseFloat(width).toFixed(2) == parseFloat(wd).toFixed(2)){
                myDoc.pages[p].groups[g].textFrames[t].move([frameGB[1]+toMoveHorizontally, frameGB[0]+toMoveVertically]);
                myDoc.recompose();
            }
        }
    }
    /////////////////////////////////////////////////////////////////////////////////////////
    /// Move all rectangle frames that match variables
    /////////////////////////////////////////////////////////////////////////////////////////
    for(var r = 0; r < myDoc.pages[p].rectangles.length; r++){
        var frameGB = myDoc.pages[p].rectangles[r].geometricBounds;
        var height = parseFloat(frameGB[2]).toFixed(2)-parseFloat(frameGB[0]).toFixed(2);
        var width = parseFloat(frameGB[3]).toFixed(2)-parseFloat(frameGB[1]).toFixed(2);
        if(parseFloat(frameGB[0]).toFixed(2) == parseFloat(y).toFixed(2) && parseFloat(frameGB[1]).toFixed(2) == parseFloat(x).toFixed(2) && parseFloat(height).toFixed(2) == parseFloat(ht).toFixed(2) && parseFloat(width).toFixed(2) == parseFloat(wd).toFixed(2)){
            myDoc.pages[p].rectangles[r].move([frameGB[1]+toMoveHorizontally, frameGB[0]+toMoveVertically]);
            myDoc.recompose();
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// Move all GROUP rectangle frames that match variables
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    for(var g = 0; g < myDoc.pages[p].groups.length; g++){
        for(var r = 0; r < myDoc.pages[p].groups[g].rectangles.length; r++){
            var frameGB = myDoc.pages[p].groups[g].rectangles[r].geometricBounds;
            var height = parseFloat(frameGB[2]).toFixed(2)-parseFloat(frameGB[0]).toFixed(2);
            var width = parseFloat(frameGB[3]).toFixed(2)-parseFloat(frameGB[1]).toFixed(2);
            if(parseFloat(frameGB[0]).toFixed(2) == parseFloat(y).toFixed(2) && parseFloat(frameGB[1]).toFixed(2) == parseFloat(x).toFixed(2) && parseFloat(height).toFixed(2) == parseFloat(ht).toFixed(2) && parseFloat(width).toFixed(2) == parseFloat(wd).toFixed(2)){
                myDoc.pages[p].groups[g].rectangles[r].move([frameGB[1]+toMoveHorizontally, frameGB[0]+toMoveVertically]);
                myDoc.recompose();
            }
        }
    }
}

 

All of this works just fine.  What I have been trying to do now is two additional things, which I simply have found no way to accomplish, so I am asking for help here.

 

1) I want to prompt for a width and height offset (easy to do), and then change those values much like I change the x and y values for an object.  I can't figure out how to get this done - in other words, what is it that I should be trying to change, and how do I actually set the new values?

 

2) When I do change the width and height offsets successfully in number 1, it is usually (but not always) for image frames.  When it is for image frames, I want to also automatically set/force Fitting - Fill Content Proportionally.

 

Assume I can modify my code to prompt for the new width and height values.  How can I code for both 1) and 2) above?

 

Thanks in advance for any assistance provided.

This topic has been closed for replies.
Correct answer Peter Kahrel

You can set a frame's size using its geometric bounds. For example, to move a frame to 10,20 (y,x) and make it 40 deep and 60 wide, do this:

frame.geometricBounds = [10, 20, 50, 80];

(Geometricbounds set the top, left, bottom and right coordinates of a frame.)

 

To fit a rectangle's image to the resized rectangle, do this:

 

frame.fit (FitOptions.CONTENT_TO_FRAME);

 See any object-model browser for details on the fit() function.

 

P.

2 replies

Community Expert
July 8, 2020

Hi any2cards,

also see into this article by Marc Autret:

 

Work Around the Width/Height Gap
Marc Autret, October 17, 2009

https://www.indiscripts.com/post/2009/10/work-around-the-width-height-gap

 

Regards,
Uwe Laubender

( ACP )

any2cards
any2cardsAuthor
Known Participant
July 11, 2020

Uwe ... thank you for the link to the article.  It was extremely informative !

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
July 8, 2020

You can set a frame's size using its geometric bounds. For example, to move a frame to 10,20 (y,x) and make it 40 deep and 60 wide, do this:

frame.geometricBounds = [10, 20, 50, 80];

(Geometricbounds set the top, left, bottom and right coordinates of a frame.)

 

To fit a rectangle's image to the resized rectangle, do this:

 

frame.fit (FitOptions.CONTENT_TO_FRAME);

 See any object-model browser for details on the fit() function.

 

P.

any2cards
any2cardsAuthor
Known Participant
July 11, 2020

Peter ... thank you so much ! This does precisely what I want.