Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

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

Explorer ,
Jul 15, 2023 Jul 15, 2023

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. 

 

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2023 Jul 15, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 15, 2023 Jul 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? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2023 Jul 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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2023 Jul 16, 2023

Guide is just a special kind of a graphic object - and mostly the same rules apply like with any other object - but you can't anchor it in the text. 

 

So it has to be on a page and be on a layer - and when you hide a layer - all objects on this layer will be hidden as well. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2023 Jul 16, 2023
LATEST

@Robert at ID-Tasker said: "but you can't anchor it in the text. "

Well, actually you can anchor a guide to an insertion point by placing an idms file holding a guide.

No scripting needed for this. But the placed guide will be something like a "zombie" object. It's not really a guide anymore where objects will snap to.

 

This all aside:

 

@dane13860245 is missing a crucial piece of information, perhaps something that is counter-intuitive if you are used to work in the GUI only. Every object on a page and also every guide object has the property itemLayer. You have to use this property to assign the guide to a specific layer.

 

You can do this when using the guides.add() method on object page, spread or document.

You can do that also later after adding the guide to the page.

 

Example below where I use property itemLayer of the guide while adding it.

When using doc.guides.add() instead of a specific spread or a specific page the guide will be added automatically to the first spread of the document.

// Will add guide to the FIRST SPREAD in the active document:

app.activeDocument.guides.add
(
	{
		location : "10 mm" ,
		orientation : HorizontalOrVertical.VERTICAL ,
		
		fitToPage : true ,
		guideColor : UIColors.GOLD ,
		guideType : GuideTypeOptions.RULER ,
		
		itemLayer : app.activeDocument.layers.itemByName( "Guide Layer")
	}
);

 

Of course, there must be a layer present with the exact name "Guide Layer".

Look up specifics at the ExtendScript DOM documentation compiled by Gregor Fellenz:

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

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

 

If you are looking into method add() in the Guides link, you can see that the script above could be written also this way:

 

app.activeDocument.guides.add
(
	// First argument, the layer:
	app.activeDocument.layers.itemByName( "Guide Layer" )
	
	// Do not miss out the comma, that parts the two arguments:
	,
	
	// Second argument, all other properties written as an object:
	{
		location : "20 mm" ,
		orientation : HorizontalOrVertical.VERTICAL ,
		
		fitToPage : true ,
		guideColor : UIColors.GOLD ,
		guideType : GuideTypeOptions.RULER
	}
);

 

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2023 Jul 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});
    }
}

 

 

 

Screen Shot 3.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2023 Jul 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.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines