Search code examples
javascriptadobe-indesign

Get current page number of InDesign multi page document


I am trying to get the current page number of an InDesign page into a label. This is what I am using:

myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0] + '_'
    + app.activeWindow.activePage.name +'_'+myCounter;

The issue I am running into is that I am running this from a multi page document, and instead of using the current page number, its using the first page number of the document in all labels in each page.

Here is the code that I think I am having issues with:

function myAddLabel(myDocument, myGraphic, myCounter, myLabelType, myLabelHeight, myLabelOffset, myStyle, mySwatch, myStoriesArray){ 
    var myLabel;
    var myLink = myGraphic.itemLink;
    var myPasteFromClipboard = false;
    //Create the label layer if it does not already exist. 
    var myLabelLayer = myDocument.layers.item("Coded Layout"); 
    try{ 
        myLabelLayer.name; 
    } 
    catch (myError){ 
        myLabelLayer = myDocument.layers.add({name:"Coded Layout"}); 
    } 
    //Label type defines the text that goes in the label.
    switch(myLabelType){
    //File name
        case 0:
            myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' + app.activeWindow.activePage.name+'_'+padNumber(myCounter);
            break;

Now I believe that the actual issue is with in this part of the script, if i select all the items on a page and run the script it works the way i want it to work.

function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myStyle, mySwatch){ 
var myDocument = app.documents.item(0);
myStoriesArray = new Array();

if (app.selection.length == 0) // If nothing is selected apply caption to all graphics in the document
    {
        var myConfirmation = confirm("Add captions to all images in the document?", false, "LabelGraphics.jsx" );
        if (myConfirmation == true)
            {
                var myGraphics = myDocument.allGraphics;
                }
        }
    else    
        { // If graphics are selected, just add captions to the selected items, as long as they are rectangles(image frames)
        var myConfirmation = true;
        var mySelections = app.selection;
        myGraphics = new Array();

        for(i = 0; i < mySelections.length; i++){
                if(mySelections[i] == "[object Rectangle]"){   //Check to make sure selection only includes rectangles
                        myGraphics.push(mySelections[i].allGraphics[0]);
                        }   
                    else{
                        //alert("Objects other than graphics were selected!");
                        //Nothing happens if you don't select at least one graphic
                        } 
                } 
            } 

And what i need to do at this point is create a loop as you suggested to run though all pages from the first to last applying the label.


Solution

  • The property app.activeWindow.activePage.name only returns the "name" (which indeed is the current page number) for the one page that is visible in your current (active) InDesign window. Unless your code actively switches the layout window to show each page in turn, it will always return the same number (and if the page currently shown is the first page, then you'd get exactly what you described).

    To assign myLabel to each page number in an entire document, you need to loop over each of the document's pages:

    for (p=0; p<myDocument.pages.length; p++)
    {
         myLabel = myDocument.name.replace(/\D+/,'') + '_'
            + app.activeDocument.pages[p].name +'_'+myCounter;
         /* .. use myLabel here .. */
    }
    

    I removed the split commands because the regex already removes all of the characters that are not digits, and so there are no spaces or underscores left to split on.