Skip to main content
wong168
Known Participant
August 20, 2020
Question

text location in the photoshop script

  • August 20, 2020
  • 2 replies
  • 3483 views

i am new to photoshop script ... i like to understand how to put the location of text in the script...

i have this in the script

// off set the text to be in the middle
myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );

this puts the text in the middle of a photo (any size) in photoshop...

i changed this to

myTextRef.position = new Array( docRef.width / 10, docRef.height / 10 );

it shifts the text a bit to the upper left corner ...

i changed this to

myTextRef.position = new Array( docRef.width / 30, docRef.height / 30 );

it shifts the text to the upper left corner ...

i like to put the text in the lower left corner of a photo (any size)...

i tried change to

myTextRef.position = new Array( docRef.width / -30, docRef.height / -30 );

myTextRef.position = new Array( -docRef.width / 30, -docRef.height / 30 );

but it did not work ... it shift the text outside a photo in upper left corner...

any advise how i should change in order to put the text in lower left corner...

thanks and cheers,

This topic has been closed for replies.

2 replies

Legend
August 22, 2020
Why don't you center (position) the text layer like a regular pixel layer using the coordinates of the layer's bounds?
 
JJMack
Community Expert
Community Expert
August 21, 2020

I think if you have an image layer and a text layer and you want to align the text to the center of the image. You should load the Image layer transparency as an Active selection then align the text layer to the horizontal and vertical center of the active selection. I see nothing in your code the look at the bounds of the image or the bounds if you text layer.  How do you expect to do any alignment between the image and the text.  Is the text a single line many lines a paragraph will it fit over the image. Text is very complex.  I feel it would be easier align layers then trying to figure out how Text works in Photoshop.  Dealing with text  content,  fonts, font size and document resolution is not simple. Adobe text seems to be based on points ie 72 DPI..

JJMack
wong168
wong168Author
Known Participant
August 21, 2020

i found the following script from the internet to add the file name to the photos ... it works to add the file name but not the location of the text to my preference...

 

QUOTED

// this script is a variation of the script addTimeStamp.js that is installed with PH7

if ( documents.length > 0 )
{
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

try
{
var docRef = activeDocument;

// Now create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";

var myTextRef = myLayerRef.textItem;

// strip the extension off
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split( "." );
if ( fileNameNoExtension.length > 1 ) {
fileNameNoExtension.length--;
}
fileNameNoExtension = fileNameNoExtension.join(".");

myTextRef.contents = fileNameNoExtension;

// off set the text to be in the middle
myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
myTextRef.size = 12;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
throw e;
}

// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
else
{
alert( "You must have a document open to add the filename!" );
}

END QUOTED

Stephen Marsh
Community Expert
Community Expert
August 22, 2020

"any advise how i should change in order to put the text in lower left corner..."

 

With the text layer being the active layer, select all and align to the lower-left then deselect all. This is relative to the canvas, however it could be altered to a layer if required:

 

 

// Align Active Layer

/* 
//macscripter.net/viewtopic.php?id=38890
AdLf = Align Left
AdRg = Align Right
AdCH = Align Centre Horizontal
AdTp = Align Top
AdBt = Align Bottom
AdCV = Align Centre Vertical
*/

//www.ps-scripts.com/viewtopic.php?f=66&t=7036&p=35273&hilit=align+layer#p35273

align('AdLf'); align('AdBt'); // Change as required

function align(method) { 
app.activeDocument.selection.selectAll();
   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){}
   app.activeDocument.selection.deselect();
};

 

 

 

 

Then adjust the text position as required:

 

 

 

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;        
app.preferences.rulerUnits = Units.PIXELS;
// Move the current layer 5px from the left and up 5px from the bottom
app.activeDocument.activeLayer.translate( 5, -5 );
// Restore the original ruler units
app.preferences.rulerUnits = savedRuler;