Copy link to clipboard
Copied
Hey,
I start photoshop 2015 win7 64bit
I create a new file with 1 Iayer in 1 group.
I duplicate (with the script below) a layer then I can't operate on that layer.
If I try to read its name (in photoshop GUI its called "Group 1 copy") I get this error message:
The object "document 0" is not currently available.
If I try to remove it, I get this error message:
The requested action requires that the target document is the frontmost document.
I dont think Im doing anything wrong, although i do make mistakes, but this seems like a bug to me
Does anyone have a work around?
All im trying to do export layers, any tips on scripting generator to do this? i found generators panel UI code but not an easy way to tell it to export from script....
here is the code (start with a 1 new file with 1 layer in a Group 1)
function copy_flatten_layerset(layerset){
$.writeln("flatten:" +layerset)
app.activeDocument.activeLayer = layerset
layerset.visible = true
var newLayer = layerset.duplicate()
app.activeDocument.activeLayer = newLayer
$.writeln("flatten:parent:"+newLayer.parent)
if (newLayer.typename == "LayerSet") {
newLayer.merge()
}
return newLayer
}
var g = app.activeDocument.layerSets.getByName("Group 1")
var nl = copy_flatten_layerset( g )
try{
$.writeln("newLayer.name = "+nl.name)
} catch (e) {
$.writeln("error: "+e.message)
}
try{
nl.remove()
} catch (e) {
$.writeln("error: "+e.message)
}
$.writeln("number of documents: "+app.documents.length)
$.writeln("active doc = "+app.activeDocument.name)
Copy link to clipboard
Copied
here's a shorter version that reproduces the bug:
var g = app.activeDocument.layerSets.getByName("Group 1")
var newLayer = g.duplicate()
if (newLayer.typename == "LayerSet") {
newLayer.merge() // its the merge that breaks it.
}
try {
$.writeln("dup name = "+newLayer.name)
} catch(e) {
$.writeln(e.message)
}
Copy link to clipboard
Copied
You omitted to define the variable "newLayer" anew after merging the Group.
Copy link to clipboard
Copied
Hi Chris,
Try this code...
#target Photoshop
$.level=2;
app.bringToFront;
var docRef = app.activeDocument;
var myLSet = docRef.layerSets;
var myLayers = docRef.layers;
var g = docRef.layerSets.getByName(myLSet[0].name);
var newLayer = g.duplicate();
if (newLayer.typename == "LayerSet") {
newLayer.merge(); // its the merge that breaks it.
//alert(myLayers[0].name)
}
try {
$.writeln("dup name = "+myLayers[0].name);
} catch(e) {
$.writeln(e.message)
}
Copy link to clipboard
Copied
Would
myLayers[0].name
not only work for this if the merged Group was the topmost Layer?
Copy link to clipboard
Copied
c.pfaffenbichler is right, merge will create a new layer leaving old reference invalid. This is subtle but hugely important thing that one should not ever store layer references beyond that function because it's very hard to keep track how long they are current. Accessing old references unspecified and cause anything from a silent fail to exception. What you need is
newLayer = newLayer.merge()
Copy link to clipboard
Copied
Thanks c.pfaffenbichler and Matias Kiviniemi,
Now I understood....
-yajiv
Final Code...should...
var g = app.activeDocument.layerSets.getByName("Group 1");
var newLayer = g.duplicate();
if (newLayer.typename == "LayerSet") {
newLayer=newLayer.merge(); // its the merge that breaks it.
}
try {
$.writeln("dup name = "+newLayer.name);
} catch(e) {
$.writeln(e.message);
}