Error Pasting Into Multiple 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

SunMesa
Posts: 4
Joined: Wed Feb 16, 2022 12:57 am

Error Pasting Into Multiple Layers

Post by SunMesa »

Hello all, brand-newbie here, please bear with me...

I have a Photoshop document with (some large number of) multiple layers. I want to paste a selected portion of the contents of the topmost layer into all the other layers from a script. This is what I've tried:

Code: Select all

var idoc = app.activeDocument;
var sel = idoc.selection[0];
var layerCount = idoc.layers.length;
for (var a=1; a < layerCount; a++) {
    sel.duplicate(idoc.layers[a], ElementPlacement.PLACEATBEGINNING);
}
This produces the following error:
Error 21: undefined is not an object
Line: 5
-> {sel.duplicate(idoc.layers[a], ElementPlacement.PLACEATBEGINNING);
Does anyone know what I'm doing wrong? Thanks for any info!
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Error Pasting Into Multiple Layers

Post by Kukurykus »

You can't duplicate selected part of layer. You have to copy it, select desired layer where it has to be put on, and paste it.
SunMesa
Posts: 4
Joined: Wed Feb 16, 2022 12:57 am

Re: Error Pasting Into Multiple Layers

Post by SunMesa »

@Kukurykus,

Thanks for your reply, and apologies for my delayed response.

I waded through what little Photoshop CS5 scripting documentation I have as best I could, and came up with this revised script:

Code: Select all

var idoc = app.activeDocument;
var layerCount = idoc.layers.length;
idoc.layers[0].copy()
for (var a=1; a < layerCount; a++) {
    idoc.activeLayer = idoc.layers[a];
    idoc.paste();
}
This almost does what I'm after, but instead of pasting into the existing layers in the document (named "Layer 1", "Layer 2", ... "Layer N"), it creates N new layers (named "Layer N+1", "Layer N+2", ... "Layer 2N"), and pastes the selection into those instead of the existing layers!

I think I must be getting close, any additional guidance is much appreciated!
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Error Pasting Into Multiple Layers

Post by Kukurykus »

Code: Select all

(slctn = (aD = activeDocument).selection).copy(), slctn.deselect()
lrs = [].slice.call(aD.artLayers).splice(1); while(lrs.length)
	aD.activeLayer = lrs.pop(), aD.paste(),
	aD.activeLayer.merge()
SunMesa
Posts: 4
Joined: Wed Feb 16, 2022 12:57 am

Re: Error Pasting Into Multiple Layers

Post by SunMesa »

@Kukurykus,

Wow, that works! Can't say I understand it fully, but hey, I'm a Fortran programmer, and have only recently (and barely) touched my toes into javascript.

I hope I'm not getting greedy by asking, is there a way to do this as Paste-In-Place rather than just Paste? That is, replace the line
"aD.activeLayer = lrs.pop(), aD.paste(),"
in your script with
"aD.activeLayer = lrs.pop(), aD.pasteInPlace(),"
where pasteInPlace is a function defined somewhere in the script (as it evidently does not exist as a built-in function).

I've made a few attempts to do this, e.g., inserting the following immediately before your script (basically cut & pasted from a search of another forum):

Code: Select all

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function pasteInPlace(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putBoolean(sTID("InPlace"), true);
    desc1.putEnumerated(cTID('AntA'), cTID('Annt'), cTID('Anno'));
    executeAction(cTID('paste'), desc1, dialogMode);
  };
but this (and other attempts) always produces the error "aD.pasteInPlace() is not a function".

Thanks so much for all your help!
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Error Pasting Into Multiple Layers

Post by Kukurykus »

Code: Select all

(slctn = (aD = activeDocument).selection)
.copy(), slctn.store(chnnl = aD.channels.add())
lrs = [].slice.call(aD.artLayers).splice(1); while(lrs.length)
	aD.activeLayer = lrs.pop(), slctn.load(chnnl), aD.paste(),
	aD.activeLayer.merge(); chnnl.remove()
SunMesa
Posts: 4
Joined: Wed Feb 16, 2022 12:57 am

Re: Error Pasting Into Multiple Layers

Post by SunMesa »

@Kukurykus,

You're a wizard, that does precisely what I needed!

I must say it's quite terse and cryptic, at least for my level of experience, but I think I see basically what's going on. By running your script and examining the History window, I gather that it copies the selection, creates a new channel, saves the selection boundary, and then, for each layer i, loads the selection boundary, pastes the selection (creating a new layer), and merges the new layer with layer i. Perfect!

In the course of this exercise I discovered the xtools package, which has opened up a whole new world... thank you xbytor! Learning more every time I use it.

I don't see a button to flag this topic as 'Solved', but I'd click it if there was one. My sincerest thanks-
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Error Pasting Into Multiple Layers

Post by Kukurykus »

That's right. That is my style, compact coding.

This solution offers what you needed but not the way you wished it to get. It's a 'document object model' workaround for something that can't be done directly when using the simplest methods.

Ps. it seems only modern forum let for correct answer feature. This one is a copy of original in memory of a founder that passed away.