• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

After duplicating layer, I cannot do anything to it with script

Community Beginner ,
Oct 20, 2015 Oct 20, 2015

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)

TOPICS
Actions and scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Beginner ,
Oct 20, 2015 Oct 20, 2015

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)

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2015 Oct 20, 2015

Copy link to clipboard

Copied

You omitted to define the variable "newLayer" anew after merging the Group.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 21, 2015 Oct 21, 2015

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)

}  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

Would

myLayers[0].name

not only work for this if the merged Group was the topmost Layer?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 21, 2015 Oct 21, 2015

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()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 21, 2015 Oct 21, 2015

Copy link to clipboard

Copied

LATEST

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);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines