Copy link to clipboard
Copied
Hello!
I am trying to modify a script so that it can resize proportionally regardless of the ratio based on the minimum width size or the height of the active layer based on the limit of a selection.
I hear this but I get a mistake! Where am I going wrong?
var docRef = activeDocument;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
docRef.activeLayer = activeDocument.activeLayer;
app.activeDocument = docRef;
var LB = activeDocument.selection.bounds;
var res = activeDocument.resolution;
var width = LB[2]-LB[0];
var height = LB[3]-LB[1];
var dwidth = activeDocument.activeLayer.value;
var dheight = activeDocument.activeLayer.value;
var ratio = height/width;
var dratio = dheight/dwidth;
if (dratio > ratio) { height = undefined; } else { width = undefined; }
//activeDocument.resizeImage(width, height, res, ResampleMethod.BICUBICSHARPER);////
activeDocument.activeLayer.resize(width, height, res, AnchorPosition.MIDDLECENTER)
This may do what you want to do. Do not modify it to suspend history states for if you do it will be bitten by an Adobe Scripting Bug.
...var docRef = activeDocument;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
docRef.activeLayer = activeDocument.activeLayer;
app.activeDocument = docRef;
var res = activeDocument.resolution.value;
if (app.activeDocument.activeLayer.kind == LayerKind.NORMAL || app.activeDocument.activeLayer.kind == LayerKi
Copy link to clipboard
Copied
Why
var dwidth = activeDocument.activeLayer.value;
var dheight = activeDocument.activeLayer.value;
var dratio = dheight/dwidth;
Why not just
var dratio = 1;
Copy link to clipboard
Copied
JJMack escreveu
Why
var dwidth = activeDocument.activeLayer.value;
var dheight = activeDocument.activeLayer.value;
var dratio = dheight/dwidth;
Why not just
var dratio = 1;
This ensures that the selection is completely filled, the pixels that exceed the selection will be eliminated by a mask.
Copy link to clipboard
Copied
What woud using 1 do that. The code is not getinh at width and height values.
What doe you thing "activeDocument.activeLayer.value" is?
Copy link to clipboard
Copied
If you set guides like you seem to show you could do something like fit image to guide. Be carefull if you suspend history. If you resize the layer when you have an active selection. You also show an avtive se;ection. There is a bug in Photoshop scripting that will bite you. I reported it years ago when I wrote my fit image to guide scrtpt. Photoshop: Script bug resize layer back document up one history state | Photoshop Family Customer Co...
Perhaps the code you posted you though you were getting the select area sides. You can get a selection bounds like you can get the layer bounds like you did. With the selections bounds you can get the x,y location and with and height of the selected area to use in your math logic. For best fit you need to compare the two aspect ratio the Layers aspect ratio and the selections area aspect ratio to figure out if you should reside to the selection width or height. It not a landscape or portrait thing for the are two aspect ratios involved.
This will work:
/* ==========================================================
// 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 // this command only works in Photoshop CS2 and higher
// 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
.direction == Direction.HORIZONTAL) {theVert.push(app.activeDocument.guides .coordinate)} else {theHor.push(app.activeDocument.guides
.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 );
}
Copy link to clipboard
Copied
It would be something similar to your script ... we would only switch guides for selection .. I have been looking to clean up my scripts as much as possible.
I believe that in the script I posted this is possible, if you change the last line:
that:
activeDocument.activeLayer.resize(width, height, res, AnchorPosition.MIDDLECENTER)
per:
activeDocument.resizeImage(width, height, res, ResampleMethod.BICUBICSHARPER);
you will notice that the size of the document change with reference to the limits of the selection.
Why does not this work with the layer?
Copy link to clipboard
Copied
Why would you resize the document the would also resize the active selection not resize the layer for the resized selection or align the layer to the selection. You could write a script like mine that just uses an active selection. You would still need to compare the two different aspect ratios to figure out what you should resize for the selection width or height. after the resize you need to align the resize layer to the selection and optionally mask the layer to confine the layers content to the active selection
Your script address the Active selection bounds Looks like it may have been trying the get the active layer width and height. however the code does not do that. Your logic does not take into consideration the two different aspect ratios or try to align the resized layer to the active selection.
Copy link to clipboard
Copied
Dare I say it looks overcomplicated for what I believe you're trying to achieve.
If you change units to percent, you can scale by a set percentage. As long as width/height get the same value.
See below:
var lyr = app.activeDocument.activeLayer
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PERCENT;
app.preferences.typeUnits = TypeUnits.PIXELS;
lyr.resize(200,200, AnchorPosition.MIDDLECENTER);
Copy link to clipboard
Copied
They want to fit an image to a selection. Not scale a layers by some percentage.
Copy link to clipboard
Copied
I think what you were trying to code is this. However, the math does not work and the is no alignment code.
You need to figure out if you need to resize for the select area width or height and calculate the correct resize percentage. After the resize you need to align the layer to the selection.
var docRef = activeDocument;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
docRef.activeLayer = activeDocument.activeLayer;
app.activeDocument = docRef;
var res = activeDocument.resolution.value;
var LB = activeDocument.selection.bounds;
var width = LB[2]-LB[0];
var height = LB[3]-LB[1];
var IB = activeDocument.activeLayer.bounds;
var dwidth = IB[2]-IB[0];
var dheight = IB[3]-IB[1];
var ratio = height/width;
var dratio = dheight/dwidth;
if (dratio > ratio) { height = undefined; } else { width = undefined; }
activeDocument.activeLayer.resize(width, height, res, AnchorPosition.MIDDLECENTER)
Copy link to clipboard
Copied
This may do what you want to do. Do not modify it to suspend history states for if you do it will be bitten by an Adobe Scripting Bug.
var docRef = activeDocument;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
docRef.activeLayer = activeDocument.activeLayer;
app.activeDocument = docRef;
var res = activeDocument.resolution.value;
if (app.activeDocument.activeLayer.kind == LayerKind.NORMAL || app.activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT && hasLayerMask()) { deleteLayerMask ();}
var LB = activeDocument.selection.bounds;
var width = LB[2]-LB[0];
var height = LB[3]-LB[1];
var IB = activeDocument.activeLayer.bounds;
var dwidth = IB[2]-IB[0];
var dheight = IB[3]-IB[1];
var ratio = height/width;
var dratio = dheight/dwidth;
if (dratio>ratio) { // layer's Aspect Ratio less the Canvas area Aspect Ratio
var percentageChange = ((width/dwidth)*100); // Resize to canvas area width
app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);
}
else {
var percentageChange = ((height/dheight)*100); // resize to canvas area height
app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);
}
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() {
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 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 );
}
Copy link to clipboard
Copied
Perfect! Thank you for your dedication and willingness to help me JJMack.
Copy link to clipboard
Copied
The code is not clean the restore of user ruler units is missing and you declare some variable and set some you never reference so I cleaned up the code removed the unneeded stuff and add the restore.
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if (app.activeDocument.activeLayer.kind == LayerKind.NORMAL || app.activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT && hasLayerMask()) { deleteLayerMask ();}
var LB = activeDocument.selection.bounds;
var width = LB[2]-LB[0];
var height = LB[3]-LB[1];
var IB = activeDocument.activeLayer.bounds;
var dwidth = IB[2]-IB[0];
var dheight = IB[3]-IB[1];
var ratio = height/width;
var dratio = dheight/dwidth;
if (dratio>ratio) { // layer's Aspect Ratio less the Canvas area Aspect Ratio
var percentageChange = ((width/dwidth)*100); // Resize to canvas area width
app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);
}
else {
var percentageChange = ((height/dheight)*100); // resize to canvas area height
app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);
}
align('AdCH'); // align to horizontal centers
align('AdCV'); // align to vertical centers
addLayermask(); // add layer mask to mask off excess
app.preferences.rulerUnits = startRulerUnits; // restore ruler units
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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() {
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 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 );
}
Copy link to clipboard
Copied
It was very good, much cleaner and with a more friendly vision.