// 2017, use it at your own risk; #target photoshop if (app.documents.length > 0) { var myDocument = app.activeDocument; var originalRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.PIXELS; myDocument.selection.deselect(); // magic wand selection; magicWandTool (10,10); magicWandTool (myDocument.width-10,10); magicWandTool (myDocument.width-10,myDocument.height-10); magicWandTool (10,myDocument.height-10); // invert selection; myDocument.selection.invert(); // layer via copy; var id14 = charIDToTypeID( "CpTL" ); executeAction( id14, undefined, DialogModes.NO ); var thisLayer = myDocument.activeLayer; // hide others; hideOthers (); // remove potential dirt; loadTransparency(false); myDocument.selection.contract(10); myDocument.selection.expand(15); myDocument.selection.invert(); myDocument.selection.clear(); myDocument.selection.deselect(); // trim with 4px edge; cropTo(thisLayer.bounds[0]-4, thisLayer.bounds[1]-4, thisLayer.bounds[2]+4, thisLayer.bounds[3]+4); // reset ruler; app.preferences.rulerUnits = originalRulerUnits; }; ////// function ////// function magicWandTool (theX, theY) { // ======================================================= var idslct = charIDToTypeID( "slct" ); var desc12 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref5 = new ActionReference(); var idmagicWandTool = stringIDToTypeID( "magicWandTool" ); ref5.putClass( idmagicWandTool ); desc12.putReference( idnull, ref5 ); /* var iddontRecord = stringIDToTypeID( "dontRecord" ); desc12.putBoolean( iddontRecord, true ); var idforceNotify = stringIDToTypeID( "forceNotify" ); desc12.putBoolean( idforceNotify, true );*/ executeAction( idslct, desc12, DialogModes.NO ); // ======================================================= var idAddT = charIDToTypeID( "AddT" ); var desc4 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref2 = new ActionReference(); var idChnl = charIDToTypeID( "Chnl" ); var idfsel = charIDToTypeID( "fsel" ); ref2.putProperty( idChnl, idfsel ); desc4.putReference( idnull, ref2 ); var idT = charIDToTypeID( "T " ); var desc5 = new ActionDescriptor(); var idHrzn = charIDToTypeID( "Hrzn" ); var idPxl = charIDToTypeID( "#Pxl" ); desc5.putUnitDouble( idHrzn, idPxl, theX ); var idVrtc = charIDToTypeID( "Vrtc" ); var idPxl = charIDToTypeID( "#Pxl" ); desc5.putUnitDouble( idVrtc, idPxl, theY ); var idPnt = charIDToTypeID( "Pnt " ); desc4.putObject( idT, idPnt, desc5 ); var idTlrn = charIDToTypeID( "Tlrn" ); desc4.putInteger( idTlrn, 36 ); var idAntA = charIDToTypeID( "AntA" ); desc4.putBoolean( idAntA, true ); executeAction( idAddT, desc4, DialogModes.NO );/* // ======================================================= var idsetd = charIDToTypeID( "setd" ); var desc2 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref1 = new ActionReference(); var idChnl = charIDToTypeID( "Chnl" ); var idfsel = charIDToTypeID( "fsel" ); ref1.putProperty( idChnl, idfsel ); desc2.putReference( idnull, ref1 ); var idT = charIDToTypeID( "T " ); var desc3 = new ActionDescriptor(); var idHrzn = charIDToTypeID( "Hrzn" ); var idPxl = charIDToTypeID( "#Pxl" ); desc3.putUnitDouble( idHrzn, idPxl, theX ); var idVrtc = charIDToTypeID( "Vrtc" ); var idPxl = charIDToTypeID( "#Pxl" ); desc3.putUnitDouble( idVrtc, idPxl, theY ); var idPnt = charIDToTypeID( "Pnt " ); desc2.putObject( idT, idPnt, desc3 ); var idTlrn = charIDToTypeID( "Tlrn" ); desc2.putInteger( idTlrn, 36 ); var idMrgd = charIDToTypeID( "Mrgd" ); desc2.putBoolean( idMrgd, true ); var idAntA = charIDToTypeID( "AntA" ); desc2.putBoolean( idAntA, true ); executeAction( idsetd, desc2, DialogModes.NO );*/ }; ////// hide others ////// function hideOthers () { // hide others; // ======================================================= var idShw = charIDToTypeID( "Shw " ); var desc2 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var list1 = new ActionList(); var ref1 = new ActionReference(); var idLyr = charIDToTypeID( "Lyr " ); var idOrdn = charIDToTypeID( "Ordn" ); var idTrgt = charIDToTypeID( "Trgt" ); ref1.putEnumerated( idLyr, idOrdn, idTrgt ); list1.putReference( ref1 ); desc2.putList( idnull, list1 ); var idTglO = charIDToTypeID( "TglO" ); desc2.putBoolean( idTglO, true ); executeAction( idShw, desc2, DialogModes.NO ); }; ////// load transparency ////// function loadTransparency (theInvert) { // ======================================================= var idsetd = charIDToTypeID( "setd" ); var desc3 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref2 = new ActionReference(); var idChnl = charIDToTypeID( "Chnl" ); var idfsel = charIDToTypeID( "fsel" ); ref2.putProperty( idChnl, idfsel ); desc3.putReference( idnull, ref2 ); var idT = charIDToTypeID( "T " ); var ref3 = new ActionReference(); var idChnl = charIDToTypeID( "Chnl" ); var idChnl = charIDToTypeID( "Chnl" ); var idTrsp = charIDToTypeID( "Trsp" ); ref3.putEnumerated( idChnl, idChnl, idTrsp ); desc3.putReference( idT, ref3 ); desc3.putBoolean(charIDToTypeID("Invr"), theInvert); executeAction( idsetd, desc3, DialogModes.NO ); }; ////// crop ////// function cropTo (x1, y1, x2, y2) { // ======================================================= var idCrop = charIDToTypeID( "Crop" ); var desc7 = new ActionDescriptor(); var idT = charIDToTypeID( "T " ); var desc8 = new ActionDescriptor(); var idTop = charIDToTypeID( "Top " ); var idPxl = charIDToTypeID( "#Pxl" ); desc8.putUnitDouble( idTop, idPxl, y1 ); var idLeft = charIDToTypeID( "Left" ); var idPxl = charIDToTypeID( "#Pxl" ); desc8.putUnitDouble( idLeft, idPxl, x1); var idBtom = charIDToTypeID( "Btom" ); var idPxl = charIDToTypeID( "#Pxl" ); desc8.putUnitDouble( idBtom, idPxl, y2 ); var idRght = charIDToTypeID( "Rght" ); var idPxl = charIDToTypeID( "#Pxl" ); desc8.putUnitDouble( idRght, idPxl, x2 ); var idRctn = charIDToTypeID( "Rctn" ); desc7.putObject( idT, idRctn, desc8 ); var idAngl = charIDToTypeID( "Angl" ); var idAng = charIDToTypeID( "#Ang" ); desc7.putUnitDouble( idAngl, idAng, 0.000000 ); var idDlt = charIDToTypeID( "Dlt " ); desc7.putBoolean( idDlt, false ); var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" ); var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" ); var idpureAspectRatio = stringIDToTypeID( "pureAspectRatio" ); desc7.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idpureAspectRatio ); var idCnsP = charIDToTypeID( "CnsP" ); desc7.putBoolean( idCnsP, false ); executeAction( idCrop, desc7, DialogModes.NO ); }; |