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

Script that if 1 layer is visible then 2 or 3 other layers are also visible?

Community Beginner ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

Basically if layer 1 is visible, then I want layer 2 or 3 to be also visible with while layer 1 is active. But the rest are hidden.

There was some explanation on the Adobe scripting guide pdf, but its not very clear.

Can anyone help? Thanks

TOPICS
Scripting

Views

1.1K

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
Participant ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

What if layer 1 is not visible, what are the conditions/states of the other layers?

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
Participant ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

Since layers work in reverse, here is my hack of script, sure someone else can do better.

var doc = app.activeDocument;

var layerArr = [];

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

    layerArr.push(i);

    doc.layers.visible = false;

}

doc.layers[layerArr[layerArr.length-1]].visible = true;

if(doc.layers[layerArr[layerArr.length-1]].visible) {

    for(var j = 0; j < 3; j++) {

        doc.layers[layerArr[layerArr.length-1]-j].visible = true;

    }

}

doc.activeLayer = doc.layers[layerArr[layerArr.length-1]];

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
Community Expert ,
Jun 01, 2019 Jun 01, 2019

Copy link to clipboard

Copied

myLayers = app.activeDocument.layers;

if (myLayers.item ('layer 1').visible) {

  myLayers.item ('layer 2').visible = true;

  myLayers.item ('layer 3').visible = true;

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

    if (!/layer [123]$/.test (myLayers.name)) {

       myLayers.visible = false;

    }

  }

}

For additional geekiness, replace lines 6-8 with this:

myLayers.visible = /layer [123]$/.test (myLayers.name);

P.

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
Community Expert ,
Jun 02, 2019 Jun 02, 2019

Copy link to clipboard

Copied

Hi David,

or do it this way:

var doc = app.documents[0];

var layerOne = doc.layers.itemByName("layer 1");

var layerTwo = doc.layers.itemByName("layer 2");

var layerThree = doc.layers.itemByName("layer 3");

if( layerOne.visible )

{

    doc.layers.everyItem().visible = false;

    layerOne.visible = true;

    layerTwo.visible = true;

    layerThree.visible = true;

};

Hm, now I wonder if the two layers that should be visible in tandem with "layer 1" are in any spacial relationship with "layer 1". E.g. Are always next to layer 1 in the layer stack. Stacked below or stacked above maybe?

FWIW: Reread your question.

What do you mean by "or" ? What is the criterion whether layer 2 or layer 3 should be visible together with layer 1 ?

Regards,
Uwe

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
Community Beginner ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

Hi thanks for the reply by everyone.

Let me clarify this a bit more. Basically I have a 1 page document with multiple Layers, roughly 10-12. Each layer has its own design/ad like a puzzle that fills the entire page. However Some layers are also stacked on each other in the same position because of different design.  When I export, I need to export roughly 14 different versions. To do this, theres a set of layers that act as a "Base" layer since these are the designs that will be the same on all exported versions. The rest of the layers are color coded or named accordingly, so when I export, I leave the base layers on, and turn on or off the other layers to create a combination of multiple versions.

I want to have a script that helps me export these versions with a click of button instead of constantly turning layers on and off because there's more margin for errors.

So I tried to do some basic scripting myself from the adobe guide but since its not complete theres alot of questions.

Here's something I was working on.

var myDocument = app.documents.item(0);

var myVersion, myLanguageCounter, myFileName;

var myFolder = Folder.desktop;

for(var myCounter = 0; myCounter < 3; myCounter ++){

    switch(myCounter){

        case 0:

                myVersion = "v1";

                break;

         case 1:

                myVersion = "v2";

                break;

         case 2:

                myVersion = "v3";

                break;

    }

    for(myLanguageCounter = 0; myLanguageCounter < myDocument.layers.length; myLanguageCounter ++){

            if((myDocument.layers.item(myLanguageCounter).name == myVersion )|| (myDocument.layers.item(myLanguageCounter).name == "Background")){

                myDocument.layers.item(myLanguageCounter).visible = true;

                myDocument.layers.item(myLanguageCounter).printable = true;

                }

            else{

                myDocument.layers.item(myLanguageCounter).visible = false;

                myDocument.layers.item(myLanguageCounter).printable = false;

            }

        }

        myFileName = myFolder + "/" + myVersion + ".pdf";

        myDocument.exportFile(ExportFormat.pdfType, File(myFileName));

    }

This seem to work by giving me 3 exported pdf with same base layer visible on all while having the layers "v1,v2,v3" separated into 3 pdfs, this was the basic code from adobe's guide.

The problem is, I need more than 1 layer,

So imagine,

        case 0:

                myVersion = "v1", "v4","v5","v2";

                break;

         case 1:

                myVersion = "v2", "v5";

                break;

         case 2:

                myVersion = "v3", "v5", "v1";

                break;

That means, the exported 3 pdfs, first one would have layer v1,v4,v5 and v2 turned on. the Second pdf would have v2,v5 etc.

But I can't for the life of me figure out how to add that. I've tried different ways to add it like above but does not work.

Also, the background layer that stays visible on all versions, I need to somehow have it be referring to the Layer color/highlight. right now its referring to the name, but since I have 5-7 layers I need it to be referring to the color so I don't have to name it individually.

Sorry for long post, but thanks for any help truely appreciate it

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
Guide ,
Jun 03, 2019 Jun 03, 2019

Copy link to clipboard

Copied

LATEST

Hi David,

Your problem is that v1, v2, v3... refer to Layer names while your need each PDF version to include a particular set of layers. So the notion of ‘version’ is somehow dimmed. What you want is to specify a map that tells e.g: “ pdf1 requires the layers v1, v2, v4, v5 ; pdf2 requires v2 and v5 ; etc ” You may of course assign the name “v1” to pdf1, “v2” to pdf2, but these “v1”, “v2” are semantically distinct from the layer names v1, v2, v3… so I think this adds confusion.

Regarding the Background layer and its associated brothers (based on the same color), an option is to identify that layer first then perform a lookup on the layerColor property. There is an assumption behind this: the Background layer color SHOULD NOT be used by other regular layers.

Here is a possible implementation:

// YOUR SETTINGS

// ---

const LAYER_MAP =

{

    pdf1:  ["v1", "v2", "v4", "v5"],

    pdf2:  ["v2", "v5"],

    pdf3:  ["v1", "v3", "v5"],

};

const COMMON_LAYER_NAME = "Background";

const OUT_FOLDER = Folder.desktop;

const exportAllVersions = function(/*Document*/doc,  fd,LS,names,t,k,i,re)

//----------------------------------

{

    // Presets.

    // ---

    const TOG =

    [

        { printable:false, visible:false },   // 0 <-> OFF

        { printable:true,  visible:true  },   // 1 <->  ON

    ];

    const PDF = +ExportFormat.pdfType;

   

    // Checkpoints.

    // ---

    fd = Folder(OUT_FOLDER);

    if( !fd.exists ){ alert("The output folder does not exist."); return; }

    // ---

    if( (!doc) || Document!==doc.constructor){ alert("No document."); return; }

    LS = doc.layers;

    // ---

    t = LS.itemByName(COMMON_LAYER_NAME);

    if( !t.isValid ){ alert("No `"+COMMON_LAYER_NAME+"` layer found."); return; }

   

    // Deactivate all and coerce into arrays.

    // ---

    (LS=LS.everyItem()).properties = TOG[0];

    names = LS.name;

    LS = LS.getElements();

   

    // Activate common layer(s) only. Color lookup.

    // ---

    k = callee.colorValue(t);

    for( i=LS.length ; i-- ; )

    {

        k == callee.colorValue(t=LS)

        && ( t.properties=TOG[1], LS.splice(i,1), names.splice(i,1) );

    }

    // Generate separate PDF versions based on LAYER_MAP.

    // ---

    for( k in LAYER_MAP )

    {

        if( !LAYER_MAP.hasOwnProperty(k) ) continue;

        re = RegExp( '^(?:' + LAYER_MAP.join('|') + ')$' );

        for( i=names.length ; i-- ; LS.properties = TOG[+re.test(names)] );

        ff = File(fd + '/' + k + '.pdf');

        doc.exportFile(PDF, ff);

    }

}

exportAllVersions.colorValue = function(/*Layer*/ly,  r)

//----------------------------------

{

    r = ly.getElements()[0].layerColor; // UIColorsEnum | [R,G,B]

    return ( r instanceof Array ) ? ((r[0]<<16)|(r[1]<<8)|r[2]) : +r;

};

// Go!

// ---

exportAllVersions(app.properties.activeDocument);

Best,

Marc

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