// This script makes a copy merged based on selection, calculate the right size // create a new doc based on the selection size, and paste it into the doc // written by Allen Jeng, Adobe Systems Inc., Photoshop QE Team // Amended by Peter Lewis - www.peteralewis.co.uk to handle transparent images and resize to image not selection. // Please note if you don't have any layers, you may get an error (as it can't copy merged), simply remove the "true" on line 16. // get the width and height of selection var calcWidth = app.activeDocument.selection.bounds[2] - app.activeDocument.selection.bounds[0]; var calcHeight = app.activeDocument.selection.bounds[3] - app.activeDocument.selection.bounds[1]; // get the document resolution var docResolution = app.activeDocument.resolution; var docResolution = app.activeDocument.resolution; // copy merged app.activeDocument.selection.copy(true); // create a new doc based on selection size var myNewDoc = app.documents.add(calcWidth, calcHeight, docResolution, "New Image", NewDocumentMode.RGB, DocumentFill.TRANSPARENT); // paste in from clipboard myNewDoc.paste(); //### Trim the document to remove excess transparent area ### app.activeDocument.trim(TrimType.TRANSPARENT);