4

Does anybody know of a script that could calculate percentages within an InDesign document? To clarify - I want to use this essentially as a page number: page 5 of 10 would be 50%, for example.

If no script exists, which I'm thinking might happen as this is a pretty niche request, does anyone have any suggestions as to how to do this that's not completely manual? This document is going to be pretty large, so I'm not sure if doing them by hand will be an option. Thanks in advance!

1
  • Hi. I can't think of any solution that wouldn't involve scripting. You might want to ask a professional scripter to write it for you. Doesn't seem to hard to script, could be cheap enough...
    – Vinny
    Apr 8, 2019 at 7:43

1 Answer 1

4

Not gonna lie... I spent way too much time on this, but here we are...

This should be close enough (Instructions at the top of the script):

// Version.1.0.
// Page Numbers To Percentages.jsx
// https://gist.github.com/joonaspaakko/6628e2b6252d622b1b7f7eba601f6c91

// You may want to save before running the script. Every single
// time you run the script, all text frames that are inside a
// master page or nested master pages will be torn out (overridden).

// The script assumes the starting point is this:
// 1. You have a designated layer for the page numbers (Layer name defined below↓).
// 2. The layer should only contain one empty textframe per page. The text frame could contain text too, but it will be overweritten so it doesn't really matter.
//    - The text frame can be inside a master page or a regular page.


var layerName = "Page Numbers";
var basedOn = 1; // 0 = Puts back the current page number, 1 = The numbered pages, 2 = All pages
var percentage_prefix = '';
var percentage_suffix = '%';

if ( app.documents.length > 0 ) {

    var doc = app.activeDocument;
    var targetLayer = null;
    var layer = doc.layers.itemByName( layerName );
    var numberItems = prepareNumberOverride( doc, layer );
    replacePageNumbers( layer, numberItems );

}


function prepareNumberOverride( doc, layer ) {

    var collect_items = [];

    for ( var pageIndex=0; pageIndex < doc.pages.length; pageIndex++ ) {

        var page = doc.pages[pageIndex];
        var nPageItems = page.textFrames;
        var mPageItems = page.masterPageItems;


        var pageItemAdded = false;
        for ( var nPageItemIndex=0; nPageItemIndex < nPageItems.length; nPageItemIndex++ ) {
            var textFrame = nPageItems[ nPageItemIndex ];
            if ( textFrame.itemLayer == layer ) {
                collect_items.push( textFrame );
                pageItemAdded = true;
            }
        }

        if ( !pageItemAdded ) {
            for ( var mPageItemIndex=0; mPageItemIndex < mPageItems.length; mPageItemIndex++ ) {

                var item = mPageItems[ mPageItemIndex ];
                if ( item.itemLayer === layer ) {

                    var text = item.contents;

                    item.allowOverrides = true;
                    if ( item.allowOverrides == true ) {
                        var newItem = item.override( page );
                        collect_items.push( newItem );
                    }

                }

            }
        }

    }

    return collect_items;

}

function replacePageNumbers( layer, numberItems ) {

    var numberItemsLength = numberItems.length;

    for ( var i=0; i < numberItems.length; i++ ) {

        var numberItem = numberItems[i];
        var currentPageNumber = numberItem.parentPage.name;

        // Based on all number pages...
        if ( basedOn == 1 ) {
            var percentage = Math.floor( ((i+1) / numberItemsLength) * 100 );
        }
        // Based on all pages...
        else if ( basedOn == 2 ) {
            var percentage = Math.floor( (currentPageNumber / app.activeDocument.pages.length) * 100 );
        }

        numberItem.contents = basedOn == 0 ? currentPageNumber : percentage_prefix + percentage + percentage_suffix;

    }

}
4
  • Ah! Cheaper than I thought... Happy Josh ^^ Nice stuff Joonas
    – Vinny
    Apr 8, 2019 at 14:18
  • This is fantastic, thanks so much! Essentially I need to create a layer with a textbox and a fresh, empty master with said textbox — correct? I just need to inherit the % master onto my current master to be good, I'm hoping? Apr 8, 2019 at 23:32
  • That should work. I kinda explained it more complicated than I needed to. If your starting point is that you already have page numbering in various master pages, all you have to do is make sure those page number text frames are on the designated layer and that should do it. You can keep running the script as you make changes, but it does have the small caveat where each time it inputs percentages to a new text frame, the text frame is ripped from the master page and put on the regular page. I didn't check if the script works if the layer is locked, but you probably want to keep it locked...
    – Joonas
    Apr 8, 2019 at 23:47
  • @JoshKrauth-Harding, I did test it now and yes it does work with the layer locked, which I would definitely recommend. That reminded me of one thing, which is kinda obvious but easy to overlook. Let's say you change the master page of a page with a percentage... Since the percentage text frame is not in the master page anymore, it will stick around until you unlock the layer and delete it (if that is what you want).
    – Joonas
    Apr 9, 2019 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.