Copy link to clipboard
Copied
I need to horizontally center this text layer in JavaScript
Doing this manually requires clicking this button:
The end result being:
Is there a way to do this in JavaScript?
Copy link to clipboard
Copied
I got this code with JavaScript listener, it is working. You need to select layers before running this script, one of them have to be locked - "background" layer.
var idAlgn = charIDToTypeID( "Algn" );
var desc32 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref5.putEnumerated( idLyr, idOrdn, idTrgt );
desc32.putReference( idnull, ref5 );
var idUsng = charIDToTypeID( "Usng" );
var idADSt = charIDToTypeID( "ADSt" );
var idAdCH = charIDToTypeID( "AdCH" );
desc32.putEnumerated( idUsng, idADSt, idAdCH );
executeAction( idAlgn, desc32, DialogModes.NO );
Copy link to clipboard
Copied
bound = (aD = activeDocument).activeLayer.bounds, h = aD.width / 2
aD.activeLayer.translate(h - (bound[0] + ((bound[2] - bound[0]) / 2)))
Copy link to clipboard
Copied
Set a selection you want to align the layer to. The selection for be the canvas size, a single pixel any area you want to align the layer to. Like an Imager location in a collage template. Then you use two Photoshop Align layers to selection to align the layers vertically and horizontally to the selection there are 9 relative alignments possible.
Here is how I stamp and Image name onto to an image area aligned to one of the 9 posible alignments. Left top, center top, Top right etc.
var Position = Number(Collage.msgPnl.grp5a.dd1.selection.index) + 1;
switch (Position){
case 1 : align('AdLf'); align('AdTp'); break;
case 2 : align('AdCH'); align('AdTp'); break;
case 3 : align('AdRg'); align('AdTp'); break;
case 4 : align('AdLf'); align('AdCV'); break;
case 5 : align('AdCH'); align('AdCV'); break;
case 6 : align('AdRg'); align('AdCV'); break;
case 7 : align('AdLf'); align('AdBt'); break;
case 8 : align('AdCH'); align('AdBt'); break;
case 9 : align('AdRg'); align('AdBt'); break;
default : break;
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){}
}