Skip to main content
Inspiring
January 30, 2018
Answered

Please Help, How to create a new document with the first open docs filename

  • January 30, 2018
  • 1 reply
  • 674 views

i have a load images to stack as layers button on my extension but it creates a new document with the name "New Document" i need it to be the name of the first open document, this is my code, how do i add the proper code into this?

function HRDStack(){

    function step1(enabled, withDialog) {

       var orig_ruler_units = app.preferences.rulerUnits;

       var orig_display_dialogs = app.displayDialogs;

       app.preferences.rulerUnits = Units.PIXELS; // set the ruler units to PIXELS

       app.displayDialogs = DialogModes.NO; // Set Dialogs off

       //var closeDocs  = prompt("Should documents be Close without saving?", "Yes");

       var canvasSize = new Array(); // what size canvas is needed

       canvasSize=getDocDementions(); // get max width and height of open documents

       app.togglePalettes(); // toggle off palette so Photoshop will not need to update them

       var doc = app.documents.add( canvasSize[0], canvasSize[1], 300, "New Document", NewDocumentMode.RGB, DocumentFill.TRANSPARENT ); // create a new document

       //for (var i=0;i<documents.length-1;i++) { // stack a layer for the open document into the new document

       for (var i=documents.length-2;i>-1;i--) { // stack a layer for the open document into the new document

          app.activeDocument = documents; // switch active document

          var layerName = app.activeDocument.name; // get document name

          app.activeDocument.selection.selectAll();           // select all

          try {app.activeDocument.selection.copy(true);} // copy composite of visible layers to clipboard

          catch (e) {try {app.activeDocument.selection.copy();} // copy composite of visible layers to clipboard

                     catch (e) {app.purge(PurgeTarget.CLIPBOARDCACHE);} // empty 

          }

          app.activeDocument.selection.deselect(); // deselect

          app.activeDocument = documents[documents.length-1]; // switch to newly created document

          try { app.activeDocument.paste(); } // paste on composite of visible layers

          catch (e) { app.activeDocument.artLayers.add(); } // add an empty layer

          app.activeDocument.activeLayer.name=layerName; // label layer with Document name

        }

       //if ( closeDocs == "Yes" ) closeAllButOne(); // close all opened document except the newly created document

       closeAllButOne(); // close all opened document

       app.togglePalettes(); // toggle user's palettes back on

       app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen 

       app.displayDialogs = orig_display_dialogs; // Reset display dialogs 

       app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings

    }

   

       function getDocDementions(){

           width=0;

           height=0;

           for (var i=0;i<documents.length;i++) { // look at open document sizes

              if (documents.width > width) width=documents.width;

              if (documents.height > height) height=documents.height;

           }

           return new Array(width, height); // return width and height

        }

      function closeAllButOne(){ // close all opened document except the newly created document

           while  (documents.length>1) {

              app.activeDocument = documents[0];

              activeDocument.close(SaveOptions.DONOTSAVECHANGES);

           }

        }

   

   app.activeDocument.suspendHistory ("Stack", "step1()");

};

This topic has been closed for replies.
Correct answer naeema79951470

ok I figured it out, issue was here var doc = app.documents.add( canvasSize[0], canvasSize[1], 300, "New Document", NewDocumentMode.RGB,

1 reply

Trevor:
Legend
January 30, 2018

Which app?

Inspiring
January 31, 2018

Photoshop CC2017

naeema79951470AuthorCorrect answer
Inspiring
January 31, 2018

ok I figured it out, issue was here var doc = app.documents.add( canvasSize[0], canvasSize[1], 300, "New Document", NewDocumentMode.RGB,