Selection bounds without excess transparency

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

izzy4505

Selection bounds without excess transparency

Post by izzy4505 »

When I make a selection in Photoshop that includes blank (completely transparent) pixels, then copy it, and then paste it, my pasted selection does not include the blank pixels... it only includes the copied image data.

I am trying to make a script that does the exact same thing as copying an already-selected area of an open document, clicking file, new, using the default image size which matches the clipboard size, paste, and save for web. So far I am getting the selection bounds before I create the new document, and creating the new document with those bounds. However it isn't the selection bounds I want... I want the square bounds of the image within the selection, without the blank pixels. Is this making sense?

Is there a way to get this sub-selection of a selection? Alternatively, is there a way to create a new image in the same way Photoshop creates the image when I click file, new?

Thank you for your time.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Selection bounds without excess transparency

Post by Mike Hale »

If I understand what you are asking call the function below after you make your selection. It will tighten the existing selection to exclude transparent pixels.

Code: Select allfunction intersectSelectionWithTransparency(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var ref1 = new ActionReference();
        ref1.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
    desc.putReference( charIDToTypeID( "With" ), ref1 );
   executeAction( charIDToTypeID( "Intr" ), desc, DialogModes.NO );
}
izzy4505

Selection bounds without excess transparency

Post by izzy4505 »

Thanks! That was exactly what I was looking for. Here is my "Save selection for web" script, in case anyone would like to use it:
Code: Select allfunction intersectSelectionWithTransparency(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var ref1 = new ActionReference();
        ref1.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
    desc.putReference( charIDToTypeID( "With" ), ref1 );
   executeAction( charIDToTypeID( "Intr" ), desc, DialogModes.NO );
}

app.displayDialogs=DialogModes.NO

var docRef=app.activeDocument
docRef.selection.copy(true)
selBounds=docRef.selection.bounds

var newDocRef=app.documents.add(selBounds[2]-selBounds[0], selBounds[3]-selBounds[1], 72, docRef.name + "-Piece", NewDocumentMode.RGB, DocumentFill.TRANSPARENT)
newDocRef.paste()
newDocRef.selection.selectAll()
intersectSelectionWithTransparency()
newDocRef.crop(newDocRef.selection.bounds)
app.displayDialogs=DialogModes.ALL
newDocRef.exportDocument(new File(), ExportType.SAVEFORWEB);
app.displayDialogs=DialogModes.NO
newDocRef.close(SaveOptions.DONOTSAVECHANGES)