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);

briann57175377
Inspiring
March 3, 2019

hi,

how do i make the search case insensitive? I tried the \regex\ construct. It doesn't work.

Shell, shell, etc

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);

briann57175377
Inspiring
March 3, 2019

You could convert the string being matched to lowercase and then match it with a lowercase representation of the search string. Like below

if (testit.toLowerCase() == "shell")

Or you could use the regex with case insensitive flag. Like below

if (testit.match(/shell/i))

-Manan


thank you Manan!

didn't know the syntax. It's perfect!