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

Auto Capture Layer Names

Community Beginner ,
Jan 08, 2013 Jan 08, 2013

Copy link to clipboard

Copied

I am trying to capture the layer names from an indesign document. The idea behind the script (when finished) is to create a seperate PDF off each layer in the document and naming that document based on the layer name. My script so far can capture the first and last layer name but I cannot get the layers.nextItem to work. I keep getting an Invalid Parameter.

Please can you advise me on what  am doing wrong - Thanks

var myDoc = app.activeDocument;

var myLayers = myDoc.layers.firstItem();

// Box shows first Layer Name

alert (myLayers.name);

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

      

   myLayers = myDoc.layers.lastItem();

// Box shows last layer name but I would like the .lastItem to actually work with .nextItem    

   alert (myLayers.name);

}

TOPICS
Scripting

Views

1.6K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jan 08, 2013 Jan 08, 2013

@elDeako – see the following snippets and read the annotations:

//The active document:
var myDoc = app.activeDocument;

//The layers collection of the active document:
var myLayers = myDoc.layers;

//Loop through the layers collection of the active document:
for(var i = 0; i < myLayers.length; i++)
{
	//alert for the name of the layer the loop is currently visiting:
	alert( myLayers[i].name );
};

 

If you only want to read out all the names of the layer in one go:

If you only want to read out al
...

Votes

Translate

Translate
Community Expert ,
Jan 08, 2013 Jan 08, 2013

Copy link to clipboard

Copied

@elDeako – see the following snippets and read the annotations:

//The active document:
var myDoc = app.activeDocument;

//The layers collection of the active document:
var myLayers = myDoc.layers;

//Loop through the layers collection of the active document:
for(var i = 0; i < myLayers.length; i++)
{
	//alert for the name of the layer the loop is currently visiting:
	alert( myLayers[i].name );
};

 

If you only want to read out all the names of the layer in one go:

If you only want to read out all the names of the layer in one go:

alert
(
	"Layer names of active document:"+"\r"+
	app.activeDocument.layers.everyItem().name.join("\r")
);

 

Uwe Laubender

( ACP )

 

IMPORTANT NOTE: Had to edit the code of the first script snippet because the code was damaged when this thread was moved over from the old InDesign forum to this new one in late 2019.

Votes

Translate

Translate

Report

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 Beginner ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

Thanks Uwe, I really appreciate your help.

Votes

Translate

Translate

Report

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 ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Hi, can you help me with my script? Ive been strugling to make this work. It supposed to prevent a user from exporting when a Layer Name doesnt match the specif Layers on the Document. 

 

#target indesign
#targetengine "session"
main();
function main(){
var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
function checkLayer(myEvent){
alert("Unwanted Layers is present:"+"\r"+app.activeDocument.layers.everyItem().name.join("\r"));
}
}
 
 

Votes

Translate

Translate

Report

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 ,
May 12, 2021 May 12, 2021

Copy link to clipboard

Copied

LATEST

Hi Joseph,

your script is working OK with InDesign 2021 on my Windows 10 Pro machine.

I'd suggest you open a new thread.

Make sure you mention "beforeExport" and event listener in your title.

 

FWIW: I had to edit the code in my answer that was marked as "Correct".

 

Thanks,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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 ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

The reason you could not get nextItem to work is because in your script you wrote lastItem, not nextItem

nextItem returns the "next" item relative to an item you have to supply as its argument.

So, if your first layer is myLayers, the 'next' layer would be "myDoc.layers.nextItem(myLayers)":

var myDoc = app.activeDocument;

var myLayers = myDoc.layers.firstItem();

// Box shows first Layer Name

alert (myLayers.name);

for(var i = 0; i < myDoc.layers.length; i++)

{

   myLayers = myDoc.layers.nextItem(myLayers);

   alert (myLayers.name);

}

But this shows an error message! Why? Because of the way you set up the loop. 'myLayers' is initialized with 'the first layer', then you call 'nextItem' for each of the layers. With three layers:

myLayers = "A" -> nextItem makes mylayers "B", alert shows "B"

myLayers = "B" -> nextItem makes mylayers "C", alert shows "C"

myLayers = "C" -> nextItem sets mylayers to null, calling the alert pops up an error because 'null' is not a valid layer

When using this loop, you would never see an alert for layer "A" -- you only do so because you actually did so before the loop. So a better script would be this:

var myDoc = app.activeDocument;
var myLayers = myDoc.layers.firstItem();

for(var i = 0; i < myDoc.layers.length; i++)
{
   alert (myLayers.name);
   myLayers = myDoc.layers.nextItem(myLayers);
}

You don't do stuff outside of the loop, and you still get one alert for each layer.

But in fact there is no reason to use 'firstItem', 'nextItem', and 'lastItem' for this task. "Layers" are a simple array, ranging from 0 to (layers.length-1). You already know the 'relative' position of each layer -- it's the index number, and you iterate from first to last. So an even better loop to process each separate layer is this:

var myDoc = app.activeDocument;

for(var i = 0; i < myDoc.layers.length; i++)
{
   alert (myDoc.layers.name);
}


'firstItem' and its cousins  are useful functions when you don't know the absolute positions of items, only a relative one. I don't have a good example of correct usage at hand, I steer well away from using these functions, because they work extremely slow. Every call to one of these functions makes your script examine the entire list of objects (in the case of your layers, it only works because that's not a very long list).

As Uwe shows, it's worth trying 'everyItem' -- but you only need to if you are sure InDesign doesn't already have the objects in an array, ready for you to loop over.

Message was edited by: [Jongware] Hah! All said and done I end up with the *exact same* 'better script' Uwe already showed you!

Votes

Translate

Translate

Report

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 Beginner ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

Thanks Jongware, I really appreciate the explanation. I am really just starting to learn about scripting and am grateful for every bit of help as it is a bit of a steep learning curve for me.

Votes

Translate

Translate

Report

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 ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

@elDeako – you can do you a big favor and "invest" in some books on ExtendScript with InDesign.

I recommend reading and working with:
"InDesign Automation using XML & JavaScript" by Grant Gamble.

It comes along with a lot of exercises and example files you can download…

For  DOM documentation download the CHM files provided by Jongware:

http://www.jongware.com/idjshelp.html

That will give you a very good start learning scripting…

Uwe

Votes

Translate

Translate

Report

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 Beginner ,
Jan 10, 2013 Jan 10, 2013

Copy link to clipboard

Copied

Thanks Uwe, I will definately invest in the book and have already bookmarked th Link.

Thanks again for the help from both of you.

Votes

Translate

Translate

Report

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