Need help with simple script to duplicate layers

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

aboyne

Need help with simple script to duplicate layers

Post by aboyne »

Hi all, It's been a while since I last visited (and I'm getting older) so I forgot some of the basics and would appreciate some help.

I want to write a simple script that duplicates a layer into four layers with addendums to the layer name.

Example:
existing layer is named ABC

I would like to end up with four layers with the same contents but named
ABC-A
ABC-B
ABC-C
ABC-D

It seems this will require a script instead of an action because I will need to use the original name as part of the new layer names and I can't use variables or cut/paste in actions.

I hope this made sense, and I hope it is as simple as it seems. I am happy to pay for the help.

Thanks so much,
Ed
txuku

Need help with simple script to duplicate layers

Post by txuku »

Bonjour


Is this appropriate?

Code: Select allapp.preferences.rulerUnits = Units.PIXELS;
app.displayDialogs = DialogModes.NO;
docRef = app.activeDocument;

//alert(docRef.activeLayer.name)
var NomCalc = docRef.activeLayer.name;

var extName =  new Array( "A", "B", "C", "D");

docRef.selection.selectAll();
//docRef.selection.copy();

try
{
  docRef.selection.copy();
  catreCalc();
}
catch(e) { alert("calque vide"); docRef.selection.deselect(); };

function catreCalc()
{
  try
  {
    for(i=0; i<4; i++)
    {
      docRef.paste();
      docRef.activeLayer.name = NomCalc + "." + extName;
    }
  }
  catch(e){};
}

tyr

Need help with simple script to duplicate layers

Post by tyr »

The only thing about copy/paste is that if you have a layer with transparency, paste will always put the content of the layer in the center of the document.
I would go with Layer Via Copy command:

Code: Select allapp.preferences.rulerUnits = Units.PIXELS;
app.displayDialogs = DialogModes.NO;
docRef = app.activeDocument;

var layerRef = docRef.activeLayer

var extName =  new Array( "A", "B", "C", "D");

for(i=0; i<4; i++)
{
    executeAction( charIDToTypeID( "CpTL" ), undefined, DialogModes.NO );
    docRef.activeLayer.name = layerRef.name + "-" + extName;
}

layerRef.remove()
aboyne

Need help with simple script to duplicate layers

Post by aboyne »

Wow, that was fast ... thank you so much txuku and tyr, it works great.

There is just one little tweak. I used TYR's script, but it leaves 5 layers - the 4 new renamed layers and the original layer.
I would like to have just the 4 renamed layers.

I tried to figure out how to do it myself by just renaming the first layer first and changing the loop to 3 for the additional layers.
But as you can tell by my writing this message ... I couldn't figure it out.

Any thoughts?

Mike, I made a donation to the site ... thanks for such a great site.

Ed
tyr

Need help with simple script to duplicate layers

Post by tyr »

aboyne wrote:There is just one little tweak. I used TYR's script, but it leaves 5 layers - the 4 new renamed layers and the original layer.
I would like to have just the 4 renamed layers.

That's weird!
I'm referencing the active layer in the beginning and then removing it in the end:
Code: Select allvar layerRef = docRef.activeLayer
[...]
layerRef.remove()

Are you sure you're using my script?
Here's my result:
txuku

Need help with simple script to duplicate layers

Post by txuku »

Right tyr !
aboyne

Need help with simple script to duplicate layers

Post by aboyne »

I am so sorry. I didn't grab that last line.
It works perfectly now. Thank you so much.