here are some functions to help you scale the last layer to the canvas size
matchLastLayerTocanvas();
function matchLastLayerTocanvas(){
try{activeDocument.backgroundLayer;var a=0 }catch(e){ var a = 1; };
// select last art layer::
while(true){
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), a );//refer the layer without selecting it
try{var desc = executeActionGet(ref);}catch(err){break;}// when the index is bigger there will be an error, the try will stop the wile loop
var ls = desc.getEnumerationValue(stringIDToTypeID("layerSection"));//get the king( layerSectionStart ) and (layerSectionEnd) are the LayerSets
// layer SectionContent is a normal layer
ls = typeIDToStringID(ls);// convert to string
if(ls == 'layerSectionContent'){// if it's not a LayerSet
if(!desc.hasKey(stringIDToTypeID("adjustment"))){// and if is not an adjutment layer
makeActiveByIndex(a);// select the layer
break;
}
}
a++;
}
var dw = parseFloat(app.activeDocument.width);
var dh = parseFloat(app.activeDocument.height);
var lb = app.activeDocument.activeLayer.bounds;
var lx = parseFloat(lb[0]);
var ly = parseFloat(lb[1]);
var lw = parseFloat(lb[2]) - lx;
var lh = parseFloat(lb[3]) - ly;
var resw = (100*dw)/lw;// calculate the procent needed to scale so it will match the doc size
var resh = (100*dh)/lh;
TrnsfWithLeftTopCorner(-lx, -ly, resw, resh);
}
function makeActiveByIndex(idx){
var idslct = charIDToTypeID( "slct" );
var desc248 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref151 = new ActionReference();
ref151.putIndex( charIDToTypeID( "Lyr " ), idx );
desc248.putReference( idnull, ref151 );
var idMkVs = charIDToTypeID( "MkVs" );
desc248.putBoolean( idMkVs, true );
executeAction( idslct, desc248, DialogModes.NO );
}
function TrnsfWithLeftTopCorner( x, y, w, h){
// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc131 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref59 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref59.putEnumerated( idLyr, idOrdn, idTrgt );
desc131.putReference( idnull, ref59 );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcszero = charIDToTypeID( "Qcs0" );
desc131.putEnumerated( idFTcs, idQCSt, idQcszero );
var idOfst = charIDToTypeID( "Ofst" );
var desc132 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc132.putUnitDouble( idHrzn, idPxl, x );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc132.putUnitDouble( idVrtc, idPxl, y );
var idOfst = charIDToTypeID( "Ofst" );
desc131.putObject( idOfst, idOfst, desc132 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc131.putUnitDouble( idWdth, idPrc, w );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc131.putUnitDouble( idHght, idPrc, h );
var idIntr = charIDToTypeID( "Intr" );
var idIntp = charIDToTypeID( "Intp" );
var idBcbc = charIDToTypeID( "Bcbc" );
desc131.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idTrnf, desc131, DialogModes.NO );
}