Skip to main content
Known Participant
July 10, 2014
Answered

Find multi layer names and alert

  • July 10, 2014
  • 1 reply
  • 726 views

Hi All,

I am trying to do export pdf using condition layer names, if this layer names mismatched should alert, else export pdf.

If only one layer it is working fine, when multi layers not working.

my code is always alert if the layer names are correct, what is wrong in my code, can any one correct.

help would be appreciate

ly = app.activeDocument.layers.everyItem().getElements();

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

        if (ly.name != "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables") {

alert ("Check Layer names"), exit();

}else {

var myJobOptionName = "[Press Quality]";

var myOutFolderPathName = "/Users/wleastudio/Desktop/Watched Folder/Out/";

var myPDFFilePath;

var InDJobOption = app.pdfExportPresets;

myPDFFilePath = myOutFolderPathName+"/"+app.documents[0].name.split(".indd")[0].split(".INDD")[0]+".pdf";

app.documents[0].exportFile (ExportFormat.pdfType, myPDFFilePath, false, myJobOptionName);

}

}

Thanks in advance

steve

This topic has been closed for replies.
Correct answer Chinnadk

Hi Chinna,

Your code running super, catching names correct, but one minor error if layers more than 4 also code is exporting PDF.

Steve


Hi Steve,

Try now.

Array.prototype.contains = function(k)

{

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

  {

        if(this === k)

        {

            return true;

        }

    }

  return false;

}

var _layers = [ "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables"];

var match = [];

var ly = app.activeDocument.layers.everyItem().getElements();

if(ly.length == 4){

    for (i=0; i<ly.length; i++)

    {

        if (_layers.contains(ly.name))

        {

                match.push(i);

            }

    }

}

else

{

        alert ("Layer count shouldn't be less or more than 4");

        exit();

    }

if(match.length == 4)

{

        var myJobOptionName = "[Press Quality]";

        var myOutFolderPathName = "/Users/wleastudio/Desktop/Watched Folder/Out/";

        var myPDFFilePath;

        var InDJobOption = app.pdfExportPresets;

        myPDFFilePath = myOutFolderPathName+"/"+app.documents[0].name.split(".indd")[0].split(".INDD")[0]+".pdf";

        app.documents[0].exportFile (ExportFormat.pdfType, myPDFFilePath, false, myJobOptionName);

}

else

{

        alert ("Check Layer names");

        exit();

    }

Regards,

Chinna

1 reply

Jump_Over
Legend
July 10, 2014

Hi,

Imagine it works, so

if a particular layer name matches your condition - code is exporting a file

if not ==> alert

It means you are going to export as many PDF as many layers matches a condition. Why?

But it does not work.

Wrong condition. You can not compare a string to a "list" of strings ("b" to "a","b","c"). It can "work" only for first element. A solution could be to create a string from each layers names, like:

goodNames = app.activeDocument.layers.everyItem().name.join();

to find out if THIS name mismatches goodNames use a String.search() method, like:

goodNames.search(currentName) == -1;

any other value than -1 means a name matches some part of goodNames.

So I am suggesting to change a conditioning procedure and reconsider an exporting goal (when to export and what)

Jarek

AsOneAuthor
Known Participant
July 11, 2014

Hi Jarek,

Your 1st point of understand is correct, if layer name matches - code is exporting a file, if not => alert.

The other one NOT export as many PDF as many layers matches, though if defied layer names match export only one PDF.

Thank you Jarek for your help and suggestion.

Jump_Over
Legend
July 11, 2014

Hi,

layers: "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables"

Regarding to your example - only only one layer from 4 listed can be present in a document?

If two or more consecutive layers match - code exports two or more PDFs and overrides previous one (same file name).

Jarek