Skip to main content
Jacques Letesson
Participant
July 6, 2013
Answered

Modify width of rectangle based on page number in InDesign via Javascript

  • July 6, 2013
  • 1 reply
  • 4538 views

I have a rectangle called `pageBar` on my master page !

I would like to create a script that will automatically resize the width of the rectangle based on the page number.

Something like : `width = 1280 * (pageNumber / pageTotal)`

Any help would be much appreciated.

Thanks in advance,

J.

This topic has been closed for replies.
Correct answer Jacques Letesson

Hi Jacques,

This will do it.

for (var n = 1; n < 5; n++) app.activeDocument.pages[n-1].rectangles.add({fillColor: "Black", geometricBounds: [10,0, 20, (n*100) + "px" ]});

Trevor


Hi Trevor,

I did it like that...

var myDocument = app.activeDocument,

myPages = myDocument.pages,

totalPages = myPages.length,

currentPage = 1;

function getPercent(a) {

    b = (a / totalPages);

    return b;

}

for (var i=0,l=myPages.length; i<l; i++) {

        currentPage = i + 1;

        percent = getPercent (currentPage);

        $.write("Current page is : " + currentPage + " / Total page : " + totalPages + " / Percentage : " + percent + "%");   

        var myRectangle = myPages.rectangles.add({geometricBounds:[0, 0, 5, (800*percent)], fillColor:myDocument.colors.item("ProgressBarColor")});

}

Thanks for your input.

1 reply

Inspiring
July 6, 2013

What your script needs to do is walk through the document pages, look for the rectangle and essentially do the calculation you've written to size it.

To size it on any particular page, you'll need to override it to the page.

Rectangles do not have a width property. The easiest way to manage the width is to use geometricBounds or visibleBounds.

Is this document single sided? Sounds like it.

Whiich way should the width increase?

Dave

Jacques Letesson
Participant
July 6, 2013

Hi DaveSofTypefi,

Thanks for your answer !

Yeah the document is single sided.

I would like to set the width of the rectangle like this X * (page Number / total number of pages)...

Example :  Page 1 : 100px, Page 2 : 200px, Page 3 : 300px, Page 4 : 400px. (Document of 4 pages with a width of 400px).

Does it make sense ?

Cheers,

J.

Inspiring
July 6, 2013

What you say makes sense, but it doesn't answer my question: do you want it to get wider to the right, to the left or from the middle?

Are you trying to write this yourself and need guidance or are you hoping someone willl write it for you?

Dave