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

Indesign Scripting: Loop through Layers

Participant ,
May 15, 2013 May 15, 2013

I am trying to make a script that will:

Loop through all layers of a document.

Add the unlocked layers to array

Loop through this array and paste in place on each layer.

I am not sure how to loop through all layers.

Here is my script (I have used ??? to represent where the loop through all layers part would go):

// JavaScript Document

//Set up an array to hold unlocked layers

var layercollection = [];

//Loop through Layers and unlocked layers to the layercollection array

for (i = 0; ? ? ? i++) {

          app.activeDocument.activeLayer = app.activeDocument.layers.itemByName( ? ? ? );

          if (app.documents[0].layers. ().locked) == false;

          layercollection.push(layers.itemByName())

}

// Loop through layercollection array and paste in place

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

          app.activeDocument.activeLayer = app.activeDocument.layers.itemByName(tl);

          app.pasteInPlace(); //Paste

}

Can someone help me complete my script.

Thanks!

TOPICS
Scripting
4.5K
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

correct answers 1 Correct answer

Community Expert , May 15, 2013 May 15, 2013

Did you all test your solutions in a Real World Scenario?

Theoretically it should work this way (if we neglect that a layer could be invisible, too), but even without the problem of  invisible layers app.pasteInPlace() prefers to paste into the original layer, if the option "Paste Remembers Layers" is checked.

So I would recommend to do the following:

var oldPasteRem = app.clipboardPreferences.pasteRemembersLayers;
//uncheck "Paste Remembers Layers"

app.clipboardPreferences.pasteRemembersLayers = fal

...
Translate
Community Expert ,
May 15, 2013 May 15, 2013

One loop is enough:

for (i = 0; i < app.activeDocument.layers.length; i++) {

    app.activeDocument.activeLayer = app.activeDocument.layers);

    app.pasteInPlace();

}

Peter

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 ,
May 15, 2013 May 15, 2013

Hi

Peter is right. You have add your condition in this code:

for (i = 0; i < app.activeDocument.layers.length; i++) {

    if(app.activeDocument.layers.locked == false) {

        app.activeDocument.activeLayer = app.activeDocument.layers;

        app.pasteInPlace();

    }

}

Suresh

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 ,
May 15, 2013 May 15, 2013

Did you all test your solutions in a Real World Scenario?

Theoretically it should work this way (if we neglect that a layer could be invisible, too), but even without the problem of  invisible layers app.pasteInPlace() prefers to paste into the original layer, if the option "Paste Remembers Layers" is checked.

So I would recommend to do the following:

var oldPasteRem = app.clipboardPreferences.pasteRemembersLayers;
//uncheck "Paste Remembers Layers"

app.clipboardPreferences.pasteRemembersLayers = false;

for (i = 0; i < app.activeDocument.layers.length; i++) {

    //Maybe the user does not want objects pasted to invisible layers:

    if(app.activeDocument.layers.visible == true && app.activeDocument.layers.locked == false) {

        app.activeDocument.activeLayer = app.activeDocument.layers;

        app.pasteInPlace();

    };

};

//Restore the old condition:

app.clipboardPreferences.pasteRemembersLayers = oldPasteRem;

Uwe

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 ,
May 15, 2013 May 15, 2013

Absolutely check for layers being locked and the state of the clipboard preferences. I was just concerned with the loops.

Peter

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
Participant ,
May 16, 2013 May 16, 2013
LATEST
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