Copy link to clipboard
Copied
welcome everybody
I have code that resizes a layer by a value
The code only works on one layer
I found a code that implements action on selected layers, but it doesn't work for me
It only works on one layer, and when implemented on more than one layer, it gives a very different result from what I want
I don't know where the problem is... and this is the code
Width = 100
Hight = 200
//- Resize Layer With Value - //
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var bounds = activeDocument.activeLayer.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
var newWidth = (100 / width) * Width;
var newHeight = (100 / height) * Hight;
var layers = getSelectedLayers();
for (var i = 0; i < layers.length; i ++){
layers[i].resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
}
function getSelectedLayers(){
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.ALL );
var resultLayers=new Array();
for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++){resultLayers.push(app.activeDocument.activeLayer.layers[ix])}
var id8 = charIDToTypeID( "slct" );
var desc5 = new ActionDescriptor();
var id9 = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var id10 = charIDToTypeID( "HstS" );
var id11 = charIDToTypeID( "Ordn" );
var id12 = charIDToTypeID( "Prvs" );
ref2.putEnumerated( id10, id11, id12 );
desc5.putReference( id9, ref2 );
executeAction( id8, desc5, DialogModes.NO );
return resultLayers;
}
My offering in a previous topic of yours does exactly this:
I would recommend converting all the Layers to Smart Objects (if they are not already) before transforming.
Does this help?
// scale all selected layers to pixeldimensions;
// 2022, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var theW = 400;
var theH = 200;
var theLayers = collectSelectedLayersBounds ();
// transform;
for (var x = 0; x < theLayers.length; x++) {
var horScale = theW / (theLayers[x][2][2] - theLayers[x][2][0]) * 100;
...
Copy link to clipboard
Copied
The function getSelectedLayers seems antiquated, I suspect it precedes some changes that made evaluating the selected Layers easier.
when implemented on more than one layer, it gives a very different result from what I want
What is that result and how does it differ from your expectations?
Copy link to clipboard
Copied
The function getSelectedLayers seems antiquated, I suspect it precedes some changes that made evaluating the selected Layers easier.
What can be changed in the code?
I attached a picture showing what is required
Copy link to clipboard
Copied
My offering in a previous topic of yours does exactly this:
Copy link to clipboard
Copied
very nice sir but i'm sorry
This code converts the layers of the Smart Object and then Restrizelayer
I want the specific layer code I need to perform more than one action on these specific layers
for example
- Change fonts for selected layers
- Make a specific filter on the selected layers, etc.
Thank you very much for your interest, appreciation and outstanding work
Copy link to clipboard
Copied
You didn't mention that!
So, is it just that you need code to apply a specific function* to multiple selected layers?
* The function could be anything, you were just using resize as one example of many possible operations that need to be performed to multiple selected layers?
Copy link to clipboard
Copied
Actually, I didn't mention that
I already know how an action (any action) is performed on a single layer
But the procedure is performed on selected layers, I know how it is done
But your code is very nice, but the procedure is implemented by smartobject, which is a procedure that is used in some things
But there are some other things where I want to perform the action on the selected layers directly
- If the procedure for the selected layers is possible to perform any action on these layers
Copy link to clipboard
Copied
I would suggest that you take another look at the code again.
There were two things going on:
1) The function resizeLayerFromPrefFile() was indeed using a smart object in order to resize via px values rather than % values. This could be changed so that one does not rasterize the SO layer, or the SO layer could be converted back to layers rather than rasterizing or you could avoid using a SO layer altogether as your script version did and use %. So there are multiple options on the resize aspect. Anyway, that is not the main point that I was making.
2) The function processSelectedLayers() does exactly that.
There are many other code examples on the site for processing selected layers, that is where I would have taken the function from in the first place.
Copy link to clipboard
Copied
Ok sir.. I will review the code again and inform you of the final result
Thank you very much
Copy link to clipboard
Copied
I would recommend converting all the Layers to Smart Objects (if they are not already) before transforming.
Does this help?
// scale all selected layers to pixeldimensions;
// 2022, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var theW = 400;
var theH = 200;
var theLayers = collectSelectedLayersBounds ();
// transform;
for (var x = 0; x < theLayers.length; x++) {
var horScale = theW / (theLayers[x][2][2] - theLayers[x][2][0]) * 100;
var verScale = theH / (theLayers[x][2][3] - theLayers[x][2][1]) * 100;
layerDuplicateOffsetScaleRotate (theLayers[x][1], 0, 0, horScale, verScale, 0, true)
};
// reselect layers;
selectLayerByID(theLayers[0][1], false);
for (var y = 1; y < theLayers.length; y++) {selectLayerByID(theLayers[y][1], true)}
};
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
selectedLayers.push([theName, theIdentifier, theseBounds]);
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
selectedLayers = [[theName, theIdentifier, theseBounds]]
} catch (e) {};
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// duplicate layer (id, xOffset, yOffset, theXScale, theYScale, theAngle) //////
function layerDuplicateOffsetScaleRotate (theIdentifier, xOffset, yOffset, theXScale, theYScale, theAngle, copy) {
selectLayerByID(theIdentifier, false);
var desc23 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc23.putReference( idnull, ref2 );
desc23.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc24 = new ActionDescriptor();
var idPxl = charIDToTypeID( "#Pxl" );
desc24.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
desc24.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
desc23.putObject( idOfst, idOfst, desc24 );
var idPrc = charIDToTypeID( "#Prc" );
desc23.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theXScale );
desc23.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theYScale );
desc23.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), theAngle );
desc23.putEnumerated( charIDToTypeID( "Intr" ), charIDToTypeID( "Intp" ), stringIDToTypeID( "bicubicAutomatic" ) );
if (copy == true) {desc23.putBoolean( charIDToTypeID( "Cpy " ), true )};
executeAction( charIDToTypeID( "Trnf" ), desc23, DialogModes.NO );
};
Copy link to clipboard
Copied
@c.pfaffenbichler @Stephen_A_Marsh
thank you very much all
Your code is really nice and good