• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Resize to Guidelines

Explorer ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Hello..

 

Hoping to explain this well enough to make sense in regards to what I'm trying to achieve.

I have about 1,000 product images that need resizing to a specific height & base guideline.

While I can quickly resize each image manually and save out with some actions, I'd like to try and batch this.

 

The part I'm trying to figure how to automate is the resizing.

If a self election is made, is there an action or known script what will resize the image so the hieght & base of the detected pixels/selection will snap & resize to the guidelines?

 

Hope this makes sense, thanks in advance!

 

IMG-01 would represent a product that needs resizing

IMG-02 would be the end results

 

01.png02.png

TOPICS
Actions and scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

When you say base, do you mean width? If so, are the images of different aspect ratios? If they are do they need cropping, or just sized to the longest or shortest side. Can you show a screen shot of how your file is set up with the guides?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Hi Chuck, thank you for your response. I've uploaded a couple images to my post to better explain what I'm trying to achieve. The image resolution would remain the same. It's mainly resizing the product in the image to the guides.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

A script to add guides to selection bounds:

 

// Add Guides to Selection Bounds.jsx
//feedback.photoshop.com/photoshop_family/topics/add_guides_to_a_selection

#target Photoshop

main();
function main(){
if(!documents.length) return;
var startRulerUnits = preferences.rulerUnits;
try{
preferences.rulerUnits = Units.PIXELS 
var SB = activeDocument.selection.bounds;
}catch(e){return;}
guideLine(SB[1].value,"Hrzn"); 
guideLine(SB[3].value,"Hrzn"); 
guideLine(SB[0].value,"Vrtc"); 
guideLine(SB[2].value,"Vrtc"); 
preferences.rulerUnits = startRulerUnits; 
}
function guideLine(position, type){
var desc = new ActionDescriptor();
var desc2 = new ActionDescriptor();
desc2.putUnitDouble( app.charIDToTypeID ('Pstn'), app.charIDToTypeID('#Pxl'), position );
desc2.putEnumerated( app.charIDToTypeID('Ornt'), app.charIDToTypeID('Ornt'), app.charIDToTypeID(type) );
desc.putObject( app.charIDToTypeID('Nw  '), app.charIDToTypeID('Gd  '), desc2 );
executeAction( app.charIDToTypeID('Mk  '), desc, DialogModes.NO );
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

A script to resize a layer to guides:

 

//forums.adobe.com/message/8732179#8732179
//forums.adobe.com/message/8728770#8728770

// 2019 - Uncomment line 123 to enable the layer mask, which has been disabled to retain the proportions of the resized layer

// FitImageToGuides.jsx

/* ========================================================== 
// 2014  John J. McAssey (JJMack)  
// ======================================================= */  
  
  
// This script is supplied as is. It is provided as freeware.   
// The author accepts no liability for any problems arising from its use.  
  
  
/* Help Category note tag menu can be used to place script in automate menu 
<javascriptresource> 
<about>$$$/JavaScripts/FitImageToGuides/About=JJMack's FitImageToGuides .^r^rCopyright 2014 Mouseprints.^r^rFour and only four guides are required</about> 
<category>JJMack's Script</category> 
</javascriptresource> 
*/  
  
  
// enable double-clicking from Mac Finder or Windows Explorer  
#target photoshop  
  
  
// bring application forward for double-click events  
app.bringToFront();  
  
  
// ensure at least one document open  
if (!documents.length) alert('There are no documents open.', 'No Document');  
else {  
  // declare Global variables  
  
  
  //main(); // at least one document exists proceed  
  app.activeDocument.suspendHistory('Fix Image to Guides','main()');  //problem if there is a selection a layer resize Photoshop back up a history step ?  
}  
///////////////////////////////////////////////////////////////////////////////  
//                            main function                               
///////////////////////////////////////////////////////////////////////////////  
function main() {  
  // declare local variables  
  var orig_ruler_units = app.preferences.rulerUnits;  
  var orig_type_units = app.preferences.typeUnits;  
  var orig_display_dialogs = app.displayDialogs;  
  app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS  
  app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS  
  app.displayDialogs = DialogModes.NO; // Set Dialogs off  
  try { code(); }  
  // display error message if something goes wrong  
  catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }  
  app.displayDialogs = orig_display_dialogs; // Reset display dialogs   
  app.preferences.typeUnits  = orig_type_units; // Reset ruler units to original settings   
  app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings  
}  
///////////////////////////////////////////////////////////////////////////////  
//                           main function end                              
///////////////////////////////////////////////////////////////////////////////  
  
  
///////////////////////////////////////////////////////////////////////////////////////////////  
// The real code is embedded into this function so that at any point it can return   
// to the main line function to let it restore users edit environment and end        
//////////////////////////////////////////////////////////////////////////////////////////////  
function code() {  
  if (app.activeDocument.guides.length != 4) { alert("Four and only four Guides are required"); return; } // quit  
  // get guides;  
  var theVert = new Array;  
  var theHor = new Array;  
  for (var m = 0; m < app.activeDocument.guides.length; m++) {  
  if (app.activeDocument.guides[m].direction == Direction.HORIZONTAL) {theVert.push(app.activeDocument.guides[m].coordinate)}  
  else {theHor.push(app.activeDocument.guides[m].coordinate)}  
    };  
  if (theHor.length != 2 || theVert.length != 2) { alert("Four Guides two vertical and two horizontal are required"); return; } // quit  
  getTarget=getSelectedLayersIdx();  
  if (getTarget.length!=1){ alert("The number of layers targeted is " + getTarget.length ); return; } // quit  
  if (app.activeDocument.activeLayer.isBackgroundLayer ) { alert("Can not resize the background layer"); return; } // quit  
  if (!app.activeDocument.activeLayer.visible ) { alert("Active layer is  not visible"); return; } // quit  
  //if (hasLayerMask()) { alert("Active layer is  Masked"); return; } // quit  
  if (app.activeDocument.activeLayer.kind == LayerKind.NORMAL  || app.activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT && hasLayerMask()) { deleteLayerMask ();}  
  if (app.activeDocument.activeLayer.kind != LayerKind.NORMAL  && app.activeDocument.activeLayer.kind != LayerKind.SMARTOBJECT )  {   
  alert("Active layer is " + app.activeDocument.activeLayer.kind); return; } // quit  
  // set selection to the area defined but the guide lines the selection may get undone by the bug in .resize() backing up a step in histoty ???  
  app.activeDocument.selection.select([[theHor[0], theVert[0]], [theHor[1], theVert[0]], [theHor[1], theVert[1]], [theHor[0], theVert[1]]]);  
  // resize current normal layer or smart object layer to just cover selection canvas area aspect ratio and size and mask off any overflow  
  var SB = app.activeDocument.selection.bounds; // Get selection bounds  
  var SWidth = (SB[2].value) - (SB[0].value); // Area width  
  var SHeight = (SB[3].value) - (SB[1].value); // Area height  
  var LB = app.activeDocument.activeLayer.bounds; // Get Active layers bounds  
  var LWidth = (LB[2].value) - (LB[0].value); // Area width  
  var LHeight = (LB[3].value) - (LB[1].value); // Area height  
  var userResampleMethod = app.preferences.interpolation; // Save interpolation settings  
  app.preferences.interpolation = ResampleMethod.BICUBIC; // resample interpolation bicubic  
  app.activeDocument.selection.deselect(); // This deselect work around Adobe Bug in CS5, CS6, CC and CC 2014  
  // Since Adobe does not fix old releases of Photoshop this is a necessary work around for many releases of Photoshop  
  //alert("Before re-size history");  // Added to debug Adobe Resize Bug  
  try {  
  if (LWidth/LHeight<SWidth/SHeight) { // layer's Aspect Ratio less the Canvas area Aspect Ratio   
  var percentageChange = ((SWidth/LWidth)*100); // Resize to canvas area width  
  app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);  
  }  
  else {   
  var percentageChange = ((SHeight/LHeight)*100); // resize to canvas area height  
  app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);  
  }  
  }  
  catch(e) {   
  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
  selectFront(); // Photoshop make top layer current when none are targeted  
  code(); // Retry  with top visible layer selected targeted   
  return; // rest would have been done during the retry  
  }  
  //alert("After re-size history");    // Added to debug Adobe Resize Bug  
  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
  // Seems to be a bug in  resize() the document seems to first be backed up a step in history  
  app.activeDocument.selection.select([[theHor[0], theVert[0]], [theHor[1], theVert[0]], [theHor[1], theVert[1]], [theHor[0], theVert[1]]]); // redo the selection  
  align('AdCH'); // align to horizontal centers  
  align('AdCV'); // align to vertical centers  
  // addLayermask(); // add layer mask to mask off excess
}  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
// Helper Functions  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
function align(method) {  
  var desc = new ActionDescriptor();  
  var ref = new ActionReference();  
  ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );  
  desc.putReference( charIDToTypeID( "null" ), ref );  
  desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );  
  try{executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );}  
  catch(e){}  
}  
///////////////////////////////////////////////////////////////////////////////     
// Function: hasLayerMask     
// Usage: see if there is a raster layer mask     
// Input: <none> Must have an open document     
// Return: true if there is a vector mask     
///////////////////////////////////////////////////////////////////////////////     
function hasLayerMask() {     
  var hasLayerMask = false;     
  try {     
  var ref = new ActionReference();     
  var keyUserMaskEnabled = app.charIDToTypeID( 'UsrM' );     
  ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyUserMaskEnabled );     
  ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );     
  var desc = executeActionGet( ref );     
  if ( desc.hasKey( keyUserMaskEnabled ) ) { hasLayerMask = true; }     
  }  
  catch(e) { hasLayerMask = false; }     
  return hasLayerMask;     
}     
function getSelectedLayersIdx(){  
      var selectedLayers = new Array;  
      var ref = new ActionReference();  
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
      var desc = executeActionGet(ref);  
      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){  
         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));  
          var c = desc.count  
          var selectedLayers = new Array();  
          for(var i=0;i<c;i++){  
            try{  
               activeDocument.backgroundLayer;  
               selectedLayers.push(  desc.getReference( i ).getIndex() );  
            }catch(e){  
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );  
            }  
          }  
       }else{  
         var ref = new ActionReference();  
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));  
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
         try{  
            activeDocument.backgroundLayer;  
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);  
         }catch(e){  
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));  
         }  
      }  
      return selectedLayers;  
};  
function selectFront() {  
// Alt+. shortcut select ftont visible layer  
var idslct = charIDToTypeID( "slct" );  
    var desc250 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
        var ref207 = new ActionReference();  
        var idLyr = charIDToTypeID( "Lyr " );  
        var idOrdn = charIDToTypeID( "Ordn" );  
        var idFrnt = charIDToTypeID( "Frnt" );  
        ref207.putEnumerated( idLyr, idOrdn, idFrnt );  
    desc250.putReference( idnull, ref207 );  
    var idMkVs = charIDToTypeID( "MkVs" );  
    desc250.putBoolean( idMkVs, false );  
executeAction( idslct, desc250, DialogModes.NO );  
}  
function deleteLayerMask (apply) {  
// Delet Layer mask default to not apply first  
if (apply == undefined) {var apply = false};  
try {  
var idDlt = charIDToTypeID( "Dlt " );  
    var desc9 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
        var ref5 = new ActionReference();  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idMsk = charIDToTypeID( "Msk " );  
        ref5.putEnumerated( idChnl, idChnl, idMsk );  
    desc9.putReference( idnull, ref5 );  
    var idAply = charIDToTypeID( "Aply" );  
    desc9.putBoolean( idAply, apply );  
executeAction( idDlt, desc9, DialogModes.NO );  
}  
catch (e) {}  
};  
function addLayermask(){  
// Add layer Mask  
var idMk = charIDToTypeID( "Mk  " );  
    var desc52 = new ActionDescriptor();  
    var idNw = charIDToTypeID( "Nw  " );  
    var idChnl = charIDToTypeID( "Chnl" );  
    desc52.putClass( idNw, idChnl );  
    var idAt = charIDToTypeID( "At  " );  
        var ref19 = new ActionReference();  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idMsk = charIDToTypeID( "Msk " );  
        ref19.putEnumerated( idChnl, idChnl, idMsk );  
    desc52.putReference( idAt, ref19 );  
    var idUsng = charIDToTypeID( "Usng" );  
    var idUsrM = charIDToTypeID( "UsrM" );  
    var idRvlS = charIDToTypeID( "RvlS" );  
    desc52.putEnumerated( idUsng, idUsrM, idRvlS );  
executeAction( idMk, desc52, DialogModes.NO );  
// Un link layer mask just added fron the layers content  
var idsetd = charIDToTypeID( "setd" );  
    var desc2 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
        var ref1 = new ActionReference();  
        var idLyr = charIDToTypeID( "Lyr " );  
        var idOrdn = charIDToTypeID( "Ordn" );  
        var idTrgt = charIDToTypeID( "Trgt" );  
        ref1.putEnumerated( idLyr, idOrdn, idTrgt );  
    desc2.putReference( idnull, ref1 );  
    var idT = charIDToTypeID( "T   " );  
        var desc3 = new ActionDescriptor();  
        var idUsrs = charIDToTypeID( "Usrs" );  
        desc3.putBoolean( idUsrs, false );  
    var idLyr = charIDToTypeID( "Lyr " );  
    desc2.putObject( idT, idLyr, desc3 );  
executeAction( idsetd, desc2, DialogModes.NO );  
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

I have posted two scripts that may help.

 

The scripts could be combined into a larger script, or you could reference them in an action and use action steps to refine and work with the results of the scripts.

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Stephen, thank you very much for your response. As mentioned to Chuck, I've uploaded a couple images to better explain what I'm trying to achieve. I wouldn't need the edges of the whole image to meet the guidelines, rather the actual picture of the product. So if the BG has a solid color, it would need to detect the pixels of the product, and then resize the highest & lowest edges of the actual product to meet at the guidelines. Please let me know if this makes sense or not, thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

If the images can be isolated using select image or a luminosity or channel or colour selection or mask, then this can still be easily automated.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Many thanks for your time & efforts with this. I will say this scripts is in the right direction, the only thing is it resizes and constraints to the vertical guides. The image should resize to the top & bottom horizontal guides. Please let me know if I can help clarify further what I'm trying to achieve, thank you!

Screen Shot 2019-10-10 at 5.20.58 PM.pngScreen Shot 2019-10-10 at 5.21.02 PM.pngScreen Shot 2019-10-10 at 5.25.30 PM.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

I have edited JJMack's script to resize on the width, rather than the height.

 

I think it will depend on whether the layer content is portrait or landscape orientation...

 

//forums.adobe.com/message/8732179#8732179
//forums.adobe.com/message/8728770#8728770

// 2019 - Uncomment line 123 to enable the layer mask, which has been disabled to retain the proportions of the resized layer

// FitImageToGuides.jsx

/* ========================================================== 
// 2014  John J. McAssey (JJMack)  
// ======================================================= */  
  
  
// This script is supplied as is. It is provided as freeware.   
// The author accepts no liability for any problems arising from its use.  
  
  
/* Help Category note tag menu can be used to place script in automate menu 
<javascriptresource> 
<about>$$$/JavaScripts/FitImageToGuides/About=JJMack's FitImageToGuides .^r^rCopyright 2014 Mouseprints.^r^rFour and only four guides are required</about> 
<category>JJMack's Script</category> 
</javascriptresource> 
*/  
  
  
// enable double-clicking from Mac Finder or Windows Explorer  
#target photoshop
  
  
// bring application forward for double-click events  
app.bringToFront();  
  
  
// ensure at least one document open  
if (!documents.length) alert('There are no documents open.', 'No Document');  
else {  
  // declare Global variables  
  
  
  //main(); // at least one document exists proceed  
  app.activeDocument.suspendHistory('Fix Image to Guides','main()');  //problem if there is a selection a layer resize Photoshop back up a history step ?  
}  
///////////////////////////////////////////////////////////////////////////////  
//                            main function                               
///////////////////////////////////////////////////////////////////////////////  
function main() {  
  // declare local variables  
  var orig_ruler_units = app.preferences.rulerUnits;  
  var orig_type_units = app.preferences.typeUnits;  
  var orig_display_dialogs = app.displayDialogs;  
  app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS  
  app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS  
  app.displayDialogs = DialogModes.NO; // Set Dialogs off  
  try { code(); }  
  // display error message if something goes wrong  
  catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }  
  app.displayDialogs = orig_display_dialogs; // Reset display dialogs   
  app.preferences.typeUnits  = orig_type_units; // Reset ruler units to original settings   
  app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings  
}  
///////////////////////////////////////////////////////////////////////////////  
//                           main function end                              
///////////////////////////////////////////////////////////////////////////////  
  
  
///////////////////////////////////////////////////////////////////////////////////////////////  
// The real code is embedded into this function so that at any point it can return   
// to the main line function to let it restore users edit environment and end        
//////////////////////////////////////////////////////////////////////////////////////////////  
function code() {  
  if (app.activeDocument.guides.length != 4) { alert("Four and only four Guides are required"); return; } // quit  
  // get guides;  
  var theVert = new Array;  
  var theHor = new Array;  
  for (var m = 0; m < app.activeDocument.guides.length; m++) {  
  if (app.activeDocument.guides[m].direction == Direction.HORIZONTAL) {theVert.push(app.activeDocument.guides[m].coordinate)}  
  else {theHor.push(app.activeDocument.guides[m].coordinate)}  
    };  
  if (theHor.length != 2 || theVert.length != 2) { alert("Four Guides two vertical and two horizontal are required"); return; } // quit  
  getTarget=getSelectedLayersIdx();  
  if (getTarget.length!=1){ alert("The number of layers targeted is " + getTarget.length ); return; } // quit  
  if (app.activeDocument.activeLayer.isBackgroundLayer ) { alert("Can not resize the background layer"); return; } // quit  
  if (!app.activeDocument.activeLayer.visible ) { alert("Active layer is  not visible"); return; } // quit  
  //if (hasLayerMask()) { alert("Active layer is  Masked"); return; } // quit  
  if (app.activeDocument.activeLayer.kind == LayerKind.NORMAL  || app.activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT && hasLayerMask()) { deleteLayerMask ();}  
  if (app.activeDocument.activeLayer.kind != LayerKind.NORMAL  && app.activeDocument.activeLayer.kind != LayerKind.SMARTOBJECT )  {   
  alert("Active layer is " + app.activeDocument.activeLayer.kind); return; } // quit  
  // set selection to the area defined but the guide lines the selection may get undone by the bug in .resize() backing up a step in histoty ???  
  app.activeDocument.selection.select([[theHor[0], theVert[0]], [theHor[1], theVert[0]], [theHor[1], theVert[1]], [theHor[0], theVert[1]]]);  
  // resize current normal layer or smart object layer to just cover selection canvas area aspect ratio and size and mask off any overflow  
  var SB = app.activeDocument.selection.bounds; // Get selection bounds  
  var SWidth = (SB[2].value) - (SB[0].value); // Area width  
  var SHeight = (SB[3].value) - (SB[1].value); // Area height  
  var LB = app.activeDocument.activeLayer.bounds; // Get Active layers bounds  
  var LWidth = (LB[2].value) - (LB[0].value); // Area width  
  var LHeight = (LB[3].value) - (LB[1].value); // Area height  
  var userResampleMethod = app.preferences.interpolation; // Save interpolation settings  
  app.preferences.interpolation = ResampleMethod.BICUBIC; // resample interpolation bicubic  
  app.activeDocument.selection.deselect(); // This deselect work around Adobe Bug in CS5, CS6, CC and CC 2014  
  // Since Adobe does not fix old releases of Photoshop this is a necessary work around for many releases of Photoshop  
  //alert("Before re-size history");  // Added to debug Adobe Resize Bug  
  try {  
  if (LWidth/LHeight<SWidth/SHeight) { // layer's Aspect Ratio less the Canvas area Aspect Ratio   
  var percentageChange = ((SWidth/LWidth)*100); // Resize to canvas area width  
  app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);  
  }  
  else {   
  // STEPHEN A. MARSH 2019 -  EDIT HEIGHT TO WIDTH
  var percentageChange = ((SWidth/LWidth)*100); // resize to canvas area width  
  app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);  
  }  
  }  
  catch(e) {   
  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
  selectFront(); // Photoshop make top layer current when none are targeted  
  code(); // Retry  with top visible layer selected targeted   
  return; // rest would have been done during the retry  
  }  
  //alert("After re-size history");    // Added to debug Adobe Resize Bug  
  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
  // Seems to be a bug in  resize() the document seems to first be backed up a step in history  
  app.activeDocument.selection.select([[theHor[0], theVert[0]], [theHor[1], theVert[0]], [theHor[1], theVert[1]], [theHor[0], theVert[1]]]); // redo the selection  
  align('AdCH'); // align to horizontal centers  
  align('AdCV'); // align to vertical centers  
  // addLayermask(); // add layer mask to mask off excess
}  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
// Helper Functions  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
function align(method) {  
  var desc = new ActionDescriptor();  
  var ref = new ActionReference();  
  ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );  
  desc.putReference( charIDToTypeID( "null" ), ref );  
  desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );  
  try{executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );}  
  catch(e){}  
}  
///////////////////////////////////////////////////////////////////////////////     
// Function: hasLayerMask     
// Usage: see if there is a raster layer mask     
// Input: <none> Must have an open document     
// Return: true if there is a vector mask     
///////////////////////////////////////////////////////////////////////////////     
function hasLayerMask() {     
  var hasLayerMask = false;     
  try {     
  var ref = new ActionReference();     
  var keyUserMaskEnabled = app.charIDToTypeID( 'UsrM' );     
  ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyUserMaskEnabled );     
  ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );     
  var desc = executeActionGet( ref );     
  if ( desc.hasKey( keyUserMaskEnabled ) ) { hasLayerMask = true; }     
  }  
  catch(e) { hasLayerMask = false; }     
  return hasLayerMask;     
}     
function getSelectedLayersIdx(){  
      var selectedLayers = new Array;  
      var ref = new ActionReference();  
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
      var desc = executeActionGet(ref);  
      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){  
         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));  
          var c = desc.count  
          var selectedLayers = new Array();  
          for(var i=0;i<c;i++){  
            try{  
               activeDocument.backgroundLayer;  
               selectedLayers.push(  desc.getReference( i ).getIndex() );  
            }catch(e){  
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );  
            }  
          }  
       }else{  
         var ref = new ActionReference();  
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));  
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
         try{  
            activeDocument.backgroundLayer;  
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);  
         }catch(e){  
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));  
         }  
      }  
      return selectedLayers;  
};  
function selectFront() {  
// Alt+. shortcut select ftont visible layer  
var idslct = charIDToTypeID( "slct" );  
    var desc250 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
        var ref207 = new ActionReference();  
        var idLyr = charIDToTypeID( "Lyr " );  
        var idOrdn = charIDToTypeID( "Ordn" );  
        var idFrnt = charIDToTypeID( "Frnt" );  
        ref207.putEnumerated( idLyr, idOrdn, idFrnt );  
    desc250.putReference( idnull, ref207 );  
    var idMkVs = charIDToTypeID( "MkVs" );  
    desc250.putBoolean( idMkVs, false );  
executeAction( idslct, desc250, DialogModes.NO );  
}  
function deleteLayerMask (apply) {  
// Delet Layer mask default to not apply first  
if (apply == undefined) {var apply = false};  
try {  
var idDlt = charIDToTypeID( "Dlt " );  
    var desc9 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
        var ref5 = new ActionReference();  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idMsk = charIDToTypeID( "Msk " );  
        ref5.putEnumerated( idChnl, idChnl, idMsk );  
    desc9.putReference( idnull, ref5 );  
    var idAply = charIDToTypeID( "Aply" );  
    desc9.putBoolean( idAply, apply );  
executeAction( idDlt, desc9, DialogModes.NO );  
}  
catch (e) {}  
};  
function addLayermask(){  
// Add layer Mask  
var idMk = charIDToTypeID( "Mk  " );  
    var desc52 = new ActionDescriptor();  
    var idNw = charIDToTypeID( "Nw  " );  
    var idChnl = charIDToTypeID( "Chnl" );  
    desc52.putClass( idNw, idChnl );  
    var idAt = charIDToTypeID( "At  " );  
        var ref19 = new ActionReference();  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idChnl = charIDToTypeID( "Chnl" );  
        var idMsk = charIDToTypeID( "Msk " );  
        ref19.putEnumerated( idChnl, idChnl, idMsk );  
    desc52.putReference( idAt, ref19 );  
    var idUsng = charIDToTypeID( "Usng" );  
    var idUsrM = charIDToTypeID( "UsrM" );  
    var idRvlS = charIDToTypeID( "RvlS" );  
    desc52.putEnumerated( idUsng, idUsrM, idRvlS );  
executeAction( idMk, desc52, DialogModes.NO );  
// Un link layer mask just added fron the layers content  
var idsetd = charIDToTypeID( "setd" );  
    var desc2 = new ActionDescriptor();  
    var idnull = charIDToTypeID( "null" );  
        var ref1 = new ActionReference();  
        var idLyr = charIDToTypeID( "Lyr " );  
        var idOrdn = charIDToTypeID( "Ordn" );  
        var idTrgt = charIDToTypeID( "Trgt" );  
        ref1.putEnumerated( idLyr, idOrdn, idTrgt );  
    desc2.putReference( idnull, ref1 );  
    var idT = charIDToTypeID( "T   " );  
        var desc3 = new ActionDescriptor();  
        var idUsrs = charIDToTypeID( "Usrs" );  
        desc3.putBoolean( idUsrs, false );  
    var idLyr = charIDToTypeID( "Lyr " );  
    desc2.putObject( idT, idLyr, desc3 );  
executeAction( idsetd, desc2, DialogModes.NO );  
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Stephen, Amazing! Thank you Sir, you have helped save serious time. Very much Appreciated!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Great, glad to be of help! What did you end up doing, making a combined script, or referencing the scripts in an action and then processing with added action steps? What is the step by step workflow of the script or action?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

LATEST

Help is an understatement 🙂 I'm using a mix of Actions with the Script. I have a template with guides to fit a specific composition/s.. With the script, I copied a masked selection > pasted over to the template. Rotating the canvas +90 before running the script hacked it just fine > Ran the script > Rotated back -90 before export.

 

The only bump is with longer objects (bench vs pole) that need to fit within the left & right vertical lines, but would then need to be moved up so it touches the top guideline - Update* With some actions, sizing down the image to the guidelines and replating it with a clr > pasting the image > script > aligning to the the top > resize canvas to needed image size.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines