Skip to main content
Participant
April 23, 2021
Answered

Duplicate layer to multiple or all artboards

  • April 23, 2021
  • 2 replies
  • 9025 views

Is there a way to duplicate a layer ("Layer > Duplicate Layer") onto multiple or all artboards instead of duplicating the layer individually for each artboard?

 

This would save me a ton of time!

This topic has been closed for replies.
Correct answer c.pfaffenbichler

this is a great script' thank you very much (:

is there a way to change it to selected group instead of selected layer?

duplicate group to all other artboards


quote

this is a great script' thank you very much (:

is there a way to change it to selected group instead of selected layer?

duplicate group to all other artboards


By @Lital5E82
if (documents.length == 0) {return};
// get list of selected layers;
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("targetLayersIDs"));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var targetLayersIDs = executeActionGet(ref).getList(stringIDToTypeID("targetLayersIDs"));
if (targetLayersIDs.count != 1) {
    alert ("select exactly one layer");
    return
};
// get current layer’s id;
var layerID = getLayerId ();
// collect artboards;
var theArtBoards = collectArtBoards ();
if (theArtBoards.length == 0) {
    alert ("no artboards");
    return
};
// get active layer’s artboard’s id;
var theLayer = activeDocument.activeLayer;
var thePar = theLayer.parent;
while (thePar.parent != activeDocument) {
    var thePar = thePar.parent;
};
activeDocument.activeLayer = thePar;
var artboardID = getLayerId ();
activeDocument.activeLayer = theLayer;
// process artboards;
for (var m = 0; m < theArtBoards.length; m++) {
// if not on the original artboard;
if (theArtBoards[m][2] != artboardID) {
// =======================================================
    var desc17 = new ActionDescriptor();
        var ref6 = new ActionReference();
        ref6.putEnumerated( stringIDToTypeID( "layer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
    desc17.putReference( stringIDToTypeID( "null" ), ref6 );
    var idto = stringIDToTypeID( "to" );
        var ref7 = new ActionReference();
        ref7.putIndex( stringIDToTypeID( "layer" ), getLayerIndexFromID(theArtBoards[m][2])-1 );
    desc17.putReference( idto, ref7 );
    desc17.putBoolean( stringIDToTypeID( "duplicate" ), true );
    desc17.putBoolean( stringIDToTypeID( "adjustment" ), false );
    desc17.putInteger( stringIDToTypeID( "version" ), 5 );
        var list6 = new ActionList();
        list6.putInteger( 36 );
    desc17.putList( stringIDToTypeID( "layerID" ), list6 );
executeAction( stringIDToTypeID( "move" ), desc17, DialogModes.NO );
// reselect original layer;
selectLayerByID (layerID, false);
    }
};
};
////// collect layers with certain name //////
function collectArtBoards () {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
if (layerSet == "layerSectionStart") {
var artBoardRect = layerDesc.getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
var v01 = artBoardRect.getDouble(artBoardRect.getKey(0));
var v02 = artBoardRect.getDouble(artBoardRect.getKey(1));
var v03 = artBoardRect.getDouble(artBoardRect.getKey(2));
var v04 = artBoardRect.getDouble(artBoardRect.getKey(3));
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
// collect if the rect values are not identical:
if (v01 != v03 && v01 != v04) {theLayers.push([theName, theIndex, theID])}
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale and paul riggott //////
function selectLayerByID(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
        if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
        desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
    try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
////// get active layer’s id //////
function getLayerId () {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("layerID"));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
return executeActionGet(ref).getInteger(stringIDToTypeID("layerID"));
};
////// get active layer’s id //////
function getLayerIndexFromID (theID) {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("itemIndex"));
ref.putIdentifier( charIDToTypeID("Lyr "), theID ); 
//ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
return executeActionGet(ref).getInteger(stringIDToTypeID("itemIndex"));
};

2 replies

Participant
April 23, 2021

Thanks @c.pfaffenbichler . I did come across this.

 

I'm trying to duplicate Smart Objects for the most part. It seems this script copies and pastes; not duplicate. When the script is executed on a Smart Object, it pastes the selection on other artboards as pixels or raster images.

c.pfaffenbichler
Community Expert
Community Expert
April 26, 2021

Thanks so much @c.pfaffenbichler for doing this.

 

I tried executing the script and received this error


Could you please post a screenshot including the Layers Panel ar the time of the error? 

Or provide the file itself?