Skip to main content
Inspiring
November 23, 2014
Question

InDesign Script select Page and Layer

  • November 23, 2014
  • 2 replies
  • 1458 views

Hi all

I need to select all graphics on a page, but only on a given layer.

myDocument.pages[myPageCounter].allGraphics.length

This works fine.

myDocument.pages[myPageCounter].layers.itemByName("[myLayerName]")

As soon as I try to bring in the layer into this I fail.

Appreciate any hints

Regards

Romano

Some more context...

==================

myLabelStyle = myDocument.paragraphStyles.item(myLabelStyleName);
mySwatch = myDocument.swatches.item(mySwatchName);
myLayer = myDocument.layers.item(myLayerName);
if (myConfirmation == true){

       

        for(var myPageCounter = 0; myPageCounter < myDocument.pages.length; myPageCounter ++){

            $.writeln("The current page counter = " + myPageCounter);           

            for(var myGraphicsCounterPerPage = 0; myGraphicsCounterPerPage < myDocument.pages[myPageCounter].layers.itemByName("[myLayerName]"), allGraphics.length; myGraphicsCounterPerPage ++){

                $.writeln("The current graphics counter per page = " + myGraphicsCounterPerPage);

                myGraphics = myDocument.pages[myPageCounter].allGraphics;           

                myAddLabel(myDocument, myGraphics[myGraphicsCounterPerPage], myGraphicsCounterPerPage, myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch, myLayer, myStoriesArray);

            }           

        }

}
This topic has been closed for replies.

2 replies

Jump_Over
Legend
November 23, 2014

Hi,

...

I need to select all graphics on a page, but only on a given layer.

...

Since layer.parent is a document (not a page) ==> better way is to set a goal like:

"I need to select all graphics on a LAYER, but only on a given PAGE".

So iterate through thisLayer.allPageItems and check if its parentPage property is equal to thisPage.

Finally do some job with pageItems which passed this filter through.

Jarek

Inspiring
November 23, 2014

did a bit more coding

I now get all the graphics on a given page, then test every single graphic if it is on my specific layer.

I place all graphics with the correct condition in an array and pass the array on to the next function

Romano

//=============================================================\\

function myAddLabels(myDocument, myLabelType, myLabelHeight, myLabelOffset, myLabelStyleName, mySwatchName, myLayerName){

    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 == "[object Rectangle]"){   //Check to make sure selection only includes rectangles

                myGraphics.push(mySelections.allGraphics[0]);

            }

            else{

                //alert("Objects other than graphics were selected!");

                //Nothing happens if you don't select at least one graphic

            }

        }

    }

    myLabelStyle = myDocument.paragraphStyles.item(myLabelStyleName);

    mySwatch = myDocument.swatches.item(mySwatchName);

    myLayer = myDocument.layers.item(myLayerName);

    if (myConfirmation == true){      

        for(var myPageCounter = 0; myPageCounter < myDocument.pages.length; myPageCounter ++){

            $.writeln("The current page counter = " + myPageCounter);  

            var myTest = myLayer.allGraphics.lenght;

            $.writeln("myTest = " + myTest);

            for(var myGraphicsCounterPerPage = 0; myGraphicsCounterPerPage < myDocument.pages[myPageCounter].allGraphics.length; myGraphicsCounterPerPage ++){

                $.writeln("===============");

                var myAllGraphics = myDocument.pages[myPageCounter].allGraphics;

                var myAllGraphicsLength = myAllGraphics.length;

                $.writeln("myAllGraphicsLength = " + myAllGraphicsLength);

                myGraphics = new Array();

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

                    if(myAllGraphics.itemLayer.name == myLayerName){   //Check to make sure selection only includes rectangles

                        myGraphics.push(myAllGraphics);

                    }

                    else{

                        //alert("Objects other than graphics were selected!");

                        //Nothing happens if you don't select at least one graphic

                    }

                }

            }

            var myLayersCounterPerPage = myDocument.layers.length;

            var myGiveMeTheLayersName = myLayer.name;

            $.writeln("myLayer = " + myLayer);

            $.writeln("myLayerName = " + myLayerName);

            $.writeln("myGiveMeTheLayersName = " + myGiveMeTheLayersName);

            $.writeln("The current layers counter per page = " + myGraphicsCounterPerPage);

            $.writeln("The current graphics counter per page = " + myLayersCounterPerPage);

            for(var myGraphicsCounterPerPageA = 0; myGraphicsCounterPerPageA < myGraphics.length; myGraphicsCounterPerPageA ++){

                myAddLabel(myDocument, myGraphics[myGraphicsCounterPerPageA], myGraphicsCounterPerPageA, myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch, myLayer, myStoriesArray);

            }

        }          

    }

}

//=============================================================\\

Participant
November 23, 2014

Have you created your layer?

I had this issue before and as long as there's nothing else on the layer, you could use:

var layerCount = myLayer.pageItems.length;