Copy link to clipboard
Copied
Hi!
I need a script that moves EVERY layer to canvas center and then will fit EVERY layer inside the circle with 140 px diameter, so if I rotate that layer it will never go outside circle with its edges.
I made it work for selected layer and have a trouble to loop it for every layer:
1. move layer to center
2. resize layer to 140 px
3. rotate -45 degree
4. check width and hight and resize if needed
5. rotate +45 degree
6. set levels (I need layers totally black in the end
How to make this work for every layer?
appendCoords(app.activeDocument)
function appendCoords() {
// var m = theParent.layers.length - 1;
// var theLayer = theParent.layers[m];
// app.activeDocument.activeLayer = theLayer;
var bounds = app.activeDocument.activeLayer.bounds;
var selHeight = bounds[3].value - bounds[1].value;
var selWidth = bounds[2].value - bounds[0].value;
var selCenterX = (bounds[0].as('px') + bounds[2].as('px')) / 2; //coordinates in pixels
var selCenterY = (bounds[1].as('px') + bounds[3].as('px')) / 2;
var x = bounds[0].as('px');
var y = bounds[1].as('px');
var w = app.activeDocument.width.value;
var h = app.activeDocument.height.value;
var docCenterW = activeDocument.width.as("px") / 2;
var docCenterH = activeDocument.height.as("px") / 2;
var roundRadius = 140;
//========move layer to center=========
// Getting center coordinates of the document
var docCenterW = activeDocument.width.as("px") / 2;
var docCenterH = activeDocument.height.as("px") / 2;
// getting values to translate the layer.
var deltaX = Math.round(docCenterW - (bounds[0].value + selWidth / 2));
var deltaY = Math.round(docCenterH - (bounds[1].value + selHeight / 2));
activeDocument.activeLayer.translate(deltaX, deltaY);
//=====resize layer==== - if heigh/width>1 then change height else change width
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if (selHeight / selWidth > 1) {
var newSize = (100 / selHeight) * roundRadius;
} else {
var newSize = (100 / selWidth) * roundRadius;
}
activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER); //resize biggest side
//app.preferences.rulerUnits = startRulerUnits;
// ===put inside the circle===
// 1. rotate -45 degree
var idtransform = stringIDToTypeID("transform");
var desc44352 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref3372 = new ActionReference();
var idlayer = stringIDToTypeID("layer");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref3372.putEnumerated(idlayer, idordinal, idtargetEnum);
desc44352.putReference(idnull, ref3372);
var idfreeTransformCenterState = stringIDToTypeID("freeTransformCenterState");
var idquadCenterState = stringIDToTypeID("quadCenterState");
var idQCSAverage = stringIDToTypeID("QCSAverage");
desc44352.putEnumerated(idfreeTransformCenterState, idquadCenterState, idQCSAverage);
var idoffset = stringIDToTypeID("offset");
var desc44353 = new ActionDescriptor();
var idhorizontal = stringIDToTypeID("horizontal");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc44353.putUnitDouble(idhorizontal, idpixelsUnit, 0.000000);
var idvertical = stringIDToTypeID("vertical");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc44353.putUnitDouble(idvertical, idpixelsUnit, 0.000000);
var idoffset = stringIDToTypeID("offset");
desc44352.putObject(idoffset, idoffset, desc44353);
var idangle = stringIDToTypeID("angle");
var idangleUnit = stringIDToTypeID("angleUnit");
desc44352.putUnitDouble(idangle, idangleUnit, -45);
var idlinked = stringIDToTypeID("linked");
desc44352.putBoolean(idlinked, true);
var idinterfaceIconFrameDimmed = stringIDToTypeID("interfaceIconFrameDimmed");
var idinterpolationType = stringIDToTypeID("interpolationType");
var idbicubicSharper = stringIDToTypeID("bicubicSharper");
desc44352.putEnumerated(idinterfaceIconFrameDimmed, idinterpolationType, idbicubicSharper);
executeAction(idtransform, desc44352, DialogModes.NO);
// 2. check widht and resize if needed
//alert("1" + newSize);
app.preferences.rulerUnits = Units.PIXELS;
//alert("1" + selHeight);
var bounds = app.activeDocument.activeLayer.bounds;
var selHeight = bounds[3].value - bounds[1].value;
var selWidth = bounds[2].value - bounds[0].value;
if (selHeight > roundRadius) {
var newSize = (100 / selHeight) * roundRadius;
activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER); //resize biggest side
} else {
if (selWidth > roundRadius) {
var newSize = (100 / selWidth) * roundRadius;
activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER); //resize biggest side
}
}
// 3. rotate +45 degree
var idtransform = stringIDToTypeID("transform");
var desc44352 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref3372 = new ActionReference();
var idlayer = stringIDToTypeID("layer");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref3372.putEnumerated(idlayer, idordinal, idtargetEnum);
desc44352.putReference(idnull, ref3372);
var idfreeTransformCenterState = stringIDToTypeID("freeTransformCenterState");
var idquadCenterState = stringIDToTypeID("quadCenterState");
var idQCSAverage = stringIDToTypeID("QCSAverage");
desc44352.putEnumerated(idfreeTransformCenterState, idquadCenterState, idQCSAverage);
var idoffset = stringIDToTypeID("offset");
var desc44353 = new ActionDescriptor();
var idhorizontal = stringIDToTypeID("horizontal");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc44353.putUnitDouble(idhorizontal, idpixelsUnit, 0.000000);
var idvertical = stringIDToTypeID("vertical");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc44353.putUnitDouble(idvertical, idpixelsUnit, 0.000000);
var idoffset = stringIDToTypeID("offset");
desc44352.putObject(idoffset, idoffset, desc44353);
var idangle = stringIDToTypeID("angle");
var idangleUnit = stringIDToTypeID("angleUnit");
desc44352.putUnitDouble(idangle, idangleUnit, 45);
var idlinked = stringIDToTypeID("linked");
desc44352.putBoolean(idlinked, true);
var idinterfaceIconFrameDimmed = stringIDToTypeID("interfaceIconFrameDimmed");
var idinterpolationType = stringIDToTypeID("interpolationType");
var idbicubicSharper = stringIDToTypeID("bicubicSharper"); //bicubicSmoother
desc44352.putEnumerated(idinterfaceIconFrameDimmed, idinterpolationType, idbicubicSharper);
executeAction(idtransform, desc44352, DialogModes.NO);
// =========set levels==========
activeDocument.activeLayer.adjustLevels(0, 2, 1, 1, 2); // set black levels for silhouet*/
//alert(selHeight + " " + selWidth)
}
Copy link to clipboard
Copied
Made it myself somehow for selected layers 🙂
if (app.documents.length > 0) {
smartifyAndGetSelectedLayersIdxEtc()
};
////// get array of arrays of smart objects witrh index, center and half-dimensions //////
function smartifyAndGetSelectedLayersIdxEtc(){
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" )));
};
};
////////////////////////////////////
var theArray = new Array;
var theIDs = new Array;
for (var m = 0; m < selectedLayers.length; m++) {
var thisIndex = selectedLayers[m];
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), thisIndex);
var layerDesc = executeActionGet(ref);
var thisID = layerDesc.getInteger(stringIDToTypeID("layerID"));
var theKind = layerDesc.getInteger(stringIDToTypeID("layerKind"));
var theName = layerDesc.getString(stringIDToTypeID("name"));
var theVisibility = layerDesc.getInteger(stringIDToTypeID("visible"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var halfWidth = theBounds.getUnitDoubleValue(stringIDToTypeID("width")) / 2;
var halfHeight = theBounds.getUnitDoubleValue(stringIDToTypeID("height")) / 2;
var theX = theBounds.getUnitDoubleValue(stringIDToTypeID("left")) + halfWidth;
var theY = theBounds.getUnitDoubleValue(stringIDToTypeID("top")) + halfHeight;
// Getting center coordinates of the document
var docCenterW = activeDocument.width.as("px") / 2;
var docCenterH = activeDocument.height.as("px") / 2;
// is normal, shape, smart object, pattern, gradiet, solid color;
if (theKind == 1 || theKind == 4 || theKind == 5 || theKind == 9 || theKind == 10 || theKind == 11) {
if (theVisibility == true) {
theIDs.push ([thisID, theX, theY, halfWidth, halfHeight, theName])
}
}
};
////////////////////////////////////
for (var n = 0; n < theIDs.length; n++) {
if (hasSmartObject(theIDs[n][0]) == false) {
try {
selectLayerByID(theIDs[n][0], false);
///////////////////////////////================
move_px(docCenterW, docCenterH);
scale_px(140);
rotate(-45);
checkSize(140);
rotate(45);
setLevels()
///////////////////////////////=================
theArray.push([getLayerId(app.activeDocument.activeLayer), theIDs[n][1], theIDs[n][2], theIDs[n][3], theIDs[n][4], theIDs[n][5]]);
} catch (e) {}
}
else {theArray.push(theIDs[n])};
};
////////////////////////////////////
// select;
selectLayerByID(theArray[0][0],false);
for (var a = 1; a < theArray.length; a++) {selectLayerByID(theArray[a][0], true)};
//return;
return theArray
////////////////////////////////////
};
// by mike hale, via paul riggott;
function getLayerId(theLayer){
// http://forums.adobe.com/message/1944754#1944754
app.activeDocument.activeLayer = theLayer;
//Assumes activeDocument and activeLayer
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
d = executeActionGet(ref);
return d.getInteger(charIDToTypeID('LyrI'));
};
/////////// ====MOVE EVERY LAYER SEPARETLY==========
//========move layer to center=========
function move_px(x, y)
{
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try {
var bounds = app.activeDocument.activeLayer.bounds;
var off_x = x - (bounds[2].value+bounds[0].value)/2; // getting values of layer center.
var off_y = y - (bounds[1].value+ bounds[3].value)/2;
var d1 = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
d1.putReference( charIDToTypeID( "null" ), r );
var d2 = new ActionDescriptor();
d2.putUnitDouble( charIDToTypeID( "Hrzn" ), charIDToTypeID( "#Pxl" ), off_x );
d2.putUnitDouble( charIDToTypeID( "Vrtc" ), charIDToTypeID( "#Pxl" ), off_y );
d1.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Ofst" ), d2);
executeAction( charIDToTypeID( "move" ), d1, DialogModes.NO );
}
catch (e) { alert(e); }
app.preferences.rulerUnits = old_units;
}
//=====resize layer==== (if heigh/width>1 then change height else change width)
function scale_px(roundRadius)
{
app.preferences.rulerUnits = Units.PIXELS;
try {
var bounds = app.activeDocument.activeLayer.bounds;
var selHeight = bounds[3].value - bounds[1].value;
var selWidth = bounds[2].value - bounds[0].value;
var newSize = (100 / selHeight) * roundRadius; //resize biggest side
if (selHeight / selWidth > 1) {
var newSize = (100 / selHeight) * roundRadius;
} else {
var newSize = (100 / selWidth) * roundRadius;
}
var old_units = app.preferences.rulerUnits;
var idtransform = stringIDToTypeID("transform"); // resize action from Scripting Listener
var desc55871 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref3911 = new ActionReference();
var idlayer = stringIDToTypeID("layer");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref3911.putEnumerated(idlayer, idordinal, idtargetEnum);
desc55871.putReference(idnull, ref3911);
var idfreeTransformCenterState = stringIDToTypeID("freeTransformCenterState");
var idquadCenterState = stringIDToTypeID("quadCenterState");
var idQCSAverage = stringIDToTypeID("QCSAverage");
desc55871.putEnumerated(idfreeTransformCenterState, idquadCenterState, idQCSAverage);
var idoffset = stringIDToTypeID("offset");
var desc55872 = new ActionDescriptor();
var idhorizontal = stringIDToTypeID("horizontal");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc55872.putUnitDouble(idhorizontal, idpixelsUnit, 0.000000);
var idvertical = stringIDToTypeID("vertical");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc55872.putUnitDouble(idvertical, idpixelsUnit, 0.000000);
var idoffset = stringIDToTypeID("offset");
desc55871.putObject(idoffset, idoffset, desc55872);
var idwidth = stringIDToTypeID("width");
var idpercentUnit = stringIDToTypeID("percentUnit");
desc55871.putUnitDouble(idwidth, idpercentUnit, newSize);
var idheight = stringIDToTypeID("height");
var idpercentUnit = stringIDToTypeID("percentUnit");
desc55871.putUnitDouble(idheight, idpercentUnit, newSize);
var idlinked = stringIDToTypeID("linked");
desc55871.putBoolean(idlinked, true);
var idinterfaceIconFrameDimmed = stringIDToTypeID("interfaceIconFrameDimmed");
var idinterpolationType = stringIDToTypeID("interpolationType");
var idbicubicSharper = stringIDToTypeID("bicubicSharper");
desc55871.putEnumerated(idinterfaceIconFrameDimmed, idinterpolationType, idbicubicSharper);
executeAction(idtransform, desc55871, DialogModes.NO);
}
catch (e) { alert(e); }
app.preferences.rulerUnits = old_units;
}
// ===put inside the circle===
// 1. rotate -45 degree
function rotate(degree)
{
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try {
var idtransform = stringIDToTypeID("transform");
var desc44352 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref3372 = new ActionReference();
var idlayer = stringIDToTypeID("layer");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref3372.putEnumerated(idlayer, idordinal, idtargetEnum);
desc44352.putReference(idnull, ref3372);
var idfreeTransformCenterState = stringIDToTypeID("freeTransformCenterState");
var idquadCenterState = stringIDToTypeID("quadCenterState");
var idQCSAverage = stringIDToTypeID("QCSAverage");
desc44352.putEnumerated(idfreeTransformCenterState, idquadCenterState, idQCSAverage);
var idoffset = stringIDToTypeID("offset");
var desc44353 = new ActionDescriptor();
var idhorizontal = stringIDToTypeID("horizontal");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc44353.putUnitDouble(idhorizontal, idpixelsUnit, 0.000000);
var idvertical = stringIDToTypeID("vertical");
var idpixelsUnit = stringIDToTypeID("pixelsUnit");
desc44353.putUnitDouble(idvertical, idpixelsUnit, 0.000000);
var idoffset = stringIDToTypeID("offset");
desc44352.putObject(idoffset, idoffset, desc44353);
var idangle = stringIDToTypeID("angle");
var idangleUnit = stringIDToTypeID("angleUnit");
desc44352.putUnitDouble(idangle, idangleUnit, degree);
var idlinked = stringIDToTypeID("linked");
desc44352.putBoolean(idlinked, true);
var idinterfaceIconFrameDimmed = stringIDToTypeID("interfaceIconFrameDimmed");
var idinterpolationType = stringIDToTypeID("interpolationType");
var idbicubicSharper = stringIDToTypeID("bicubicSharper");
desc44352.putEnumerated(idinterfaceIconFrameDimmed, idinterpolationType, idbicubicSharper);
executeAction(idtransform, desc44352, DialogModes.NO);
}
catch (e) { alert(e); }
app.preferences.rulerUnits = old_units;
};
/// Check size after rotation
function checkSize(roundRadius){
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try {
var bounds = app.activeDocument.activeLayer.bounds;
var selHeight = bounds[3].value - bounds[1].value;
var selWidth = bounds[2].value - bounds[0].value;
var newSize = (100 / selHeight) * roundRadius; //resize biggest sid
if (selHeight > roundRadius) {
scale_px(roundRadius);}
var old_units = app.preferences.rulerUnits;
}
catch (e) { alert(e); }
app.preferences.rulerUnits = old_units;
};
// =========set levels==========
function setLevels(){
try {
var idlevels = stringIDToTypeID("levels");
var desc58219 = new ActionDescriptor();
var idpresetKind = stringIDToTypeID("presetKind");
var idpresetKindType = stringIDToTypeID("presetKindType");
var idpresetKindCustom = stringIDToTypeID("presetKindCustom");
desc58219.putEnumerated(idpresetKind, idpresetKindType, idpresetKindCustom);
var idadjustment = stringIDToTypeID("adjustment");
var list1989 = new ActionList();
var desc58220 = new ActionDescriptor();
var idchannel = stringIDToTypeID("channel");
var ref4099 = new ActionReference();
var idchannel = stringIDToTypeID("channel");
var idchannel = stringIDToTypeID("channel");
var idcomposite = stringIDToTypeID("composite");
ref4099.putEnumerated(idchannel, idchannel, idcomposite);
desc58220.putReference(idchannel, ref4099);
var idoutput = stringIDToTypeID("output");
var list1990 = new ActionList();
list1990.putInteger(0);
list1990.putInteger(0);
desc58220.putList(idoutput, list1990);
var idlevelsAdjustment = stringIDToTypeID("levelsAdjustment");
list1989.putObject(idlevelsAdjustment, desc58220);
desc58219.putList(idadjustment, list1989);
executeAction(idlevels, desc58219, DialogModes.NO);
}
catch (e) { alert(e); }
};
////// smart object or not //////
function hasSmartObject(idx){
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "smartObject" ));
//ref.putIndex( charIDToTypeID( "Lyr " ), idx);
ref.putIdentifier( charIDToTypeID( "Lyr " ), idx);
var desc = executeActionGet(ref);
if(desc.hasKey(stringIDToTypeID('smartObject'))) {return true}
else {return false};
};
////// based on code by mike hale and paul riggott //////
function selectLayerByID(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), index);
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);
}
};