Skip to main content
G_Drey_V
Participating Frequently
August 28, 2017
Question

New document from layer

  • August 28, 2017
  • 2 replies
  • 1871 views

Hello, not sure if it's correct group but maybe someone know how exactly Photoshop 4-chars commands work.

I'm trying to find correct script to create new document from specific layer by id. But it always uses active layer. Is there any way to change the script to fit my requirements?

var mkId = charIDToTypeID("Mk  "),
       action = new ActionDescriptor(),
       nullId = charIDToTypeID("null"),
       actionRef = new ActionReference(),
       docId = charIDToTypeID("Dcmn");
    actionRef.putClass(docId),
    action.putReference(nullId, actionRef);

    var nmId = charIDToTypeID("Nm  ");
    action.putString(nmId, "My new document");
   
    var usingId = charIDToTypeID("Usng"),   
    // trying to set target layer, but it doesn't work
    c = new ActionReference();
    c.putIndex( charIDToTypeID( "Lyr " ), 2);

    action.putReference(usingId, c);
    var versionId = charIDToTypeID("Vrsn");
    action.putInteger(versionId, 5),
    executeAction(mkId, action, DialogModes.NO);

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
August 28, 2017

I do not understand what you mean by expanding the layer tree. Is that the Layers palette?  If you do not want to activate the reference layer. You could get the documents layers to get all the layers and then find the reference layer. Open a new document the with the same size and resolution as the current document. Add a new artlayer. Set the new layer to be the same as reference layer. Then delete the new document's bottom layer.  Could be done using DOM code action manager code would not be needed.

JJMack
G_Drey_V
G_Drey_VAuthor
Participating Frequently
August 28, 2017

Sorry, could you please provide some details how can I create new layer and make it the same as reference layer (or layers section). I wrote small script but it doesn't work:

function main() {

    var doc = app.activeDocument;

    var lr = doc.activeLayer; // replace it with getTargetLayer(doc, id);

     

    var newDoc = app.documents.add(doc.width, doc.height);

    var newLr = newDoc.artLayers.add();

    newDoc.activeLayer = lr; // this code causes exception 

}

JJMack
Community Expert
Community Expert
August 28, 2017

Sorry I only hack at Photoshop scripting and do not know javascript.. It seemed logical that you  would be able to open a new document the correct size and resolution. Add a new layer and set it with an artlayer from the original document.  However  it look like Adobe does not allow that.  I get a error  or the new layer added does not get set  with an assignment statement using the  atrlayer object from the original and remains empty.

function main() {

    var doc = app.activeDocument;

    var lr = doc.activeLayer; // replace it with getTargetLayer(doc, id);

 

    var newDoc = app.documents.add(doc.width, doc.height, doc.resolution);

    var newLr = newDoc.artLayers.add();

//    newDoc.activeLayer = lr;

//    newLr = lr;  // nothing get set no error message

   newLr.kind = LayerKind.TEXT;

   textColor = new SolidColor;

   textColor.rgb.red = 255;

   textColor.rgb.green = 0;

   textColor.rgb.blue = 0;

   newLr.textItem.color = textColor;

   newLr.textItem.font = "ArialMT";

   newLr.blendMode = BlendMode.NORMAL;

   newLr.textItem.fauxBold = false;

   newLr.textItem.fauxItalic = false;

   newLr.textItem.underline = UnderlineType.UNDERLINEOFF;

   newLr.textItem.capitalization = TextCase.NORMAL;

   newLr.textItem.antiAliasMethod = AntiAlias.SHARP;

   newLr.textItem.size = 20;

   newLr.textItem.position = Array(40, 40 );

   newLr.textItem.contents = "text";

   newLr = lr; // nothing get set no error message

}

main();

JJMack
JJMack
Community Expert
Community Expert
August 28, 2017

Duplicate layer duplicate the Active layer set the layer you want to create a new document with as the Active layer before using the code.

function NewDocFromActiveLayer(LayerName,DocName) {

var idMk = charIDToTypeID( "Mk  " );

    var desc54 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref9 = new ActionReference();

        var idDcmn = charIDToTypeID( "Dcmn" );

        ref9.putClass( idDcmn );

    desc54.putReference( idnull, ref9 );

    var idNm = charIDToTypeID( "Nm  " );

    desc54.putString( idNm, DocName );

    var idUsng = charIDToTypeID( "Usng" );

        var ref10 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref10.putEnumerated( idLyr, idOrdn, idTrgt );

    desc54.putReference( idUsng, ref10 );

    var idLyrN = charIDToTypeID( "LyrN" );

    desc54.putString( idLyrN, LayerName );

    var idVrsn = charIDToTypeID( "Vrsn" );

    desc54.putInteger( idVrsn, 5 );

executeAction( idMk, desc54, DialogModes.NO );

}

NewDocFromActiveLayer("llll","xxx");

JJMack
G_Drey_V
G_Drey_VAuthor
Participating Frequently
August 28, 2017

Thanks for reply! Really the point is to NOT use active layer but use layer reference instead.

Create new doc using active layer is exactly how the code I shared works

JJMack
Community Expert
Community Expert
August 28, 2017

I do not understand what is a layer reference.   If it is a layer in the document save the active layers in a variable set  the docment's active layer the layer reference  duplicate the active layer into a new document then restore the document active layer to the one you saved before.  If you want to us a layer you normally need to target the layer first.  Many Photoshop tools and commands work on or with the active layer.  If you look at the duplicate layer dialog the source layer  can not be set it is the current targeted active layer. You have to target the correct document not the  new document you opened that became the Acrive document set a varable  Doc to the original source document first. Set Doc.acticeLayer = the old saved active layer.

JJMack