Skip to main content
Known Participant
July 15, 2023
Question

How do add a layer, and add guides to that layer?

  • July 15, 2023
  • 3 replies
  • 1270 views

I'd like to create a script that adds a layer to my actively selected indesign document. 

 

Something I'm unclear on: does each page in InDesign have its own layers? Or are the layers global across all pages? This will change the script slightly. 

 

The script I tried to get started is this

app.activeDocument.layer.add({name:"Guide Layer"});
 
but this didn't work. 

 

This topic has been closed for replies.

3 replies

Peter Kahrel
Community Expert
Community Expert
July 16, 2023

A layer can be provided as a parameter of the function:

 

d = app.documents[0];
d.pages[0].guides.add (
  d.layers.item('Layer 2')
);

 

 

P.

(Edit: I now see that @rob day does add a guide to a layer, though in a slightly different way.)

rob day
Community Expert
Community Expert
July 15, 2023

A practical example: makes a layer named Guides Layer, adds a new vertical guide at 1" on every page with the guides’ item layer set to Guides Layer:

 

 

 

 

var doc = app.activeDocument;
//add a layer named "Guide Layer" if it doesn’t already exist
var gl = makeLayer(doc, "Guide Layer");
//get all of the document parent/master pages
var mp = doc.masterSpreads.everyItem().pages.everyItem().getElements()
//add a new vertical guide to each master page at 1" in the gl layer
for (var i = 0; i < mp.length; i++){
    mp[i].guides.add({guideColor:UIColors.FIESTA, orientation:HorizontalOrVertical.VERTICAL, guideType:GuideTypeOptions.RULER, fitToPage:true, location:"1in", itemLayer:gl})
};   


/**
* Makes a new named Layer 
* @ param the document to add the layer 
* @ param layer name 
* @ return the new layer 
*/

function makeLayer(d, n){
    if (d.layers.itemByName(n).isValid) {
        return d.layers.itemByName(n);
    } else {
        return d.layers.add({name:n});
    }
}

 

 

 

rob day
Community Expert
Community Expert
July 15, 2023

Hi @dane13860245 , Layers are a document, not page, property. Guides get added to a page, not a layer

Known Participant
July 15, 2023

I appreciate your response but I'm getting information which contradicts what you suggested. When I create a new layer, then draw a guide via dragging from the ruler, the guide becomes hidden when I hide the layer, and becomes unhidden when the layer unhides. You're 100% sure guides aren't a property of a layer? 

rob day
Community Expert
Community Expert
July 15, 2023

A layer can have a collection of guides, but the guide’s parent is either a page or a spread.

 

If I try to add a guide to a layer object I’ll get an error, but if I add a guide to a page or spread object, the guide is successfully added. For example in this code the variable dl is the active layer and p is the active page—dl.guides.add() throws an error, but p.guides.add() does not. When I add the guide to a page I can specify which layer I want it on—the guide’s .itemLayer property:

 

 

 

//the active layer
var dl = app.activeDocument.activeLayer;

//trying to add a guide to the layer object will throw an error
try {
    dl.guides.add({orientation:HorizontalOrVertical.VERTICAL, location:1})
}catch(e) {
    $.writeln(e)
    //returns: Error: Cannot create an additional item. Operation not permitted on this object.
}  

//a guide can be added to a page—a guide‘s parent is a spread or page
//active page is page number 3
var p = app.activeWindow.activePage;
var ng = p.guides.add({orientation:HorizontalOrVertical.VERTICAL, guideType:GuideTypeOptions.RULER, fitToPage:true, location:1, itemLayer:dl})
$.writeln("New Guide added to Page " +p.name + " in layer named: " + ng.itemLayer.name)
//returns: New Guide added to Page 3 in layer named: Layer 1

 

 

If you want the guides to show on all of the document pages, add them to the Parent Spread with the guide’s .itemLayer property set to your layer named Guide Layer.

 

See the parent property here:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Guide.html