Skip to main content
johns32458848
Participant
December 8, 2017
Answered

Script to copy Group of layers without adding word "copy"

  • December 8, 2017
  • 6 replies
  • 2572 views

When you make a copy of a group using duplicate(), the word "copy" is added at the end of every layer inside the group and its subgroups.  Is it possible to make duplicate() not add "copy" at the end of layer's name. Disabling "Add copy to copied Layers and Groups" in layer panel settings doesn't affect the running of script.

I came up with recursive solution that removes copy from name, but it runs extremely slow on a large file with hundreds of layers. Are there more elegant solutions.

Structure before duplicating:

+Group1

      +Subgroup

          -Layer1

          -Layer2

Structure after duplicating:

+Group1 copy

     +Subgroup copy

          -Layer1 copy

          -Layer2 copy

+Group1

     +Subgroup

          -Layer1

          -Layer2

var original  = app.activeDocument.layerSets.getByName("group1");

app.activeDocument.activeLayer = original;

var copy = source.duplicate();

recFixName(copy);

// Recursively removes copy from name

function recFixName(curr) {

  var str = curr.name;

  var target = "copy";

  var match = 0;

  var pos = 0;

  var i = 0;

  while(i < str.length) {

  if(str == target[match]) {

  match++;

  pos = i;

  }

  i++;

  }

This topic has been closed for replies.
Correct answer r-bin

// takes into account not add "copy" at the end of layer's name

executeAction( stringIDToTypeID("copyToLayer"), undefined, DialogModes.NO );

6 replies

Jarda Bereza
Inspiring
December 8, 2017

Here is reading value

getAddCopyToLayerNames ();

function getAddCopyToLayerNames (){

var ref = new ActionReference();

ref.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "addCopyToLayerNames" ) );

ref.putEnumerated( charIDToTypeID( "capp" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

var result = executeActionGet(ref).getBoolean(stringIDToTypeID( "addCopyToLayerNames" ));

alert(result);

}

Both works for me in CC2018

CS6 is very old. A lot of things are not working here.

Kukurykus
Legend
December 8, 2017

Well no matter then, reading value gives error as well in CS6

Jarda Bereza
Inspiring
December 8, 2017

here it is:

function setAddCopyToLayerNames (add){

    var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "addCopyToLayerNames" ) );

        ref.putEnumerated( charIDToTypeID( "capp" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

    desc.putReference( charIDToTypeID( "null" ), ref );

    desc.putBoolean(stringIDToTypeID( "addCopyToLayerNames" ), add)

    executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );

}

setAddCopyToLayerNames (false);

Kukurykus
Legend
December 8, 2017

It gives error. Is it theoretical code or there is some mistake, or maybe it's not possible in CS6 extended to make it?

Jarda Bereza
Inspiring
December 8, 2017

This is from spaces-adapter

/**

* PlayObject to set "add 'copy' to duplicated layer names" preference

*

* @param {boolean} add If true, duplicated layers will have "[layer name] copy" as their name

* @return {PlayObject}

*/

export function setAddCopyToLayerNames (add) {

    return new PlayObject("set", {

        "null": {

            _ref: [

                {

                    _ref: null,

                    _property: "addCopyToLayerNames"

                },

                referenceBy.current

            ]

        },

        "addCopyToLayerNames": add || false

    });

}

So it is possible to set this property. We just need to translate this into AM code.

Jarda Bereza
Inspiring
December 8, 2017

Layers panel > flyout menu > panel options > last checkbox

Kukurykus
Legend
December 8, 2017

Too bad it's not scriptable or it is?

Jarda Bereza
Inspiring
December 8, 2017

I don't know. It doesn't looks scriptable. Only thumbnail size in aplication class could be scriptable.

I found these stringIDs

r-binCorrect answer
Legend
December 8, 2017

// takes into account not add "copy" at the end of layer's name

executeAction( stringIDToTypeID("copyToLayer"), undefined, DialogModes.NO );

Kukurykus
Legend
December 8, 2017

Ah right, only not add 'copy' at end of duplicated layers. Hah, I did with SL the same, but then noticed there is copy in name of new layerSet, but forgot it may be so as he wants only copy wasn't part of new layers. Anyway, if you want completely same name versions both for layerSets and layers you can use that workaround

Legend
December 8, 2017

For layers, sometimes you need to do

app.activeDocument.selection.deselect ();

)

davescm
Community Expert
Community Expert
December 8, 2017

Hi

I've moved your post to the Photoshop Scripting forum where you are more likely to get help with your issue

Dave

Kukurykus
Legend
December 8, 2017

I didn't test it for all cases but that's only workaround what came to me now:

aD = activeDocument, doc = documents.add(), activeDocument = aD

function dpl(v1, v2) {

    v1.layerSets[0].duplicate(v2, ElementPlacement.PLACEATEND)

}

dpl(aD, doc), dpl(activeDocument = doc, aD)

doc.close(SaveOptions.DONOTSAVECHANGES)

You may also check recent topic about alike problem: Help With Ps Script: Copy Group to a New PSD