7 Replies Latest reply on Oct 29, 2013 6:18 AM by Michael L Hale

    trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)

    barqyUser

      I have a multi-layer PSD file, and am selecting various layers so I can export to PNG.  I have some layers with small images, the rest of the canvas being transparent.

       

      I just want the non-transparent region (like what Ctrl-A, Ctrl-C, Ctrl-N, and Ctrl-V accomplishes).  The problem with doing that is that I have no way to know what the POSITION of this image is.

       

      So, I set the layers appropriatly in Photoshop CS6, then run a script:

       

      #target photoshop

      app.bringToFront();

      app.activeDocument.selection.selectAll();

      app.activeDocument.selection.copy(true); // want to grab all visible layers to clipboard

       

      var calcWidth = app.activeDocument.selection.bounds[2] - app.activeDocument.selection.bounds[0];

      var calcHeight = app.activeDocument.selection.bounds[3] - app.activeDocument.selection.bounds[1];

      var docResolution = app.activeDocument.resolution;

       

      var myNewDoc = app.documents.add(calcWidth, calcHeight, docResolution,undefined, NewDocumentMode.RGB,

      DocumentFill.TRANSPARENT);  // the selection is the size of the FULL image, not the active region of the selection.

       

      myNewDoc.paste();

        • 1. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
          Chuck Uebele Adobe Community Professional & MVP

          Try the below.  I don't know how to get the copy merged to work.  One of those things that's in the SDK, but can't seem to figure out.  A work around is to merge visible then copy those.  The last line of the code is commented out, but you can uncomment it so that you close your reference document and don't accidently save the merged layers.

           

          You would run into another problem in that when you paste, it will paste into the center of the new document.  So I added a translate part that will move the new layer back to where the original was.

           

           

          #target photoshop

           

          app.bringToFront();

           

          var docRef = activeDocument

           

          try{docRef.mergeVisibleLayers()}

          catch(e){}

           

          var moveAmt = docRef.activeLayer.bounds

           

          docRef.selection.selectAll();

           

          docRef.selection.copy(); // want to grab all visible layers to clipboard

           

          var moveAmt = docRef.activeLayer.bounds

           

          var calcWidth = docRef.selection.bounds[2] - docRef.selection.bounds[0];

           

          var calcHeight = docRef.selection.bounds[3] - docRef.selection.bounds[1];

           

          var docResolution = docRef.resolution;

           

           

           

          var myNewDoc = app.documents.add(calcWidth, calcHeight, docResolution,undefined, NewDocumentMode.RGB,

           

          DocumentFill.TRANSPARENT);  // the selection is the size of the FULL image, not the active region of the selection.

           

           

           

          myNewDoc.paste();

           

          var newLayerBounds = myNewDoc.activeLayer.bounds

           

          myNewDoc.activeLayer.translate (moveAmt[0]-newLayerBounds[0], moveAmt[1]-newLayerBounds[1])

           

          //docRef.close(SaveOptions.DONOTSAVECHANGES);

          • 2. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
            Chuck Uebele Adobe Community Professional & MVP

            Would have been better to post this to the scripting forum, as there are more folks who know what the issue the copy might be.

            [ admin moved it ]

            • 3. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
              barqyUser Level 1

              Thank you csuebele, I wasn't aware the copy merged was a problem, I'll kick over to using mergeVisibleLayers().  Infact, I'm setting the visible items anyway, so not a problem.

               

              So, there's a slight difference in what we've both been able to achieve, and what I want to achieve.

               

              Assume I have a 1000 x 800 pixel image, and near the bottom left is a black rectangle, at (100,600) and is (200,100)  wide/tall.  The rest of the image is transparent.

               

              What I'm wanting is to be able to script out a (200,100) pixel PNG file containing (only) the black rectangle.  This works perfectly with Ctrl-A-C,N,P with one exception: I don't know where the origin of the image is (in this case 100,600).

              • 4. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
                Chuck Uebele Adobe Community Professional & MVP

                Oh, in that case use the values from the merged layer's bounds to set your new document and get rid of the translate stuff.

                • 5. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
                  Michael L Hale Level 5

                  You could use action manager to do the ctrl-n ( new doc from clipboard ) part.

                   

                  function newDocFromClipboard() {
                      var desc = new ActionDescriptor();
                          var desc1 = new ActionDescriptor();
                          desc1.putString( stringIDToTypeID('preset'), "Clipboard" );
                      desc.putObject( charIDToTypeID('Nw  '), charIDToTypeID('Dcmn'), desc1 );
                      executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );
                  };
                  
                  app.activeDocument.selection.selectAll();
                  app.activeDocument.selection.copy(true); // want to grab all visible layers to clipboard
                  newDocFromClipboard();
                  var myNewDoc = app.activeDocument;
                  myNewDoc.paste();
                  if(myNewDoc.layers.length>1) myNewDoc.layers[1].remove();
                  • 6. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
                    barqyUser Level 1

                    I like this approach, as it doesn't flatten the original, and risk my doing something stupid to it (like saving)

                     

                    (to save myself, I captured the filename, and at the end of the script, closed the origional (not saving changes) and reopened it so I could do more exporting.

                     

                    Howeve, with this, I don't see how to get the x,y of the selection going to the clipboard.

                     

                    It's not app.activeDocument.selection.bounds[0]  / [1]

                     

                    Sure would be nice to have a app.activeDocument.selection.position array!

                    • 7. Re: trying to script copy-new file-paste (ctrl-a,ctrl-c,ctrl-n,ctrl-v)
                      Michael L Hale Level 5

                      I don't see how to get the x,y of the selection going to the clipboard.

                      In that case I would skip using the clipboard.

                      function newDocFromMergedLayers( docName ) {
                          var desc = new ActionDescriptor();
                              var ref = new ActionReference();
                              ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Frst') );
                          desc.putReference( charIDToTypeID('null'), ref );
                          desc.putString( charIDToTypeID('Nm  '), docName );
                          desc.putBoolean( charIDToTypeID('Mrgd'), true );
                          executeAction( charIDToTypeID('Dplc'), desc, DialogModes.NO );
                      };
                      newDocFromMergedLayers( "newDocName" );
                      var mergedBounds = app.activeDocument.activeLayer.bounds;
                      app.activeDocument.trim( TrimType.TRANSPARENT);
                      alert(mergedBounds[0]+' : '+mergedBounds[1]);