Apply a selected layer to all layer comps

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

ChrisD

Apply a selected layer to all layer comps

Post by ChrisD »

I have a photoshop file with about 20 – 30 layer comps. If I want to add a new item to the design and have it persistent across all layer comps I have to make it visible within each layer comp and update that layer comp separately. This is fine if you just have a few layer comps but when it the number goes up it can be really tedious.

I'm looking for some kind of script that will allow me to do this. A simple method might be highlighting a layer (or number of layers) and saying "make the highlighted layer(s) visible across all layer comps" or even better "make the highlighted layer(s) visible across all highlighted layer comps".

Does anyone know haw to do this?

Any help would be so much appreciated!

Thanks in advance!

Chris

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Apply a selected layer to all layer comps

Post by Paul MR »

This will add the selected layer to all layer comps...

Code: Select all#target Photoshop

function main(){
if(!documents.length) return;
var newLayer = getSelectedLayersIdx()[0];
var doc = app.activeDocument;
for( var c = 0; c < doc.layerComps.length; c++ ){
   doc.layerComps[c].apply();
   makeActiveByIndex( Number(newLayer), true );
   doc.activeLayer.visible=true;
   doc.layerComps[c].recapture();
    }
}
main();
function makeActiveByIndex( idx, visible ){
    var desc = new ActionDescriptor();
      var ref = new ActionReference();
      ref.putIndex(charIDToTypeID( "Lyr " ), idx)
      desc.putReference( charIDToTypeID( "null" ), ref );
      desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};

function getSelectedLayersIdx(){
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var desc = executeActionGet(ref);
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
       desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
        var c = desc.count
        var selectedLayers = new Array();
        for(var i=0;i<c;i++){
          try{
             activeDocument.backgroundLayer;
             selectedLayers.push(  desc.getReference( i ).getIndex() );
          }catch(e){
             selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
          }
        }
     }else{
       var ref = new ActionReference();
       ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
       ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
       try{
          activeDocument.backgroundLayer;
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
       }catch(e){
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
       }
    }
    return selectedLayers;
};

and this will add the selected layer to selected layer comps...
Code: Select all#target Photoshop

function main(){
if(!documents.length) return;
var newLayer = getSelectedLayersIdx()[0];
var doc = app.activeDocument;
for( var c = 0; c < doc.layerComps.length; c++ ){
    if( doc.layerComps[c].selected == true ){
   doc.layerComps[c].apply();
   makeActiveByIndex( Number(newLayer), true );
   doc.activeLayer.visible=true;
   doc.layerComps[c].recapture();
        }
    }
}
main();
function makeActiveByIndex( idx, visible ){
    var desc = new ActionDescriptor();
      var ref = new ActionReference();
      ref.putIndex(charIDToTypeID( "Lyr " ), idx)
      desc.putReference( charIDToTypeID( "null" ), ref );
      desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};

function getSelectedLayersIdx(){
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var desc = executeActionGet(ref);
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
       desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
        var c = desc.count
        var selectedLayers = new Array();
        for(var i=0;i<c;i++){
          try{
             activeDocument.backgroundLayer;
             selectedLayers.push(  desc.getReference( i ).getIndex() );
          }catch(e){
             selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
          }
        }
     }else{
       var ref = new ActionReference();
       ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
       ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
       try{
          activeDocument.backgroundLayer;
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
       }catch(e){
          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
       }
    }
    return selectedLayers;
};
mycort

Apply a selected layer to all layer comps

Post by mycort »

Truly great idea for a script because it is a painful task of updating many layer comps if you want to have a specific layer or folder to be visible, but I have a question.

1) Is it possible to combine the two scripts below?

2) aside from visibility control to all layer comps, is it possible to also script Update all layer comps when something is moved? I'd like to move something and update many comp layers all at once. Right now, you have to update one layer comp at a time. also, if this is possible, it would be nice to have all these functionality on one script.

Thx
mycort

Apply a selected layer to all layer comps

Post by mycort »

Tried both script code snippets and I think I answered my own question of why you can't really combine these 2 scripts....unless there is a way to toggle this.

it seems like the selecting layer comps for update is the most useful, because you have the flexibility to select specific comps for visibility update or you can select all comps and update.

Aside from visibility control, Is it possible to update layer/folder "Position & Appearance(Blending Options)" too?
mycort

Apply a selected layer to all layer comps

Post by mycort »

anyone? this is a big help if someone has any time to give.......thx a lot.
mycort

Apply a selected layer to all layer comps

Post by mycort »

Hi Mike,

I talked to Paul who originally created this code snippet and he said that he didn't know layer comps well enough for this to support position and appearance. Just wondering if you or anyone else know if it's possible to control these 2 for layer comp updates.

thx.
Mike Hale

Apply a selected layer to all layer comps

Post by Mike Hale »

Sorry, no I don't have any suggestions either. I also don't use layer comps much and the couple of times I looked at them I seem to remember there was very little scripting support for working with layer comps.
mycort

Apply a selected layer to all layer comps

Post by mycort »

ah, I see. Aside from visibility, positioning and appearance are supported by layer comps....So I don't see why it can't be controlled.

What about a code addition like this:

Current visibility control:
doc.activeLayer.visible=true;


Proposed Code snippet for position and appearance:
doc.activeLayer.position=true;

doc.activeLayer.appearance=true;

What about running a script listener when you updated a layer comp for when a layer is moved or the blending option is adjusted.....then use the recorded log file to extract or alter the code to support for this?
Mike Hale

Apply a selected layer to all layer comps

Post by Mike Hale »

mycort wrote:ah, I see. Aside from visibility, positioning and appearance are supported by layer comps....So I don't see why it can't be controlled.

There are lots of things you can do in the Photoshop GUI that are not scriptable.

And I am not saying that it can be done. I said that I don't have any suggestions on how to do this.
mycort

Apply a selected layer to all layer comps

Post by mycort »

ah, got it.

what about running a script listener based on position and appearance update for the layer comp, see what code you comeback with and make adjustments that way to make it workable?