Skip to main content
Participant
September 15, 2017
Answered

Merge all layers with the same prefix

  • September 15, 2017
  • 1 reply
  • 1956 views

I am trying to figure out how to merge all layers with the same prefix and then use the prefix to name the new combined layer

The example would be as follows. An INDD file contains the following layers

  • FPO layer 1
  • FPO layer 2
  • SHELL random naming
  • SHELL another random name
  • SHELL A 3rd random name
  • Another Layers
  • More Layers

Upon running the script a dialog would ask me for a Prefix word, in this case i would type 'SHELL' and hit ok

the resulting layers would look as follows

  • FPO layer 1
  • FPO layer 2
  • SHELL
  • Another Layers
  • More Layers

Any help with this would be much appreciated!

Thanks

This topic has been closed for replies.
Correct answer S Hopkins

Here is a sample of layer merge in AppleScript

tell application "Adobe InDesign CC 2017"

  tell document 1

  set layerList to layers whose name begins with "SHELL"

  end tell

  if length of layerList > 1 then

  set mergeLayer to item 1 of layerList

  set remainingLayers to rest of layerList

  tell mergeLayer to merge with remainingLayers

  end if

end tell

and in ExtendScript:

var selArr = [];

var testit;

var layerName;

var docRef = app.documents[0];

var layerArr = docRef.layers.everyItem().name;

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

testit = layerArr.substr(0,5);

if (testit == "SHELL") {

     layerName = layerArr;

     selArr.push(docRef.layers.item(layerName));

  }

}

var mergeLayer = selArr.shift();

mergeLayer.merge(selArr);

1 reply

S HopkinsCorrect answer
Inspiring
September 15, 2017

Here is a sample of layer merge in AppleScript

tell application "Adobe InDesign CC 2017"

  tell document 1

  set layerList to layers whose name begins with "SHELL"

  end tell

  if length of layerList > 1 then

  set mergeLayer to item 1 of layerList

  set remainingLayers to rest of layerList

  tell mergeLayer to merge with remainingLayers

  end if

end tell

and in ExtendScript:

var selArr = [];

var testit;

var layerName;

var docRef = app.documents[0];

var layerArr = docRef.layers.everyItem().name;

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

testit = layerArr.substr(0,5);

if (testit == "SHELL") {

     layerName = layerArr;

     selArr.push(docRef.layers.item(layerName));

  }

}

var mergeLayer = selArr.shift();

mergeLayer.merge(selArr);

Participant
September 18, 2017

This is amazing, thankyou!

one final thing... if i wanted the final layer to end up as the word 'SHELL' how would i incorporate this into the ExtendScript. I believe by modifying the AppleScript as shown below i should get the result i am looking for, but i am not sure on the ExtendScript

tell application "Adobe InDesign CC 2017"

  tell document 1

  set layerList to layers whose name begins with "SHELL"

  end tell

  if length of layerList > 1 then

  set mergeLayer to item 1 of layerList

  set remainingLayers to rest of layerList

  tell mergeLayer to merge with remainingLayers

  set the name of mergeLayer to "SHELL"

  end if

end tell