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

[JS] Finding layer and ignoring case

New Here ,
Sep 16, 2012 Sep 16, 2012

Copy link to clipboard

Copied

Hello,

I need to get pageItems from a specific InDesign layer with javascript.  The problem is, the layer name isn't always the same case.  For example, sometimes it's "artwork", or it can be "Artwork", or it can be anything in-between.

I found this works if it's all capped:

app.activeDocument.layers.item('ARTWORK').pageItems;

I was hoping there would be a way like so, but it doesn't work:

app.activeDocument.layers.item(/ARTWORK/i).pageItems;

Is the only way to loop through all the layers beforehand to find the correct character case?

Thanks.

TOPICS
Scripting

Views

762

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

Enthusiast , Sep 16, 2012 Sep 16, 2012

Hi,

layersstring = app.activeDocument.layers.everyItem().name.join('');

getIndex = layersstring.toUpperCase().indexOf('ARTWORK');

app.activeDocument.layers.itemByName(layersstring.slice(getIndex, getIndex+7)).pageItems;

should also work.

Guess my choice would be a 'normal' loop ....

Hope it'll work

Hans Gerd Claßen

Votes

Translate

Translate
Enthusiast ,
Sep 16, 2012 Sep 16, 2012

Copy link to clipboard

Copied

Hi,

layersstring = app.activeDocument.layers.everyItem().name.join('');

getIndex = layersstring.toUpperCase().indexOf('ARTWORK');

app.activeDocument.layers.itemByName(layersstring.slice(getIndex, getIndex+7)).pageItems;

should also work.

Guess my choice would be a 'normal' loop ....

Hope it'll work

Hans Gerd Claßen

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
New Here ,
Sep 17, 2012 Sep 17, 2012

Copy link to clipboard

Copied

LATEST

Hi Hans,

Thank you very much for your reply.

That's very clever, and definitely works.  Thanks for sharing.  🙂

I agree too - I'm thinking the loop would be best.  I ended up writing this:

---------------------------------------------

$.writeln(findLayer(app.activeDocument.layers, 'ARTWORK').name);

function findLayer(layerArray, capName) {

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

        var iLayer = layerArray;

        if (iLayer.name.toUpperCase() === capName) {

            return iLayer;

        }

    }

    throw ('"' + capName + '" layer not found!');

}

---------------------------------------------

Thanks!

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