Highlighted

text location in the photoshop script

New Here ,
Aug 20, 2020

Copy link to clipboard

Copied

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,

Topics

Actions and scripting, How to, Mac

Views

94

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

text location in the photoshop script

New Here ,
Aug 20, 2020

Copy link to clipboard

Copied

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,

Topics

Actions and scripting, How to, Mac

Views

95

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Aug 20, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
New Here ,
Aug 20, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 22, 2020

Copy link to clipboard

Copied

"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; 

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Most Valuable Participant ,
Aug 22, 2020

Copy link to clipboard

Copied

Not all script you find we be good.  That script uses

 

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

 

 

To try to position the text layer it added to the center of the document.   The Script never  check the resolution of the document or the length of the file name or set some font.  I would think this scripts operation will vary with image document resolution and photoshop text tool current settings.

 

 

Text is complex..... I wrote this one years ago I don't use it but it may work for you and you should  be able to see where you can change the font and text color.

// This script was hacked by JJMack 
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
// LowerLeftFileName.jsx
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
if (!documents.length) {alert('There are no documents open.', 'No Document');} // ensure at least one document open
else { main();}

///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
	/* Internal Photoshop Text name								*/								
    var fontName = "ArialMT";
	var fontName = "TimesNewRomanPSMT";
	var fontName = "Tahoma";
	/* Text Color										*/
	textColor = new SolidColor;						
	textColor.rgb.red = 255;
	textColor.rgb.green = 0;
	textColor.rgb.blue = 0;
	/* END Variables You can hard here */

    // remember users Ruler avd Type Units and set ours
	var strtRulerUnits = app.preferences.rulerUnits;
	var strtTypeUnits = app.preferences.typeUnits;
	app.preferences.rulerUnits = Units.PIXELS;
 	app.preferences.typeUnits = TypeUnits.PIXELS;
    var testres = 72;
	res = app.activeDocument.resolution;
	if(res!=testres){ app.activeDocument.resizeImage(app.activeDocument.width.value,app.activeDocument.height.value,testres); }

	var docName = app.activeDocument.name;
	var doc = app.activeDocument;

	text_layer = doc.artLayers.add();							// Add a Layer
	text_layer.name = docName;									// Name Layer
	text_layer.kind = LayerKind.TEXT;							// Make Layer a Text Layer
	text_layer.textItem.color = textColor;						// set text layer color
/* Do not set TextType to Pargarph Text so action can position text layer
	text_layer.textItem.kind = TextType.PARAGRAPHTEXT;			// Set text layers text type
 */
	text_layer.textItem.font = fontName;						// set text font
	text_layer.blendMode = BlendMode.NORMAL						// blend mode
	text_layer.textItem.fauxBold = false;						// Bold
	text_layer.textItem.fauxItalic = false;						// Italic
	text_layer.textItem.underline = UnderlineType.UNDERLINEOFF;	// Underlibn
	text_layer.textItem.capitalization = TextCase.NORMAL;		// Case
	text_layer.textItem.antiAliasMethod = AntiAlias.SHARP;		// antiAlias
    if (doc.width>=1001) fontSize = 60;
	else fontSize = 10;
	text_layer.textItem.size = fontSize;						// set text font Size
	text_layer.textItem.position = Array(fontSize, (doc.height - fontSize ));	// set text layers position in and down 

	try{
        text_layer.textItem.contents = decodeURI(app.activeDocument.fullName);
	}
	catch (er) {
		text_layer.textItem.contents = activeDocument.name; 
		//alert("Error Setting Contents...");
	}
	if(res != testres){ app.activeDocument.resizeImage(app.activeDocument.width.value,app.activeDocument.height.value,res); }	
	app.preferences.rulerUnits = strtRulerUnits;
	app.preferences.typeUnits = strtTypeUnits;
}
///////////////////////////////////////////////////////////////////////////////
// END - main function
///////////////////////////////////////////////////////////////////////////////

 

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
r-bin LATEST
Adobe Community Professional ,
Aug 22, 2020

Copy link to clipboard

Copied

Why don't you center (position) the text layer like a regular pixel layer using the coordinates of the layer's bounds?
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...